欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Mistwald(矩阵快速幂解类似图论问题)

程序员文章站 2022-06-04 12:17:43
...

Mistwald

Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Problem Description
In chapter 4 of the game Trails in the Sky SC, Estelle Bright and her friends are crossing Mistwald to meet their final enemy, Lucciola.

Mistwald is a mysterious place. It consists of M * N scenes, named Scene (1, 1) to Scene (M, N). Estelle Bright and her friends are initially at Scene (1, 1), the entering scene. They should leave Mistwald from Scene (M, N), the exiting scene. Note that once they reach the exiting scene, they leave Mistwald and cannot come back. A scene in Mistwald has four exits, north, west, south, and east ones. These exits are controlled by Lucciola. They may not lead to adjacent scenes. However, an exit can and must lead to one scene in Mistwald.

Mistwald(矩阵快速幂解类似图论问题)

Estelle Bright and her friends walk very fast. It only takes them 1 second to cross an exit, leaving a scene and entering a new scene. Other time such as staying and resting can be ignored. It is obvious that the quicker they leave Mistwald, the better.

Now you are competing with your roommate for who uses less time to leave Mistwald. Your roommate says that he only uses P seconds. It is known that he lies from time to time. Thus, you may want to code and find out whether it is a lie.

Input

There are multiple test cases. The first line of input is an integer T ≈ 10 indicating the number of test cases.

Each test case begins with a line of two integers M and N (1 ≤ M, N ≤ 5), separated by a single space, indicating the size of Mistwald. In the next M lines, the ith line contains N pieces of scene information, separated by spaces, describing Scene (i, 1) to Scene (i, N). A scene description has the form "((x1,y1),(x2,y2),(x3,y3),(x4,y4))" (1 ≤ xkM; 1 ≤ ykN; 1 ≤ k ≤ 4) indicating the locations of new scenes the four exits lead to. The following line contains an integer Q (1 ≤ Q ≤ 100). In the next Q lines, each line contains an integer P (0 ≤ P ≤ 100,000,000), which is the time your roommate tells you.

Test cases are separated by a blank line.

Output

For each P, output one of the following strings in one line: "True" if it cannot be a lie; "Maybe" if it can be a lie; "False" if it must be a lie.

Print a blank line after each case.

Sample Input

2
3 2
((3,1),(3,2),(1,2),(2,1)) ((3,1),(3,1),(3,1),(3,1))
((2,1),(2,1),(2,1),(2,2)) ((3,2),(3,2),(3,2),(3,2))
((3,1),(3,1),(3,1),(3,1)) ((3,2),(3,2),(3,2),(1,1))
3
1
2
10

2 1
((2,1),(2,1),(2,1),(2,1))
((2,1),(2,1),(2,1),(2,1))
2
1
2

Sample Output

Maybe
False
Maybe

True
False

 

题意

有N*M的地图,地图上每个点能到其他4个点(可能是自己,也可能重复),每次从(0,0)开始,到(N-1,M-1)结束(注意:到终点时直接结束,不能从终点再到其他点)。有Q个询问,每次给一个数p,问能否经过恰好p个点到终点,如果不能输出False,如果经过p个点只能到终点,输出True,如果经过p个点可以到终点也可以到其他点,则输出Maybe。


思路

用矩阵快速幂来解:

首先,我们把地图的每个点抽象成一个数字,比如2*2的地图:

 1 2

 3 4

第一行第一列为1,第一行第二列为2

第二行第一列为3,第二行第二列为4


然后,就可以通过输入的点与点相互到达的信息构建一个邻接矩阵,ex:如果1能到3,则mp[1][3]=1,否则,mp[1][3]=0;


思考一下,把得到的邻接矩阵^2会得到什么?


矩阵乘法是行*列,在我们的邻接矩阵里行代表的是一个点(该行设为x)能否到其他点,而列(该列设为y)代表的是所有点中有没有点能到达y。那么如果x能到达z,且z能达到y,是不是就意味着x通过2个点就能到达y。所以,邻接矩阵^2表示初始所有点通过2次后能到达哪些点。


那么,对于每次询问(p),我们只要求出邻接矩阵^(p),看mp[0][n*m-1]是不是等于1,即经过p次后,能否从位置0到位置N*M-1。若可以的话,再判断下位置0还能不能到除了终点后的其他点,能的话输出Maybe,反之输出True。


注意:到达终点后就不能再到其他点了,所以邻接矩阵中代表终点的那一行应全为0。


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;

typedef long long ll;

typedef struct {
    ll m[50][50];
}Matrix;

int n,m;
int siz;

Matrix Mul(Matrix a, Matrix b)
{
    Matrix c;
    memset(c.m, 0, sizeof(c.m));
    for (int i = 0; i < siz; i++)
    {
        for (int j = 0; j < siz; j++)
        {
            for (int k = 0; k < siz; k++)
            {
                c.m[i][j] = (c.m[i][j] + (a.m[i][k] * b.m[k][j])  ) ;
            }
        }
    }
    return c;
}

Matrix fastm(Matrix a, ll num)
{
    Matrix res;
    memset(res.m, 0, sizeof(res.m));
    for(int i=0;i<siz;i++)
        res.m[i][i]=1;
    while (num)
    {
        if (num & 1)
            res = Mul(res, a);
        num >>= 1;
        a = Mul(a, a);
    }
    return res;
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        getchar();
        Matrix a;
        memset(a.m,0,sizeof(a.m));
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                int x1,y1,x2,y2,x3,y3,x4,y4;
                scanf("((%d,%d),(%d,%d),(%d,%d),(%d,%d))",&x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4);
                getchar();
                if(i==n-1&&j==m-1)  continue;
                int now=i*m+j;
                a.m[now][(x1-1)*m+y1-1]=1;
                a.m[now][(x2-1)*m+y2-1]=1;
                a.m[now][(x3-1)*m+y3-1]=1;
                a.m[now][(x4-1)*m+y4-1]=1;
            }
        }
        siz=n*m;
        int Q;
        scanf("%d",&Q);
        while(Q--)
        {
            int p;
            scanf("%d",&p);
            Matrix res=fastm(a,p);
            int flag=0;
            if(res.m[0][siz-1]==0)
                printf("False\n");
            else
            {
                for(int i=0;i<siz-1;i++)
                {
                    if(res.m[0][i]){
                        flag=1;
                        break;
                    }
                }
                if(flag==1) printf("Maybe\n");
                else    printf("True\n");
            }
        }
        printf("\n");
    }
    return 0;
}


相关标签: 矩阵快速幂