首页 > 代码库 > ZOJ 3497 Mistwald

ZOJ 3497 Mistwald

矩阵快速幂。

邻接矩阵的$P$次方就是走$P$步之后的方案数,这里只记录能否走到就可以了。然后再判断一下三种情况即可。

#pragma comment(linker, "/STACK:1024000000,1024000000")#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<vector>#include<map>#include<set>#include<queue>#include<stack>#include<ctime>#include<iostream>using namespace std;typedef long long LL;const double pi=acos(-1.0);void File(){    freopen("D:\\in.txt","r",stdin);    freopen("D:\\out.txt","w",stdout);}template <class T>inline void read(T &x){    char c = getchar();    x = 0;    while(!isdigit(c)) c = getchar();    while(isdigit(c))    {        x = x * 10 + c - 0;        c = getchar();    }}int T;int n,m,a,b,c,d,e,f,g,h;char s[10000];int r[30][30];long long p;struct Matrix{    int A[30][30];    int R, C;    Matrix operator*(Matrix b);};Matrix X, Y, Z;Matrix Matrix::operator*(Matrix b){    Matrix c;    memset(c.A, 0, sizeof(c.A));    int i, j, k;    for (i = 1; i <= R; i++)        for (j = 1; j <= b.C; j++)            for (k = 1; k <= C; k++)                if(c.A[i][j]==0) c.A[i][j]=(A[i][k]&b.A[k][j]);    c.R = R; c.C = b.C;    return c;}void init(){    memset(X.A, 0, sizeof X.A);    memset(Y.A, 0, sizeof Y.A);    memset(Z.A, 0, sizeof Z.A);    for(int i=1;i<=n*m;i++) Y.A[i][i]=1;    Y.R=Y.C=n*m;    for(int i=1;i<=n*m;i++)        for(int j=1;j<=n*m;j++) X.A[i][j]=r[i][j];    X.R=X.C=n*m;}void work(){    while (p)    {        if (p % 2 == 1) Y = Y*X;        p = p >> 1;        X = X*X;    }}int main(){    scanf("%d",&T);    while(T--)    {        scanf("%d%d",&n,&m);        memset(r,0,sizeof r);        for(int i=1;i<=n;i++)        {            for(int j=1;j<=m;j++)            {                scanf(" ((%d,%d),(%d,%d),(%d,%d),(%d,%d))",&a,&b,&c,&d,&e,&f,&g,&h);                if(i==n&&j==m) continue;                r[(i-1)*m+j][(a-1)*m+b]=1;                r[(i-1)*m+j][(c-1)*m+d]=1;                r[(i-1)*m+j][(e-1)*m+f]=1;                r[(i-1)*m+j][(g-1)*m+h]=1;            }        }        int Q; scanf("%d",&Q);        while(Q--)        {            scanf("%lld",&p);            init();            work();            if(Y.A[1][n*m]==0) printf("False\n");            else            {                bool Find=0;                for(int i=1;i<=n*m-1;i++) if(Y.A[1][i]==1) Find=1;                if(Find==0) printf("True\n");                else printf("Maybe\n");            }        }        printf("\n");    }    return 0;}

 

ZOJ 3497 Mistwald