题意:给一个矩阵,求第x步时能否从起点走到终点。一开始死活都想不到啊。。。什么时候maybe啊。。。都怪我英语差。题目中的 "Other time such as staying and resting can be ignored"意思是这样的时间是完全不用考虑的,即使是你的室友在撒谎要考虑maybe的时候,maybe是因为在那样的时间下从起点还可以走到另外的点,也可以走到终点。
#include<cstdio>
#include<cstring>
const int LMT=30;
struct matrix
{
int mat[LMT][LMT];
int n,m;
matrix(int a,int b):n(a),m(b)
{
memset(mat,0,sizeof(mat));
}
};
matrix operator *(const matrix &a,const matrix &b)
{
matrix ret(a.n,b.m);
for(int k=0;k<a.m;k++)
for(int i=0;i<a.n;i++)
if(a.mat[i][k])
for(int j=0;j<b.m;j++)
ret.mat[i][j]|=a.mat[i][k]&b.mat[k][j];
return ret;
}
matrix pow(int x,matrix sor)
{
int i;
matrix res(sor.n,sor.m);
for(i=0;i<res.n;i++)res.mat[i][i]=1;
while(x)
{
if(x&1)res=res*sor;
sor=sor*sor;
x>>=1;
}
return res;
}
int main(void)
{
int T,q,n,m,x[4],y[4],i,jj,j,s,t,cnt,pos,to,xx;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&m,&n);getchar();
cnt=m*n;s=0;t=m*n-1;
matrix gra(cnt,cnt),tem(cnt,cnt);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
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]);
getchar();
pos=i*n+j;
for(jj=0;jj<4;jj++)
{
x[jj]--;y[jj]--;
to=x[jj]*n+y[jj];
gra.mat[pos][to]=1;
}
}
memset(gra.mat[t],0,sizeof(gra.mat[t]));
scanf("%d",&q);
while(q--)
{
scanf("%d",&xx);
tem=pow(xx,gra);
if(tem.mat[s][t]==0)puts("False");
else
{
for(i=0;i<cnt&&!tem.mat[s][i];i++);
if(i==t)puts("True");
else puts("Maybe");
}
}
puts("");
}
return 0;
}