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.
OutputFor 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 Input2 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 2Sample Output
Maybe False Maybe True False题意:在一个m*n的地图上,每点表示一个房间,每个房间有4个门,表示从当前房间通过该门可以一步到达的房间坐标
一共Q次询问,求在每次询问中,从房间(0,0)走x步能否到达房间(m-1,n-1)
如果一定能到达输出“true”,一定不能到达输出“False”,其他的输出“Maybe”
思路:将每个房间从0到n*m-1标号,能互相到达的房间建边,利用矩阵的幂求能否x步可达
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
struct Matrix{
int m[40][40];
};
int x[5],y[5];
int n;
Matrix init(){
Matrix ans;
int a,b;
scanf("%d%d",&a,&b);
n=a*b;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
ans.m[i][j]=0;
for(int i=0;i<a;i++){
for(int j=0;j<b;j++){
getchar();
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]);
if(i==a-1&&j==b-1) continue;
int t=i*b+j;//标号
for(int k=0;k<4;k++){
x[k]--;
y[k]--;
int t1=x[k]*b+y[k];
ans.m[t][t1]=1;//建边
}
}
}
return ans;
}
Matrix Mul(Matrix a,Matrix b){
Matrix ans;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
ans.m[i][j]=0;
for(int k=0;k<n;k++){
ans.m[i][j]+=a.m[i][k]*b.m[k][j];
}
}
}
return ans;
}
Matrix quick(Matrix a,int b){ //矩阵快速幂
Matrix ans;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
ans.m[i][j] = (i==j);
while(b){
if(b&1) ans=Mul(ans,a);
a=Mul(a,a);
b>>=1;
}
return ans;
}
void gao(Matrix a,int b){
Matrix ans=quick(a,b);
if(!ans.m[0][n-1]){
printf("False\n");
return;
}
for(int i=1;i<n-1;i++)
if(ans.m[0][i]){
printf("Maybe\n");
return;
}
printf("True\n");
}
int main(){
int T,Q;
scanf("%d",&T);
while(T--){
Matrix ans = init();
scanf("%d",&Q);
while(Q--){
int x;
scanf("%d",&x);
gao(ans,x);
}
puts("");
}
return 0;
}