集训队专题(2)1006 Mistwald

Mistwald

Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 54   Accepted Submission(s) : 23
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 (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

此题用到的是矩阵的一个很基础的用法:位置变换。这中间还可以用到位运算的小技巧做判断^^。

#include <cstdio>
#include <cstring>
struct Matrix
{
	int mat[30][30];
}unit,init;
int N,M,T;
void Init()  
{
    scanf("%d%d",&N,&M);  
    getchar();  
    int i,j,x1,y1,x2,y2,x3,y3,x4,y4;  
    memset(init.mat,0,sizeof(init.mat));//每次清零!  
    for(i=1; i<=N; i++)  
        for(j=1; j<=M; j++)  
        {  
            scanf("((%d,%d),(%d,%d),(%d,%d),(%d,%d))",&x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4);  
            getchar();  
            if ((i-1)*M+j == N*M) continue;//舍掉终点的出边  
            init.mat[(i-1)*M+j][(x1-1)*M+y1]=1;  
            init.mat[(i-1)*M+j][(x2-1)*M+y2]=1;  
            init.mat[(i-1)*M+j][(x3-1)*M+y3]=1;  
            init.mat[(i-1)*M+j][(x4-1)*M+y4]=1;  
            unit.mat[(i-1)*M+j][(i-1)*M+j]=1;  
        }  
}  
Matrix multi(Matrix a,Matrix b)  
{  
    int i,j,k;  
    Matrix c;
    for(i=1;i<=T;i++)  
        for(j=1;j<=T;j++)  
        {  
            c.mat[i][j]=0;  
            for(k=1;k<=T;k++) 
            c.mat[i][j] |= (a.mat[i][k]&b.mat[k][j]);  
        }  
    return c;  
} 
Matrix cal(int k)//矩阵二分快速幂  
{  
    Matrix i=init,ans=unit;  
    while(k)
	{  
        if(k&1)  
            ans = multi(ans,i);  
        i = multi(i,i);  
        k >>= 1;  
    }  
    return ans;  
}  
int main()  
{     
    int t;  
    scanf("%d",&t);  
    while(t--)
    {
        Init(); 
        T = N*M;
        int num,q;  
        scanf("%d",&num);
        int i;  
        while(num--)  
        {  
            scanf("%d",&q);  
            Matrix r=cal(q);  
            if(r.mat[1][T]==0)  
                printf("False\n");  
            else  
            {  
                for(i=1;i<=T;i++)  
                {  
                    if(r.mat[1][i])  
                        break;  
                }  
                if(i == T)  
                    printf("True\n");  
                else printf("Maybe\n");  
            }  
        }  
        printf("\n");
    }  
    return 0;  
}  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值