ZOJ 3497Mistwald 矩阵加速DP

题意:给一个矩阵,求第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;
} 


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值