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. 利用反射把DataTable自动赋值到Model实体(自动识别数据类型)

    转:http://www.cnblogs.com/the7stroke/archive/2012/04/22/2465591.html using System.Collections.Generic ...

  2. JavaScript遍历方式详解

    为了方便例子讲解,现有数组和json对象如下: var demoArr = ['Javascript', 'Gulp', 'CSS3', 'Grunt', 'jQuery', 'angular']; ...

  3. kendoui-grid篇

    kendo确实是个好东西,能够让我们专注于后端开发,无需在效果呈现上花大力气,唯一的缺点,它是收费的,但是我目前还没发现为嘛要掏钱,因为free的也满足了我的需求. kendoUI For asp.m ...

  4. Intellij IDEA Maven创建web项目

    Intellij IDEA在创建java webapp的时候没有那么人性化,新手使用会处处碰壁.特此记录! 一.File--New--project 二.Next--输入GroupId.Artifac ...

  5. Windows Azure中的配置管理

    最近一直在做项目迁移的工作,由传统的ASP.NET转到Windows Azure,这里介绍一下Azure的配置管理.在传统的WinForm或ASP.NET项目下,配置文件为web.config(app ...

  6. 《Java数据结构与算法》笔记-CH2有序数组

    /** * 上个例子是无序数组,并且没有考虑重复元素的情况. * 下面来设计一个有序数组,我们设定不允许重复,这样提高查找的速度,但是降低了插入操作的速度. * 1.线性查找 * 2.二分查找 * 有 ...

  7. 《Java数据结构与算法》笔记-CH2无序数组

    /** * 本章目标: * 1.自制数组类 * 2.有序数组:按关键字升降序排列:二分法查找 * 3.分析有序数组.大O表示法 */ /** * 自制数组类 书中有的地方有错误,本程序以修改 */ c ...

  8. mysql的登录密码带特殊符号登录不进去的问题

    eg : mysqldump -u root -p)P:9 ${dbname} > $dataPath$filename 当我将数据库的数据每天进行自动导出时,需要带上密码,但 ) 是一个特殊符 ...

  9. android中向bitmap里写入文字

    public static Bitmap drawText2Bitmap(String text, int imgResourceId, Context mContext) { try { Resou ...

  10. Nginx的配置文件(nginx.conf)解析和领读官网

    步骤一:vi nginx.conf配置文件,参考本博文的最下面总结,自行去设置 最后nginx.conf内容为 步骤二:每次修改了nginx.conf配置文件后,都要reload下. index.ht ...