Mistwald
Time Limit : 4000/2000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 54 Accepted Submission(s) : 23
Problem Description
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.
P
Input
T
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
Test cases are separated by a blank line.
Output
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;
}