ZOJ 3497 Mistwald(矩阵快速幂)

31 篇文章 0 订阅
4 篇文章 0 订阅

题目:

Mistwald

Time Limit: 2 Seconds      Memory Limit: 65536 KB

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 ≤ xkM; 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 ≤ 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

题意:给一个图,问从起点到终点能否在P秒完成。

思路:以邻接矩阵存图,设矩阵为A,那么A^P即为走P秒后的图,如果此时起点和终点连通那么就可以。注意中途走到终点后是不能再走的,必须出去,所以把从终点出发的边都删掉就行了。

代码:

#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include<climits>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime>
using namespace std;

#define PB push_back
#define MP make_pair

#define REP(i,x,n) for(int i=x;i<(n);++i)
#define FOR(i,l,h) for(int i=(l);i<=(h);++i)
#define FORD(i,h,l) for(int i=(h);i>=(l);--i)
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define RI(X) scanf("%d", &(X))
#define RII(X, Y) scanf("%d%d", &(X), &(Y))
#define RIII(X, Y, Z) scanf("%d%d%d", &(X), &(Y), &(Z))
#define DRI(X) int (X); scanf("%d", &X)
#define DRII(X, Y) int X, Y; scanf("%d%d", &X, &Y)
#define DRIII(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)
#define OI(X) printf("%d",X);
#define RS(X) scanf("%s", (X))
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
#define Swap(a, b) (a ^= b, b ^= a, a ^= b)
#define Dpoint  strcut node{int x,y}
#define cmpd int cmp(const int &a,const int &b){return a>b;}

 /*#ifdef HOME
    freopen("in.txt","r",stdin);
    #endif*/
const int MOD = 1e9+7;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<double> VD;
typedef long long LL;
typedef pair<int,int> PII;
//#define HOME

int Scan()
{
	int res = 0, ch, flag = 0;

	if((ch = getchar()) == '-')				//判断正负
		flag = 1;

	else if(ch >= '0' && ch <= '9')			//得到完整的数
		res = ch - '0';
	while((ch = getchar()) >= '0' && ch <= '9' )
		res = res * 10 + ch - '0';

	return flag ? -res : res;
}
/*----------------PLEASE-----DO-----NOT-----HACK-----ME--------------------*/


int sz;
struct Matrix
{
    int m[30][30];
    void init()
    {
        MS0(m);
    }
    void show()
    {
        for(int i=0;i<sz;i++)
            for(int j=0;j<sz;j++)
        {
            printf("%d ",m[i][j]);
            if(j==sz-1)
                printf("\n");
        }
    }
};

Matrix mul(Matrix a,Matrix b)
{
    Matrix c;
    c.init();
    for(int i=0;i<sz;i++)
        for(int j=0;j<sz;j++)
        for(int k=0;k<sz;k++)
        c.m[i][j]=c.m[i][j]|(a.m[i][k]&&b.m[k][j]);
    return c;
}
Matrix mypow(Matrix a,int k)
{
    Matrix ans;
    ans.init();
    for(int i=0;i<sz;i++)
        ans.m[i][i]=1;
    Matrix tmp=a;
    while(k)
    {
         if(k&1)
            ans=mul(ans,tmp);
        tmp=mul(tmp,tmp);
        k>>=1;
    }
    return ans;
}
int main()
{
int T;
RI(T);
while(T--)
{
    int m,n;
    RII(m,n);
    Matrix A;
    A.init();
    for(int i=0;i<m;i++)
        for(int j=0;j<n;j++)
    {
        int t=i*n+j;
        char str[30];
        scanf("%s",str);
        int x1,y1,x2,y2,x3,y3,x4,y4;
        x1=str[2]-'0';
        y1=str[4]-'0';
        x2=str[8]-'0';
        y2=str[10]-'0';
        x3=str[14]-'0';
        y3=str[16]-'0';
        x4=str[20]-'0';
        y4=str[22]-'0';
        A.m[t][(x1-1)*n+y1-1]=1;
        A.m[t][(x2-1)*n+y2-1]=1;
        A.m[t][(x3-1)*n+y3-1]=1;
        A.m[t][(x4-1)*n+y4-1]=1;

    }
    sz=m*n;
    for(int i=0;i<sz;i++)
        A.m[sz-1][i]=0;
   // A.show();
    int Q;
    RI(Q);
    while(Q--)
    {int p;
    RI(p);
        Matrix ans=mypow(A,p);
        //ans.show();
    if(ans.m[0][sz-1]==0)
        printf("False\n");
    else
    {   int ok=1;
        for(int i=0;i<sz-1;i++)
        {
            if(ans.m[0][i])
            {
                ok=0;
                break;
            }
        }
        if(ok)
            printf("True\n");
        else
            printf("Maybe\n");

    }
    }
printf("\n");
}


        return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值