zoj3497(经典矩阵乘法)

原以为是用搜索做的题,想了好久都无法想到一个高效正确的解法。 

后面发现竟然这就是矩阵的应用! 碉堡! 

 

给定一个有向图,问从A点恰好走k步(允许重复经过边)到达B点的方案数mod p的值  ——选自matrix67
    把给定的图转为邻接矩阵,即A(i,j)=1当且仅当存在一条边i->j。令C=A*A,那么C(i,j)=ΣA(i,k)*A(k,j),实际上就等于从点i到点j恰好经过2条边的路径数(枚举k为中转点)。类似地,C*A的第i行第j列就表示从i到j经过3条边的路径数。同理,如果要求经过k步的路径数,我们只需要二分求出A^k即可。

K - Mistwald
Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu
Submit  Status  Practice  ZOJ 3497

Description

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

 
 
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
using namespace std;

int n,m;
bool g[33][33];
int x[10],y[10];
bool tg[33][33];

void mul(bool s[33][33],bool t[33][33])
{
    bool tmp[33][33];
    int top=n*m;
    memset(tmp,0,sizeof(tmp));
    for(int k=0;k<top;k++)
        for(int i=0;i<top;i++)
            for(int j=0;j<top;j++)
            {
                tmp[i][j]|=(s[i][k]&t[k][j]);
            }

    for(int i=0;i<top;i++)
        for(int j=0;j<top;j++)
            s[i][j]=tmp[i][j];

}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        memset(g,0,sizeof(g));

        scanf("%d%d",&n,&m);
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
            {
                int id=i*m+j;
                scanf(" ((%d,%d),(%d,%d),(%d,%d),(%d,%d))",&x[1],&y[1],&x[2],&y[2],&x[3],&y[3],&x[4],&y[4]);
                if(i!=n-1||j!=m-1)
                {
                    for(int k=1;k<=4;k++)
                    {
                        int tid=(x[k]-1)*m+y[k]-1;
                        g[id][tid]=1;
                    }
                }
            }
        int q;
        scanf("%d",&q);
        while(q--)
        {
            int tmp;
            scanf("%d",&tmp);
            bool sum[33][33];
            for(int i=0;i<n*m;i++)
                for(int j=0;j<n*m;j++)
                {
                    if(i==j) sum[i][j]=1;
                    else sum[i][j]=0;
                    tg[i][j]=g[i][j];
                }
            while(tmp)
            {
                if((tmp&1)) mul(sum,tg);
                mul(tg,tg);
                tmp>>=1;
            }
            if(sum[0][n*m-1]==0) printf("False\n");
            else
            {
                int flag=0;
                for(int i=1;i<n*m-1;i++)
                {
                    if(g[0][i]!=0)
                    {
                        flag=1;
                        break;
                    }
                }
                if(flag) printf("Maybe\n");
                else printf("True\n");
            }
        }
        printf("\n");
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
毕业设计,基于SpringBoot+Vue+MySQL开发的公寓报修管理系统,源码+数据库+毕业论文+视频演示 现代经济快节奏发展以及不断完善升级的信息化技术,让传统数据信息的管理升级为软件存储,归纳,集中处理数据信息的管理方式。本公寓报修管理系统就是在这样的大环境下诞生,其可以帮助管理者在短时间内处理完毕庞大的数据信息,使用这种软件工具可以帮助管理人员提高事务处理效率,达到事半功倍的效果。此公寓报修管理系统利用当下成熟完善的Spring Boot框架,使用跨平台的可开发大型商业网站的Java语言,以及最受欢迎的RDBMS应用软件之一的MySQL数据库进行程序开发。公寓报修管理系统有管理员,住户,维修人员。管理员可以管理住户信息和维修人员信息,可以审核维修人员的请假信息,住户可以申请维修,可以对维修结果评价,维修人员负责住户提交的维修信息,也可以请假。公寓报修管理系统的开发根据操作人员需要设计的界面简洁美观,在功能模块布局上跟同类型网站保持一致,程序在实现基本要求功能时,也为数据信息面临的安全问题提供了一些实用的解决方案。可以说该程序在帮助管理者高效率地处理工作事务的同时,也实现了数据信息的整体化,规范化与自动化。 关键词:公寓报修管理系统;Spring Boot框架;MySQL;自动化;VUE
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值