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.
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 ≤ 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
思路:构造出图的邻接矩阵,然后用矩阵乘求p次幂。
最后判断Matrix[1][n],若为假,则为“False”,为真,再判断Matrix[1][0…n-1],若有一个为真为“Maybe”,其他为“True”。
#include<cstdio> #include<math.h> #include<cstring> #include<climits> #include<string> #include<queue> #include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<climits> #include<string> #include<queue> #include<stack> #include<set> #include<map> #include<algorithm> using namespace std; #define rep(i,j,k)for(i=j;i<k;i++) #define per(i,j,k)for(i=j;i>k;i--) #define MS(x,y)memset(x,y,sizeof(x)) #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define ll long long #define abs(x) (x>0?x:-x) const int INF=0x7ffffff; const ll MAX=1e18; struct Matrix { ll m[50][50]; }I,A,B; int ssize; int i,j,k,n,m; Matrix Mul(Matrix a,Matrix b) { Matrix c; for(i=1;i<=ssize;i++) for(j=1;j<=ssize;j++){ c.m[i][j]=0; for(k=1;k<=ssize;k++) c.m[i][j]+=(a.m[i][k]*b.m[k][j]); } return c; } Matrix quickpagow(ll n) { Matrix m=A,b=I; while(n){ if(n&1) b=Mul(b,m); n>>=1; m=Mul(m,m); } return b; } int main() { int T; scanf("%d",&T); while(T--){ MS(I.m,0); MS(A.m,0); MS(B.m,0); int N ,M; scanf("%d%d",&N,&M); getchar(); int L=N*M; for(i=1;i<=L;i++){ I.m[i][i] = 1; } int x1, y1, x2, y2, x3, y3, x4, y4; 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==N&&j==M) continue; A.m[(i-1)*M+j][(x1-1)*M+y1]=1; A.m[(i-1)*M+j][(x2-1)*M+y2]=1; A.m[(i-1)*M+j][(x3-1)*M+y3]=1; A.m[(i-1)*M+j][(x4-1)*M+y4]=1; } ssize=L; int q,p; scanf("%d",&q); while(q--){ scanf("%d",&p); B=quickpagow(p); if(B.m[1][L]==0) printf("False\n"); else{ int flag=0; for(i=1;i<L;i++){ if(B.m[1][i]){ flag=1; break; } } if(!flag)printf("True\n"); else printf("Maybe\n"); } } printf("\n"); } return 0; }