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;
}
时间: 2024-10-25 15:18:43

ZOJ 3497 Mistwald的相关文章

ZOJ 3497 Mistwald(矩阵快速幂)

题目: 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 *

zoj 3497 Mistwald 矩阵快速幂

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 s

矩阵快速幂 ZOJ 3497 Mistwald

题目传送门 题意:看似给了一个迷宫,每个点能传送到4个地方,问在P时间能否到达终点 分析:其实是一个有向图,可以用邻接矩阵存图,连乘P次看是否能从1到n*m,和floyd的传递背包思想一样 #include <bits/stdc++.h> int tot; struct Mat { int m[30][30]; Mat() { memset (m, 0, sizeof (m)); } void init() { for (int i=1; i<=tot; ++i) { m[i][i] =

ZOJ 3497 Mistwald 矩阵

利用可达矩阵的幂来判断是否可达 #include <cstdio> #include <cstring> #include <algorithm> #include <queue> #include <stack> #include <map> #include <set> #include <climits> #include <iostream> #include <string>

概率dp ZOJ 3640

Help Me Escape Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice ZOJ 3640 Appoint description:  System Crawler  (2014-10-22) Description Background     If thou doest well, shalt thou not be accepted? an

zoj 2156 - Charlie&#39;s Change

题目:钱数拼凑,面值为1,5,10,25,求组成n面值的最大钱币数. 分析:dp,01背包.需要进行二进制拆分,否则TLE,利用数组记录每种硬币的个数,方便更新. 写了一个 多重背包的 O(NV)反而没有拆分快.囧,最后利用了状态压缩优化 90ms: 把 1 cents 的最后处理,其他都除以5,状态就少了5倍了. 说明:貌似我的比大黄的快.(2011-09-26 12:49). #include <stdio.h> #include <stdlib.h> #include <

ZOJ 1718 POJ 2031 Building a Space Station 修建空间站 最小生成树 Kruskal算法

题目链接:ZOJ 1718 POJ 2031 Building a Space Station 修建空间站 Building a Space Station Time Limit: 2 Seconds      Memory Limit: 65536 KB You are a member of the space station engineering team, and are assigned a task in the construction process of the statio

ZOJ 3607 Lazier Salesgirl (贪心)

Lazier Salesgirl Time Limit: 2 Seconds      Memory Limit: 65536 KB Kochiya Sanae is a lazy girl who makes and sells bread. She is an expert at bread making and selling. She can sell the i-th customer a piece of bread for price pi. But she is so lazy

ZOJ - 2243 - Binary Search Heap Construction

先上题目: Binary Search Heap Construction Time Limit: 5 Seconds      Memory Limit: 32768 KB Read the statement of problem G for the definitions concerning trees. In the following we define the basic terminology of heaps. A heap is a tree whose internal n