Link:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4320
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 Qlines, 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
Author: WU, Jun
Contest: The 8th Zhejiang Provincial Collegiate Programming Contest
编程思想:邻接矩阵A的k次幂得到的新矩阵B中,bij表示ai到aj长度为k的通路数。这里无须求通路数,所以直接用离散数学书中所讲的,矩阵相乘时用逻辑加的结果表示能否k步到达终点即可。该方法计算的是任意两点通路数,而本题目要求走到(n,m)点之后就不能再走了,所以要把所有的有关于最后一个点的边去掉。即终点不能有出边,基于该条件,只要把终点那一行全部置为0即可。首先构造出图的邻接矩阵,然后用矩阵乘法+log(p)便可求p次幂。最后判断Matrix[1][n*m],若为假,则为“False”,为真,再判断Matrix[1][0…n-1],若有一个为真为“Maybe”,其他为“True”。本题另外需要注意的是数据读取格式和数据输出格式的问题。
AC code:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#define LL long long
#define MAXN 1000010
using namespace std;
const int INF=0x3f3f3f3f;
//----以下为矩阵快速幂模板-----//
//const int mod=1000;//模3,故这里改为3即可
int mod=1000;
const int NUM=32;//定义矩阵能表示的最大维数
int N;//N表示矩阵的维数,以下的矩阵加法、乘法、快速幂都是按N维矩阵运算的
struct Mat{//矩阵的类
int a[NUM][NUM];
Mat(){memset(a,0,sizeof(a));}
void init()//将其初始化为单位矩阵
{
memset(a,0,sizeof(a));
for(int i=0;i<NUM;i++)
{
a[i][i]=1;
}
}
};
Mat add(Mat a,Mat b)//(a+b)%mod 矩阵加法
{
Mat ans;
for(int i=0;i<N;i++)
{
for(int j=0;j<N;j++)
{
ans.a[i][j]=(a.a[i][j]%mod)+(b.a[i][j]%mod);
ans.a[i][j]%=mod;
}
}
return ans;
}
Mat mul(Mat a,Mat b) //(a*b)%mod 矩阵乘法
{
Mat ans;
for(int i=1;i<=N;i++)
{
for(int j=1;j<=N;j++)
{
ans.a[i][j]=0;
for(int k=1;k<=N;k++)
{
ans.a[i][j]=ans.a[i][j]|(a.a[i][k]&b.a[k][j]);
}
//ans.a[i][j]%=mod;
}
}
return ans;
}
Mat power(Mat a,int num)//(a^n)%mod 矩阵快速幂
{
Mat ans;
ans.init();
while(num)
{
if(num&1)
{
ans=mul(ans,a);
}
num>>=1;
a=mul(a,a);
}
return ans;
}
Mat pow_sum(Mat a,int num)//(a+a^2+a^3....+a^n)%mod 矩阵的幂和
{
int m;
Mat ans,pre;
if(num==1)
return a;
m=num/2;
pre=pow_sum(a,m);
ans=add(pre,mul(pre,power(a,m)));
if(num&1)
ans=add(ans,power(a,num));
return ans;
}
void output(Mat a)//输出矩阵
{
for(int i=1;i<=N;i++)
{
for(int j=1;j<=N;j++)
{
printf("%d%c",a.a[i][j],j==N-1?'\n':' ');
}
}
}
//----以上为矩阵快速幂模板-----//
int main()
{
//freopen("D:\in.txt","r",stdin);
int T,n,m,i,j,u,v1,v2,v3,v4,x1,y1,x2,y2,x3,y3,x4,y4,Q,P;
scanf("%d",&T);
while(T--)
{
Mat A,ans;
scanf("%d%d\n",&n,&m);
//getchar();//注意这里要getchar或者scanf里面加'\n',否则会影响下面的读取
//因为下面的scanf读取指定格式的字符,若没先读取掉回车符,scanf将处于等待"(("的状态,最后读取不正确
//memset(A.a,0,sizeof(A.a));
//memset(ans.a,0,sizeof(ans.a));
N=n*m;
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
scanf("((%d,%d),(%d,%d),(%d,%d),(%d,%d))\n",&x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4);
//getchar();//注意这里要getchar或者scanf里面加'\n',否则会影响下面的读取
u=(i-1)*m+j;
if(u==n*m)
continue;
v1=(x1-1)*m+y1;
v2=(x2-1)*m+y2;
v3=(x3-1)*m+y3;
v4=(x4-1)*m+y4;
A.a[u][v1]=1;
A.a[u][v2]=1;
A.a[u][v3]=1;
A.a[u][v4]=1;
}
}
scanf("%d",&Q);
int st=1;
int ed=n*m;
while(Q--)
{
scanf("%d",&P);
ans=power(A,P);
if(ans.a[st][ed]==0)
{
printf("False\n");
}
else
{
for(i=1;i<=ed-1;i++)
{
if(ans.a[st][i]!=0)
{
printf("Maybe\n");
break;
}
}
if(i==ed)
{
printf("True\n");
}
}
}
//if(T!=0)
printf("\n");
}
return 0;
}