ZOJ 3497 Mistwald 矩阵

利用可达矩阵的幂来判断是否可达

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <climits>
#include <iostream>
#include <string>

using namespace std;

#define MP make_pair
#define PB push_back
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
const int INF = INT_MAX / 3;
const double eps = 1e-8;
const LL LINF = 1e17;
const LL MOD = LINF;
const double DINF = 1e60;
const int maxn = 30;

struct Matrix {
    int n, m;
    LL data[maxn][maxn];
    Matrix(int n = 0, int m = 0): n(n), m(m) {
        memset(data, 0, sizeof(data));
    }

    void print() {
        for(int i = 1; i <= n; i++) {
            for(int j = 1; j <= m; j++) {
                cout << data[i][j] << " ";
            }
            cout << endl;
        }
    }
};

Matrix operator * (Matrix a, Matrix b) {
    Matrix ret(a.n, b.m);
    for(int i = 1; i <= a.n; i++) {
        for(int j = 1; j <= b.m; j++) {
            for(int k = 1; k <= a.m; k++) {
                ret.data[i][j] += a.data[i][k] * b.data[k][j];
                ret.data[i][j] %= MOD;
            }
        }
    }
    return ret;
}

Matrix operator + (Matrix a, Matrix b) {
    for(int i = 1; i <= a.n; i++) {
        for(int j = 1; j <= a.m; j++) {
            a.data[i][j] += b.data[i][j];
            a.data[i][j] %= MOD;
        }
    }
    return a;
}

Matrix operator * (int p, Matrix mat) {
    for(int i = 1; i <= mat.n; i++) {
        for(int j = 1; j <= mat.m; i++) {
            mat.data[i][j] *= p;
            mat.data[i][j] %= MOD;
        }
    }
}

Matrix pow(Matrix mat, LL p) {
    if(p == 0) {
        Matrix ret(mat.n, mat.m);
        for(int i = 1; i <= mat.n; i++) ret.data[i][i] = 1;
        return ret;
    }
    if(p == 1) return mat;
    Matrix ret = pow(mat * mat, p / 2);
    if(p & 1) ret = ret * mat;
    return ret;
}

int n, m;

int main() {
    int T; scanf("%d", &T);
    while(T--) {
        scanf("%d%d", &n, &m);
        Matrix mat(n * m, n * m);
        for(int i = 1; i <= n; i++) {
            for(int j = 1; j <= m; j++) {
                int nowx = i - 1, nowy = j - 1, nx, ny;
                char tmp; scanf(" %c", &tmp);
                for(int k = 0; k < 4; k++) {
                    if(k) scanf(" %c", &tmp);
                    scanf("(%d,%d)", &nx, &ny);
                    nx--; ny--;
                    if(i == n && j == m) continue;
                    mat.data[nowx * m + nowy + 1][nx * m + ny + 1] = 1;
                }
                scanf(" %c", &tmp);
            }
        }
        int Q; scanf("%d", &Q);
        while(Q--) {
            int K; scanf("%d", &K);
            Matrix ret = pow(mat, K);
            if(ret.data[1][n * m] == 0) puts("False");
            else {
                int cnt = 0;
                for(int i = 1; i < n * m; i++) {
                    if(ret.data[1][i] != 0) cnt++;
                }
                if(cnt == 0) puts("True");
                else puts("Maybe");
            }
        }
        puts("");
    }
    return 0;
}

  

时间: 2024-10-26 10:09:08

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 * N s

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

题目传送门 题意:看似给了一个迷宫,每个点能传送到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

矩阵快速幂. 邻接矩阵的$P$次方就是走$P$步之后的方案数,这里只记录能否走到就可以了.然后再判断一下三种情况即可. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<vector> #include<

ZOJ 2671 Cryptography 矩阵乘法+线段树

B - Cryptography Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice ZOJ 2671 Description Young cryptoanalyst Georgie is planning to break the new cipher invented by his friend Andie. To do this, he must

zoj - 3538(矩阵乘法)

题目链接:here—————— 题意:有四个人 A,B,C,D 每天出一套卷子,相邻的两天不能由同一个人出题 给你两个数n,m分别表示n天和m个操作(把第ai天定为有bi出题) 问有多少种方式?? 题解:  先排序 if  bi == bi-1 && ai - ai-1 = 1     return 0;  if       bi == bi-1  设f1 = 3:fn = 3^n - fn-1; else    f1 = 2;fn = 3^n - fn-1; 再判断两头 矩阵    -1

zoj 2853 Evolution 矩阵快速幂

在我的电脑上code::blocks运行过不了,但代码是可以AC的,很是郁闷. 问了大神,知道了函数的参数是放在栈区,结构体太大的话,栈就爆了,如是后来就听从大神的意见用引用改写了. 传送门:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1853 #include <stdio.h> #include <string.h> #include <stdlib.h> int Len_Matrix; in

ZOJ 3690 Choosing number(矩阵快速幂)

题目地址:ZOJ 3690 假设F(n)表示前n个人第n个人选择的数大于k的个数,G(n)表示的是前n个人第n个人选择的数小于等于k的个数 那么F(n) = F(n-1)*(m-k)+G(n-1)*(m-k) , G(n) = F(n-1)*k+G(n-1)*(k-1) , 那么最后的结果就是F(n)+G(n); 那么我们可以构造出矩阵 | m-k m-k|   | F(n-1) |       | F(n) | | k      k-1| * | G(n-1) | => | G(n) | 那么

zoj 2974 Just Pour the Water矩阵快速幂

Just Pour the Water Time Limit: 2 Seconds      Memory Limit: 65536 KB Shirly is a very clever girl. Now she has two containers (A and B), each with some water. Every minute, she pours half of the water in A into B, and simultaneous pours half of the