矩阵快速幂。

邻接矩阵的$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 = ;
while(!isdigit(c)) c = getchar();
while(isdigit(c))
{
x = x * + c - '';
c = getchar();
}
} int T;
int n,m,a,b,c,d,e,f,g,h;
char s[];
int r[][];
long long p; struct Matrix
{
int A[][];
int R, C;
Matrix operator*(Matrix b);
}; Matrix X, Y, Z; Matrix Matrix::operator*(Matrix b)
{
Matrix c;
memset(c.A, , sizeof(c.A));
int i, j, k;
for (i = ; i <= R; i++)
for (j = ; j <= b.C; j++)
for (k = ; k <= C; k++)
if(c.A[i][j]==) 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, , sizeof X.A);
memset(Y.A, , sizeof Y.A);
memset(Z.A, , sizeof Z.A); for(int i=;i<=n*m;i++) Y.A[i][i]=;
Y.R=Y.C=n*m; for(int i=;i<=n*m;i++)
for(int j=;j<=n*m;j++) X.A[i][j]=r[i][j];
X.R=X.C=n*m;
} void work()
{
while (p)
{
if (p % == ) Y = Y*X;
p = p >> ;
X = X*X;
}
} int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m); memset(r,,sizeof r);
for(int i=;i<=n;i++)
{
for(int j=;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-)*m+j][(a-)*m+b]=;
r[(i-)*m+j][(c-)*m+d]=;
r[(i-)*m+j][(e-)*m+f]=;
r[(i-)*m+j][(g-)*m+h]=;
}
} int Q; scanf("%d",&Q);
while(Q--)
{
scanf("%lld",&p);
init();
work(); if(Y.A[][n*m]==) printf("False\n");
else
{
bool Find=;
for(int i=;i<=n*m-;i++) if(Y.A[][i]==) Find=;
if(Find==) printf("True\n");
else printf("Maybe\n");
}
}
printf("\n");
}
return ;
}

ZOJ 3497 Mistwald的更多相关文章

  1. 矩阵快速幂 ZOJ 3497 Mistwald

    题目传送门 题意:看似给了一个迷宫,每个点能传送到4个地方,问在P时间能否到达终点 分析:其实是一个有向图,可以用邻接矩阵存图,连乘P次看是否能从1到n*m,和floyd的传递背包思想一样 #incl ...

  2. Mistwald zoj 3497

    链接 [https://vjudge.net/contest/294259#problem/K] 题意 就是有个m*n矩阵 出发(1,1) 出口(m,n) 然后给出每个点能到大的四个位置 而且一旦到达 ...

  3. zoj3497(经典矩阵乘法)

    原以为是用搜索做的题,想了好久都无法想到一个高效正确的解法. 后面发现竟然这就是矩阵的应用! 碉堡! 给定一个有向图,问从A点恰好走k步(允许重复经过边)到达B点的方案数mod p的值  ——选自ma ...

  4. ZOJ People Counting

    第十三届浙江省大学生程序设计竞赛 I 题, 一道模拟题. ZOJ  3944http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=394 ...

  5. ZOJ 3686 A Simple Tree Problem

    A Simple Tree Problem Time Limit: 3 Seconds      Memory Limit: 65536 KB Given a rooted tree, each no ...

  6. ZOJ Problem Set - 1394 Polar Explorer

    这道题目还是简单的,但是自己WA了好几次,总结下: 1.对输入的总结,加上上次ZOJ Problem Set - 1334 Basically Speaking ac代码及总结这道题目的总结 题目要求 ...

  7. ZOJ Problem Set - 1392 The Hardest Problem Ever

    放了一个长长的暑假,可能是这辈子最后一个这么长的暑假了吧,呵呵...今天来实验室了,先找了zoj上面简单的题目练练手直接贴代码了,不解释,就是一道简单的密文转换问题: #include <std ...

  8. ZOJ Problem Set - 1049 I Think I Need a Houseboat

    这道题目说白了是一道平面几何的数学问题,重在理解题目的意思: 题目说,弗雷德想买地盖房养老,但是土地每年会被密西西比河淹掉一部分,而且经调查是以半圆形的方式淹没的,每年淹没50平方英里,以初始水岸线为 ...

  9. ZOJ Problem Set - 1006 Do the Untwist

    今天在ZOJ上做了道很简单的题目是关于加密解密问题的,此题的关键点就在于求余的逆运算: 比如假设都是正整数 A=(B-C)%D 则 B - C = D*n + A 其中 A < D 移项 B = ...

随机推荐

  1. [LeetCode] Largest Rectangle in Histogram O(n) 解法详析, Maximal Rectangle

    Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...

  2. 子字符串substring 问题 - KMP 字符串匹配算法备忘录

    本文为自己对KMP的理解. 对KMP很好的介绍可以参考 http://www.cnblogs.com/yjiyjige/p/3263858.html 本文为对这篇文章的提炼和补充. KMP算法基本思想 ...

  3. 上下文路径request.getContextPath();与${pageContext.request.contextPath}

    (1) request.getContextPath();与${pageContext.request.contextPath}都是获取上下文路径: 1. request.getContextPath ...

  4. 【设计模式】 模式PK:命令模式VS策略模式

    1.概述 命令模式和策略模式的类图确实很相似,只是命令模式多了一个接收者(Receiver)角色.它们虽然同为行为类模式,但是两者的区别还是很明显的.策略模式的意图是封装算法,它认为“算法”已经是一个 ...

  5. HDU5852 Intersection is not allowed!

    There are K pieces on the chessboard. The size of the chessboard is N*N. The pieces are initially pl ...

  6. 20155335俞昆《java程序设计》第三周总结

    20155335  2006-2007-2  <Java程序设计>第三周学习总结 ##  教材学习内容总结 首先,关键是区基本类型和类类型,,产生对象必须定义类,类是一个概念,并不存在,对 ...

  7. HDU 1312 Red and Black (深搜)

    题目链接 Problem Description There is a rectangular room, covered with square tiles. Each tile is colore ...

  8. bzoj 2669 状压DP

    因为最多有8个'X',所以我们可以用w[i][s]来表示现在我们填了前i个数,填的X的为S,因为每次新加进来的数都不影响前面的最小值,所以我们可以随便添加,这样就有了剩下所有位置的方案,每次都这样转移 ...

  9. apache log 按日期记录 格式 <GOOD>-- (转)

    在apache的配置文件中找到ErrorLog logs/error_logCustomLog logs/access_log common Linux系统配置方法: 将其改为ErrorLog “| ...

  10. ribbon使用eureka的meta进行动态路由

    序 使用eureka的元数据信息,再配上ribbon的路由功能,就可以在api-gateway实现很多功能,比如灰度测试.生产调试等等.下面介绍一下,怎么使用jmnarloch大神提供的ribbon- ...