zoj 3497

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 (MN). Estelle Bright and her friends are initially at Scene (1, 1), the entering scene. They should leave Mistwald from Scene (MN), 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 ≤ MN ≤ 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 (iN). 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 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


题意:给定一个有向图(最多25个节点,每个节点的出度最多为4),给定起点和终点,然后从起点开始走,走到终点就停止,否则一直往下走,问能不能P步到达终点。也就是说从起点出发,走一条长度为P的路径,路径中间点不能经过终点(但可以反复经过其他点)。如果从起点出发P步后,不能到达终点,就是False,如果可以到达终点也可以到其他别的点,就是Maybe,如果P步后只能到达终点(到别的点没有长度为P的路径),则是Yes

样例输入意思:四个坐标分别为,m*n矩阵中的坐标,通过次计算出每个节点对应的出口,然后见图。

思路:显然的矩阵乘法。图的临接矩阵A p次方Ap中为1的元素(i,j)表示节点i到节点j有一条长度为p的路径(经历的节点可能重复)。要理解矩阵的含义,两个矩阵相乘如果(x,y)元素为1,而(y,z)元素为1,则结果(x,z)元素为1,这表明通过yxz连起来了。而题目要求经过终点就不能走了,所以在做矩阵乘法时,需要把(x,n-1 (n-1,y)这样决定的(x,y)去掉。(n-1表示终点)。做乘法时,中间点小心一点就好了。矩阵乘法和floyd在本质上是一样的……

 矩阵的P次方运用的是经典的log(P)的算法。最后看一下结果矩阵的首行(0行)里面有几个1,以及(0,n-1)是不是1,来决定结果。

 

定理:设G是带有相对于顶点顺序v1,v2,......,vn的邻接矩阵A的
图(允许带有无向或有向边,带有多重边和环).
从vi到vj的长度为r的不同通路的数目等于A^r 的第 (i,j) 项,
其中r是正整数.
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int map[26][26],tmap[26][26];
int res[26][26];
int x[5],y[5];//表示给出的坐标
int n,m,mul;
void MulPow(int a[][26],int b[][26])//矩阵相乘
{
    int t[26][26];
    for(int i=0;i<mul-1;i++)
    for(int j=0;j<mul;j++)
    {
        t[i][j]=0;
        for(int k=0;k<mul-1;k++)
        if(a[i][k]&&b[k][j])
        {
            t[i][j]=1;
            break;
        }
    }
    memcpy(a,t,sizeof(t));
}
int main()
{
    int cas,q,time;
    char s;
    cin>>cas;
    while(cas--)
    {
        cin>>m>>n;
        mul=n*m;
        memset(map,0,sizeof(map));
        getchar();
        for(int i=0;i<m;i++)//构图
        for(int 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]);
            for(int k=0;k<4;k++)
            {
                x[k]--;y[k]--;
                map[i*n+j][n*x[k]+y[k]]=1;
            }
            getchar();
        }
        cin>>q;
        while(q--)
        {
            cin>>time;
            if(!time)
            {
                if(mul==1)
                cout<<"True"<<endl;
                else
                cout<<"False"<<endl;
                continue;
            }
            bool ok=true;
            memcpy(tmap,map,sizeof(map));
            while(time)
            {
                if(time%2==1)//奇数
                {
                    if(ok)
                    {
                        memcpy(res,tmap,sizeof(tmap));
                        ok=false;
                    }
                    else
                    MulPow(res,tmap);
                }
                time/=2;
                MulPow(tmap,tmap);
            }
            int ans=0;
            for(int i=0;i<mul-1;i++)
            if(res[0][i])
            ans++;
            if(res[0][mul-1])//Matrix[1][n],若为假,则为“False”,
                            //为真,再判断Matrix[1][0…n-1],
                            //若有一个为真为“Maybe”,其他为“True”。
            {
                if(!ans)
                cout<<"True"<<endl;
                else
                cout<<"Maybe"<<endl;
            }
            else
            cout<<"False"<<endl;
        }
        cout<<endl;
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值