E. Wilbur and Strings

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://codeforces.com/contest/596/problem/E

Description

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 (xy) and the digit d (0 ≤ d ≤ 9) is written at position (xy), then he must move to the square (x + ady + bd), if that square lies within the table, and he stays in the square (xy) 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 (xy) 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 nm, 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 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

Sample Output

YES
NO

HINT

题意

给你n*m的矩阵,每个矩阵里面都是0-9的数字,假设你踩到了(x,y),那么下一步就会在(x+ad,y+bd) ad和bd会给你 d是(x,y)的数值

然后你从不同的起点出发,会得到不同的字符串

然后有Q次询问,给你一个字符串,问你这个字符串是不是可能是之前的走出的字符串的子序列

题解:

暴力DP,dp[x][y][t],表示你在(x,y),你想得到字符t,你最近的一步是在哪儿

这个dp由dfs可以很容易得到

然后询问的时候,就直接暴力就好了

代码

#include<iostream>
#include<cstring>
#include<vector>
#include<stdio.h>
using namespace std; int n,m,q;
char s2[];
char s[][];
int vis[][];
int dp[*][];
int dx[];
int dy[];
vector<int> Q;
int id(int x,int y)
{
return x*m+y;
}
void dfs(int x,int y)
{
vis[x][y]=;
int t = (s[x][y]-''),p = id(x,y);
int xx = x+dx[t],yy = y+dy[t];
if(xx<||xx>=n||yy<||yy>=m)
dp[p][t]=p;
else
{
int v = id(xx,yy);
dp[p][t]=id(xx,yy);
if(vis[xx][yy]==)dfs(xx,yy);
for(int i=;i<;i++)
if(i!=t)
dp[p][i]=dp[v][i];
}
} int check(char s2[])
{
int len = strlen(s2);
for(int i=;i<Q.size();i++)
{
int x = Q[i];
for(int j=;j<len;j++)
{
x = dp[x][s2[j]-''];
if(x<)break;
}
if(x>=)return ;
}
return ;
} int main()
{
memset(dp,-,sizeof(dp));
scanf("%d%d%d",&n,&m,&q);
for(int i=;i<n;i++)
scanf("%s",s[i]);
for(int i=;i<;i++)
scanf("%d%d",&dx[i],&dy[i]);
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
if(!vis[i][j])
{
dfs(i,j);
Q.push_back(id(i,j));
}
}
}
for(int i=;i<q;i++)
{
scanf("%s",s2);
if(check(s2))printf("YES\n");
else puts("NO");
}
}

Codeforces Round #331 (Div. 2) E. Wilbur and Strings dfs乱搞的更多相关文章

  1. Codeforces Round #331 (Div. 2) D. Wilbur and Trees 记忆化搜索

    D. Wilbur and Trees Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/596/p ...

  2. Codeforces Round #331 (Div. 2)C. Wilbur and Points 贪心

    C. Wilbur and Points Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/596/ ...

  3. Codeforces Round #331 (Div. 2) B. Wilbur and Array 水题

    B. Wilbur and Array Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/596/p ...

  4. Codeforces Round #331 (Div. 2) A. Wilbur and Swimming Pool 水题

    A. Wilbur and Swimming Pool Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/conte ...

  5. Codeforces Round #331 (Div. 2) C. Wilbur and Points

    C. Wilbur and Points time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  6. Codeforces Round #331 (Div. 2) _A. Wilbur and Swimming Pool

    A. Wilbur and Swimming Pool time limit per test 1 second memory limit per test 256 megabytes input s ...

  7. Codeforces Round #331 (Div. 2) B. Wilbur and Array

    B. Wilbur and Array time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  8. Codeforces Round #Pi (Div. 2) D. One-Dimensional Battle Ships set乱搞

    D. One-Dimensional Battle ShipsTime Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/con ...

  9. 水题 Codeforces Round #302 (Div. 2) A Set of Strings

    题目传送门 /* 题意:一个字符串分割成k段,每段开头字母不相同 水题:记录每个字母出现的次数,每一次分割把首字母的次数降为0,最后一段直接全部输出 */ #include <cstdio> ...

随机推荐

  1. 转载RabbitMQ入门(6)--远程调用

    远程过程调用(RPC) (使用Java客户端) 在指南的第二部分,我们学习了如何使用工作队列将耗时的任务分布到多个工作者中. 但是假如我们需要调用远端计算机的函数,等待结果呢?好吧,这又是另一个故事了 ...

  2. 【JSP】弹出带输入框可 确认密码 对话框

    <body> <input type="submit" value="删除历史全部订单" onclick="deleteall()& ...

  3. 【转】使用NetBeans和Eclipse开发PHP应用程序

    [51CTO独家特稿]各位用户如果单独看NetBeans和Eclipse的市场占有率,你可能会认为使用其中任何一种IDE开发PHP应用程序都没有 问题,例如: 1.NetBeans:一款开源的集成开发 ...

  4. 【转】如何用WINDBG分析64位机上32位程序的DUMP文件

    将dump拖入到windbg中后,在command输入栏输入 .load wow64exts 回车 !sw 回车,就将windbg的dump,从64位模式切换到了32位模式,否则看到的call sta ...

  5. poj 4052(ac自动机)

    题意:自己百度吧!! 分析:就是通过它的fail指针来找出它的子串就行了,这题其实不难的.这好像还是金华邀请赛的题哦! 代码实现: #include<cstdio> #include< ...

  6. 022 UFT虚拟对象

    虚拟对象: 程序中那些行为标准类型对象的对象,但不能被QTP识别,则可把这些对象类型称为虚拟对象.并且映射到某类标准对象,例如button,check box等,QTP在测试过程中就会对这些虚拟对象模 ...

  7. Selenium IDE测试创建

    Selenium IDE 测试创建 涉及使用IDE Selenium创建测试,如下面的步骤 记录和测试添加命令 保存测试记录 保存测试程序 执行测试记录 在测试中记录和添加命令 为了演示目的,我们将利 ...

  8. C++ 编程输入输出语句

    C++ 的标准输入.输出就是我们已经使用的包含头文件iostream,他不但提供了I/O的库函数,也提供了使用该库的流模式,从cin>> 流入  和cout<<流出到设备就是一 ...

  9. JQuery插件之图片轮播插件–slideBox

    来源:http://www.ido321.com/852.html 今天偶然发现了一个比较好用的图片轮播插件—slideBox 先看看效果:http://slidebox.sinaapp.com/ 代 ...

  10. 我的web前端修炼之路从此开始

    看过一篇文章,上面说过要想学习一门新技术,从什么时候开始都是不晚的.但对于一名大四的学生,只会一点简单的网页架构,只懂得HTML,CSS,JavaScript简单的一点皮毛,却怎么也说不过去.但也是这 ...