Mistwald ZOJ - 3497 (矩阵快速幂)

本文介绍了一个基于游戏《Trails in the Sky SC》中Mistwald章节的路径寻找问题。通过使用矩阵快速幂的方法来判断玩家能否在特定时间内从起点到达终点,并详细解释了算法实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Mistwald

ZOJ - 3497

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


思路:一开始就老想着搜索了,但是搜索还不知如何下手,然后看了网上题解才恍然大悟,如果大家学过离散数学的话,这个题实际上就是让我们求某个点到某个点的多步可达路径,而求多步可达路径就是矩阵的幂,也就是传递。

所以我们把所给的矩阵位置每个点按1-m*n表好号,然后就相当于每个点可以通向四个点,有向路径,然后用邻接矩阵存下来,接下来用矩阵快速幂求解就可以了。

code:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define LL long long
struct Matrix
{
    LL  m[50][50];
} I, A, B;
int ssize;
Matrix Mul(Matrix a,Matrix b)
{
    int i, j, k;
    Matrix c;
    for(i = 1; i <= ssize; i++)
    {
        for(j = 1; j <= ssize; j++)
        {
            c.m[i][j]=0;
            for(k = 1; k <= ssize; k++)
            {
                c.m[i][j]+=(a.m[i][k]*b.m[k][j]);
            }
        }
    }
    return c;
}

Matrix quickpagow(LL n)
{
    Matrix m = A, b = I;
    while(n)
    {
        if(n & 1)
            b = Mul(b,m);
        n = n >> 1;
        m = Mul(m,m);
    }
    return b;
}

int main()
{
    int t;
    int N, M;
    scanf("%d",&t);
    while(t--)
    {
        memset(I.m,0,sizeof(I.m));
        memset(A.m,0,sizeof(A.m));
        memset(B.m,0,sizeof(B.m));
        scanf("%d%d",&N,&M);
        getchar();

        int T = N*M;
        for(int i = 1; i <= T; i++)
        {
            I.m[i][i] = 1;
        }
        int x1, y1, x2, y2, x3, y3, x4, y4;
        for(int i = 1; i <= N; i++)
            for(int j = 1; j <= M; j++)
            {
                scanf("((%d,%d),(%d,%d),(%d,%d),(%d,%d))",&x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4);
                getchar();
                if(i==N && j==M)//图中路径不能经过终点
                    continue;
                A.m[(i-1)*M+j][(x1-1)*M+y1]=1;
                A.m[(i-1)*M+j][(x2-1)*M+y2]=1;
                A.m[(i-1)*M+j][(x3-1)*M+y3]=1;
                A.m[(i-1)*M+j][(x4-1)*M+y4]=1;
            }
        ssize = T;
        int q, p;
        scanf("%d",&q);
        while(q--)
        {
            scanf("%d",&p);
            B = quickpagow(p);
            if(B.m[1][T]==0)
                printf("False\n");
            else
            {
                int flag = 0;
                for(int i = 1; i < T; i++)
                {
                    if(B.m[1][i])
                    {
                        flag = 1;
                        break;
                    }
                }
                if(!flag)
                    printf("True\n");
                else//  恰好P步 既能到达终点又能到达其他点
                    printf("Maybe\n");
            }
        }
        printf("\n");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值