Codeforces Round #331 (Div. 2) E. Wilbur and Strings dfs乱搞
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 (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 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乱搞的更多相关文章
- 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 ...
- 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/ ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 水题 Codeforces Round #302 (Div. 2) A Set of Strings
题目传送门 /* 题意:一个字符串分割成k段,每段开头字母不相同 水题:记录每个字母出现的次数,每一次分割把首字母的次数降为0,最后一段直接全部输出 */ #include <cstdio> ...
随机推荐
- Android下高斯模糊的算法和demo
采用纯java和RenderScript两种方式来做高斯算法. 也可以用NDK来做,想试试的可以参考: http://stackoverflow.com/questions/2067955/fast- ...
- SQL优化34条
我们要做到不但会写SQL,还要做到写出性能优良的SQL,以下为笔者学习.摘录.并汇总部分资料与大家分享! (1) 选择最有效率的表名顺序(只在基于规则的优化器中有效): ORACLE 的解析 ...
- css3属性及事例
在看网上别的前端大牛的作品时,总会有新的收获,我想很多人应该都知道box-shadow,但是不知道有没有接触过这个 box-shadow: 2px 2px 4px rgba(0,0,0,0.4) , ...
- poj 1651 http://poj.org/problem?id=1651
http://poj.org/problem?id=1651Multiplication Puzzle Time Limit: 1000MS Memory Limit: 65536K To ...
- 个人经验 - Android的RelativeLayout布局的layout_height属性设置为wrap_content时的坑
Android的RelativeLayout布局的layout_height属性设置为wrap_content时的坑: 此坑出现的条件: 1.RelativeLayout布局的layout_heigh ...
- Watermarking 3D Polygonal Meshes in the Mesh Spectral Domain
这周看了一篇Ryutarou Ohbuchi网格水印的论文,论文中提出在网格的频率域中加入水印.对于网格而言,没有如图像中的DCT等转换到频率域的变换,因此用什么量来模拟传统频率域中的系数,是很关键的 ...
- dwr消息推送
闲来无事,把自己关于对dwr消息推送的实现过程描述一番. 首先第一步,当然在工程中是加入dwr.jar了,接着在web.xml中配置以下代码 <servlet> <servlet-n ...
- SQL入门
# SQL入门 数据库表 一个数据库(database)通常包含一个或多个表(table). 每一个表都有一个名字标识. 表单包含数据的记录(行). 一些重要的SQL命令(常用的吧) 命令 说明 ...
- the application could not be verified
在iphone上安装app时,提示the application could not be verified 解决方式: 将iphone已有的这个app卸载,然后安装就可以了.
- POJ3278http://poj.org/problem?id=3278
http://poj.org/problem?id=3278 题目大意: m,n两个数m可+1, -1, *2变成n,需要经过几步 #include<stdio.h> #include&l ...