题目链接

E. Wilbur and Strings
time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Wilbur the pig now wants to play with strings. He has found an n by m table consisting only of the digits from 0 to 9 where the rows are numbered 1 to n and the columns are numbered 1 to m. Wilbur starts at some square and makes certain moves. If he is at square (x, y) and the digit d (0 ≤ d ≤ 9) is written at position (x, y), then he must move to the square (x + ad, y + bd), if that square lies within the table, and he stays in the square (x, y) otherwise. Before Wilbur makes a move, he can choose whether or not to write the digit written in this square on the white board. All digits written on the whiteboard form some string. Every time a new digit is written, it goes to the end of the current string.

Wilbur has q strings that he is worried about. For each string si, Wilbur wants to know whether there exists a starting position (x, y) so that by making finitely many moves, Wilbur can end up with the string si written on the white board.

Input

The first line of the input consists of three integers n, m, and q (1 ≤ n, m, q ≤ 200) — the dimensions of the table and the number of strings to process, respectively.

Each of the next n lines contains m digits from 0 and 9 giving the table itself.

Then follow 10 lines. The i-th of them contains the values ai - 1 and bi - 1 ( - 200 ≤ ai, bi ≤ 200), i.e. the vector that Wilbur uses to make a move from the square with a digit i - 1 in it.

There are q lines that follow. The i-th of them will contain a string si consisting only of digits from 0 to 9. It is guaranteed that the total length of these q strings won't exceed 1 000 000.

Output

For each of the q strings, print "YES" if Wilbur can choose x and y in order to finish with this string after some finite number of moves. If it's impossible, than print "NO" for the corresponding string.

Sample test(s)
Input
1 1 2
0
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
0000000000000
2413423432432

Output
YES
NO

Input
4 2 5
01
23
45
67
0 1
0 -1
0 1
0 -1
0 1
0 -1
0 1
0 -1
0 1
0 -1
0000000000
010101011101
32232232322
44343222342444324
6767

Output
YES
YES
YES
NO
YES

Note

In the first sample, there is a 1 by 1 table consisting of the only digit 0. The only move that can be made is staying on the square. The first string can be written on the white board by writing 0 repeatedly. The second string cannot be written as there is no 2 on the table.

题意:n*m的矩阵,每个格子内有一个(0-9)的数字。若当前在格子(i,j)那么下一步必须前往格子(i+a[i],j+b[j])(当且仅当该格子在矩阵内,否则任然在原地)。到了一个格子可以选择盖格子上的数字并将它加入已选数字的末尾,也可以不选。有q次询问,每次询问一个字符串,求解任选起点,在有限的步数内能否获得该字符串。

题解:根据题意建图。有一个特点,每一个格子出度最大为1。那么以任意一个点为起点一直走,那么路径一定唯一,末尾要么是个点,要么是个环。我们把环缩点,再反向建图,每次询问的一个字符串的时候,从入度为0的点开始,DFS遍历图,能匹配则匹配,贪心求解即可。由于图是个DAG,每个点的入度最大为1,那么每次询问,每个点最多只访问一次。复杂度O(q*n*m)

代码如下:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<math.h>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<list>
typedef __int64 LL;
typedef unsigned __int64 LLU;
const int nn=210;
const int inf=0x3fffffff;
const LL inf64=(LL)inf*inf;
using namespace std;
int n,m,q;
char stu[nn][nn];
int sz[nn*nn];
int tu[nn][nn];
int a[nn],b[nn];
char s[nn*10000];
struct node
{int en,next;
}E[nn*nn];
int p[nn*nn],num;
int rd[nn*nn];
void init()
{memset(rd,0,sizeof(rd));memset(p,-1,sizeof(p));num=0;
}
void add(int st,int en)
{rd[en]++;E[num].en=en;E[num].next=p[st];p[st]=num++;
}
bool check(int x,int y)
{return x>=1&&x<=n&&y>=1&&y<=m;
}
int ID(int x,int y)
{return (x-1)*m+y;
}
int dfn[nn*nn],low[nn*nn];
int cnt;
bool insta[nn*nn];
stack<int>sta;
bool use[nn*nn][15];
bool vis[nn*nn];
void dfs(int id)
{sta.push(id);insta[id]=true;dfn[id]=low[id]=++cnt;int i,w;for(i=p[id];i+1;i=E[i].next){w=E[i].en;if(dfn[w]==-1){dfs(w);low[id]=min(low[id],low[w]);}else if(insta[w]){low[id]=min(low[id],dfn[w]);}}if(dfn[id]==low[id]){while(1){int ix=sta.top();if(ix!=id){vis[ix]=false;rd[id]=0;}sta.pop();insta[ix]=false;use[id][sz[ix]]=true;if(ix==id)break;}}
}
void tarjan()
{memset(dfn,-1,sizeof(dfn));memset(insta,false,sizeof(insta));memset(use,false,sizeof(use));memset(vis,true,sizeof(vis));cnt=0;int i,j,w;for(i=1;i<=n*m;i++){if(dfn[i]==-1){dfs(i);}}
}
bool solve(int id,int ix)
{if(ix==-1)return true;int i,w;bool re=false;for(i=p[id];i+1;i=E[i].next){w=E[i].en;if(!vis[w])continue;if(use[w][s[ix]-'0']){re=re||solve(w,ix-1);}else{re=re||solve(w,ix);}}return re;
}
int main()
{int i,j;while(scanf("%d%d%d",&n,&m,&q)!=EOF){for(i=1;i<=n;i++){scanf("%s",stu[i]);for(j=0;j<m;j++){tu[i][j+1]=stu[i][j]-'0';sz[ID(i,j+1)]=tu[i][j+1];}}for(i=0;i<=9;i++){scanf("%d%d",&a[i],&b[i]);}init();for(i=1;i<=n;i++){for(j=1;j<=m;j++){if(check(i+a[tu[i][j]],j+b[tu[i][j]])){add(ID(i+a[tu[i][j]],j+b[tu[i][j]]),ID(i,j));}}}tarjan();while(q--){scanf("%s",s);int ls=strlen(s);for(i=1;i<=n*m;i++){if(rd[i]==0){int fuck=ls;while(fuck>=1){if(use[i][s[fuck-1]-'0']){fuck--;}elsebreak;}if(fuck==0){puts("YES");break;}if(solve(i,fuck-1)){puts("YES");break;}}}if(i>n*m){puts("NO");}}}return 0;
}

codeforces 596E Wilbur and Strings(DFS)相关推荐

  1. codeforces 596E Wilbur and Strings

    题意:一个矩阵上面有0~9的数字,可以从任意一个格子出发,每次根据格子上的数字会前进到另一个格子(或原地不动),现在给出q个数位串,问是否有走法可以取出这个串(走到格子上的时候可以不取). 思路:发现 ...

  2. CodeForces - 798B Mike and strings

    B. Mike and strings time limit per test2 seconds memory limit per test256 megabytes inputstandard in ...

  3. Codeforces 846 B Math Show DFS + 贪心

    题目链接: http://codeforces.com/contest/846/problem/B 题目描述: 有N个节点, 每个节点有相同的K个子节点, 每个子节点有时间花费,完成一个子节点获得1分 ...

  4. Codeforces 375D - Tree and Queries(dfs序+莫队)

    题目链接:http://codeforces.com/contest/351/problem/D 题目大意:n个数,col[i]对应第i个数的颜色,并给你他们之间的树形关系(以1为根),有m次询问,每 ...

  5. Robbers' watch【Codeforces 686 C】【DFS】

    Codeforces 686 C 题意还是比较的难懂的,得亏最后给理解了. 题意:我们给一天的时间做了新的定义,一天中,有N小时,每个小时呢,有M分钟,然后呢时针是按照7进制的,也就是7小时(分钟)其 ...

  6. CodeForces 396C 树状数组 + DFS

    本主题开始看到以为段树或树状数组,但是,对于一个节点的有疑问的所有子节点的加权,这一条件被视为树的根,像 然后1号是肯定在第一层中,然后建立一个单向侧倒查,然后记录下来 其中每个节点 层,终于 两个节 ...

  7. CodeForces - 817F Graph and String(dfs判二分图)

    题目链接:点击查看 题目大意:给出一张图,现在要求给出一种合适的染色方案,使得: 只能用 ' a ' , ' b ' , ' c ' 进行染色 有边相连的两个点的颜色要么相同,要么相邻,不能是 ' a ...

  8. CodeForces - 1325F Ehabs Last Theorem(dfs树找最大环)

    题目链接:点击查看 题目大意:给出一个由 n 个结点和 m 条边构成的无向图,再给出一个 k ,需要在图中完成下面任意一种操作: 找到一个大小恰好为   的独立集 找到一个大小至少为  的环 题目分析 ...

  9. CodeForces - 1316D Nash Matrix(构造+dfs)

    题目链接:点击查看 题目大意:给出一个 n * n 的矩阵,初始时每个格子都为空,现在要求我们自己用 ' R ' , ' L ' , ' U ' , ' D ' 和 ' X ' 填充,分别表示在每个格 ...

最新文章

  1. 用户列表-投资记录sql
  2. .Net Framework 4.5.1 安装时遇到严重错误 问题的解决方法
  3. 在杭州,我们逛了一家“数据博物馆”
  4. 宜昌远安谋定功能性-农业大健康·万祥军:绿色和谐新路
  5. .net框架、CLR和C#的版本之间的对应关系
  6. 微信小程序,实现 watch 属性,监听数据变化
  7. js读取本地excel文档数据
  8. clion windows安装
  9. Google的自定义搜索-GOOD!
  10. java版本号分段比较_java实现的版本号比较
  11. 2021-08-30缺失的第一个正数
  12. Raki的读paper小记:Empower Entity Set Expansion via Language Model Probing
  13. 电商项目整体介绍-尚硅谷大数据培训
  14. java怎么弹出页面_java怎么样实现弹出窗口
  15. 华为运营商级路由器配置示例 | EVdPdN VPLS over SRv6 BE(CE双归接入)
  16. 北京理工大学计算机科学与技术培养方案,王全玉_北京理工大学计算机学院
  17. 【英语】英语写作——三段式开头
  18. 计算机一级考试PPT部分几分,计算机一级考试PPT幻灯片和最后一题(就是自己输网址)各有多少分啊...
  19. 【实战笔记】Java 算法与数据结构-排序(选择、插入、冒泡、希尔、归并、快速、堆)
  20. 计算机网络(二) 虚拟网桥

热门文章

  1. C++核心准则边译边学-I.6 表达前提条件最好使用Expects()
  2. 超强!Figma汉化版下载教程来了!
  3. w ndows7怎样连接无线网,windows7电脑如何连接wifi
  4. 转载(中文、日文、韩文编码问题)
  5. 基于博客系统的访客日志记录
  6. 小程序之仿小米商城Lite
  7. Oracle查询数据表数据很少却很慢(查询空表很很耗时)
  8. 宝塔面板计划任务每日重启服务器
  9. 作为一个计算机专业的学生,除了教材,这些书籍你读过多少?
  10. 使用Photoshop给Premiere批量添加对白字幕听语音 |浏览:25974|更新:2013-12-23 23:18|标签:photoshop premiere 使用Photoshop给Pre