首页 > 代码库 > zoj 3497 Mistwald

zoj 3497 Mistwald

Mistwald

Time Limit: 2 Seconds      Memory Limit: 65536 KB

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 (MN). Estelle Bright and her friends are initially at Scene (1, 1), the entering scene. They should leave Mistwald from Scene (MN), 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.

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 ≤ MN ≤ 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 (iN). A scene description has the form "((x1,y1),(x2,y2),(x3,y3),(x4,y4))" (1 ≤ xk ≤ M; 1 ≤ yk ≤ N; 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

这道题给了一个有向图,每个节点出度最多为4,规定起点(1,1),终点(m,n),问能否在p步内到达,注意中间节点不能经过终点。p步后,如果只能到达终点,则输出“True”,不能到达输出“False”,其他情况输出“Maybe”。

首先将图转化为矩阵,图是二维的,可以通过i*p+j转化为一维,矩阵构造出来直接flody就可以了,如果ans.ma[0][n-1]为假,肯定是False,为真时再判断一下是否可能到其他点是否可以到达,可以时是Maybe,不可以时是True。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int n;
struct matrix
{
    int ma[40][40];
};
void flody(matrix &x,matrix y)
{
    matrix ans;
    memset(ans.ma,0,sizeof(ans.ma));
    for(int i=0; i<n-1; i++)
    {
        for(int j=0; j<n; j++)
        {
            for(int k=0;k<n-1;k++)
            {
                if(x.ma[i][k]&&y.ma[k][j])
                ans.ma[i][j]=1;
            }
        }
    }
    x=ans;
}
void pow_ma(matrix &a,int m)
{
    matrix ans;
    int sign=1;
    while(m)
    {
        if(m&1)
        {
            if(sign)
            {
                ans=a;
                sign=0;
            }
            else
            flody(ans,a);
        }
        flody(a,a);
        m=m>>1;
    }
    a=ans;
}
int main()
{
    int x[5];
    int y[5];
    int p,q,t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&p,&q);
        n=p*q;
        matrix a;
        memset(a.ma,0,sizeof(a.ma));
        getchar();
        for(int i=0;i<p;i++)
        {
            for(int j=0;j<q;j++)
            {
                scanf("((%d,%d),(%d,%d),(%d,%d),(%d,%d))",&x[0],&y[0],&x[1],&y[1],&x[2],&y[2],&x[3],&y[3]);
                for(int k=0;k<4;k++)
                {
                    a.ma[i*q+j][(x[k]-1)*q+(y[k]-1)]=1;
                }
                getchar();
            }
        }
        int qu;
        scanf("%d",&qu);
        matrix ans;
        while(qu--)
        {
            int m;
            scanf("%d",&m);
            if(m==0)
            {
                if(n==1)
                printf("True\n");
                else
                printf("False\n");
                continue;
            }
            ans=a;
            pow_ma(ans,m);
            if(ans.ma[0][n-1])
            {
                int sign=0;
                for(int i=0;i<n-1;i++)
                {
                    if(ans.ma[0][i])
                    {
                        sign=1;
                        break;
                    }
                }
                if(sign)
                printf("Maybe\n");
                else
                printf("True\n");
            }
            else
            printf("False\n");
        }
        printf("\n");
    }
    return 0;
}



zoj 3497 Mistwald