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

思路:发现每个点的出度至多为1,那么可以预处理出走到这个格子后取某个数字最近的格子是哪个(f[i][j][0-9]),然后用队列存下开头,在做一遍贪心就可以了

#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
int cnt,sz=,f[*][],c[][],id[][],vis[][],n,m,a[],b[],ans[*],d[*];
int read(){
char ch=getchar();int f=,t=;
while (ch<''||ch>'') {if (ch=='-') f=-;ch=getchar();}
while (''<=ch&&ch<=''){t=t*+ch-'';ch=getchar();}
return t*f;
}
void dfs(int x,int y){
int dx=x+a[c[x][y]],dy=y+b[c[x][y]];
if (dx<||dx>n||dy<||dy>m) f[id[x][y]][c[x][y]]=id[x][y];
else{
f[id[x][y]][c[x][y]]=id[dx][dy];
if (!vis[dx][dy]) {vis[dx][dy]=;dfs(dx,dy);}
for (int i=;i<=;i++)
if (i!=c[x][y])
f[id[x][y]][i]=f[id[dx][dy]][i];
}
}
int main(){
n=read();m=read();int T=read();cnt=;
for (int i=;i<=n;i++){
char s[];
scanf("%s",s+);
for (int j=;j<=m;j++){
c[i][j]=s[j]-'';id[i][j]=++cnt;d[cnt]=c[i][j];
     }
}
   char s[1000005];
for (int i=;i<=;i++)
a[i]=read(),b[i]=read();
for (int i=;i<=n;i++)
for (int j=;j<=m;j++)
if (!vis[i][j]){
vis[i][j]=;
ans[++sz]=id[i][j];
dfs(i,j);
}while (T--){
scanf("%s",s+);bool flag=false;
for (int i=;i<=sz;i++){
int x=ans[i];
for (int j=;j<=strlen(s+);j++){
x=f[x][s[j]-''];if (x<=) break;
}
if (x>) {flag=true;break;}
}
if (flag) printf("YES\n");else printf("NO\n");
}
}

codeforces 596E Wilbur and Strings的更多相关文章

  1. 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 ...

  2. Codeforces 385B Bear and Strings

    题目链接:Codeforces 385B Bear and Strings 记录下每一个bear的起始位置和终止位置,然后扫一遍记录下来的结构体数组,过程中用一个变量记录上一个扫过的位置,用来去重. ...

  3. Codeforces 482C Game with Strings(dp+概率)

    题目链接:Codeforces 482C Game with Strings 题目大意:给定N个字符串,如今从中选定一个字符串为答案串,你不知道答案串是哪个.可是能够通过询问来确定, 每次询问一个位置 ...

  4. 【24.34%】【codeforces 560D】Equivalent Strings

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  5. CodeForces 682D Alyona and Strings (四维DP)

    Alyona and Strings 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/D Description After re ...

  6. Codeforces 596D Wilbur and Trees

    http://codeforces.com/contest/596/problem/D 题目大意: 有n棵树排成一排,高度都为h. 主人公要去砍树,每次等概率地随机选择没倒的树中最左边的树或者最右边的 ...

  7. codeforces 518A. Vitaly and Strings

    A. Vitaly and Strings time limit per test 1 second memory limit per test 256 megabytes input standar ...

  8. Codeforces 149 E. Martian Strings

    正反两遍扩展KMP,维护公共长度为L时.出如今最左边和最右边的位置. . .. 然后枚举推断... E. Martian Strings time limit per test 2 seconds m ...

  9. Codeforces 985 F - Isomorphic Strings

    F - Isomorphic Strings 思路:字符串hash 对于每一个字母单独hash 对于一段区间,求出每个字母的hash值,然后排序,如果能匹配上,就说明在这段区间存在字母间的一一映射 代 ...

随机推荐

  1. jQuery总结或者锋利的jQuery笔记一

      在线测试脚本网站 层次 选择器要多花时间看看. 第一章: hover = enter+leave jQuery对象 jQuery产生的对象时jQuery独有的,只能自己调用 var $c=$(&q ...

  2. JAVA多线程之线程的挂起与恢复(suspend方法与resume方法)

    一,介绍 本文讨论JAVA多线程中,使用 thread.suspend()方法暂停线程,使用 thread.resume()恢复暂停的线程 的特点. 先介绍二个关于线程的基本知识: ①线程的执行体是r ...

  3. JavaScript模拟QQ签名(HTML5 contenteditable属性)

    例图: 一.思路 1.单击元素时,使元素可以编辑,并获得焦点 2.按下键盘检测用户编辑元素中的文本 3.监听按下Enter键操作或离开可编辑元素焦点时,更新数据库 二.代码 $(function(){ ...

  4. 【转】用CornerStone配置SVN,HTTP及svn简单使用说明

    已经安装了的小伙伴请直接看三步骤 一.下载地址 CornerStoneV2.6:http://pan.baidu.com/s/1qWEsEbM密码:www.macx.cn 二.安装破解方法 1.安装之 ...

  5. LeetCode-Valid Number - 有限状态机

    判断合法数字,之前好像在哪里看到过这题, 记得当时还写了好久,反正各种改, 今天看到了大神的解法(https://github.com/fuwutu/LeetCode/blob/master/Vali ...

  6. Quartus II 安装教程—FPGA入门教程【钛白Logic】

    Quartus II 工具安装一般分为两个部分,首先是开发工具本身的安装,其次就是器件库的安装,我们可以根据我们的需要选择相应的器件库来安装,这里我们使用Cyclone IV的FPGA,即安装Cycl ...

  7. kali的中文输入法-安装后无中文选项的解决办法

    我在我实体电脑上安装了kali 本想安装个中文输入法但是有时候输入发中根本没有选择输入法的这一项.所以导致很别扭,之前在虚机上安装过没有问题但是挪到实体机上就是没有用的也是fcitx,这一度让我这个小 ...

  8. BAT修改文本内容

    @echo off (for /f "delims=" %%a in (文件名) do ( set "str=%%a" setlocal enabledelay ...

  9. 关于USBHID协议以及鼠标键盘描述符的解释【转】

    转自:https://blog.csdn.net/jiujiujiuqiuqiuqiu/article/details/47277685 一.HID设备识别 前面有提到关于SCSI协议的USB设备实现 ...

  10. C语言清空输入缓冲区的N种方法对比【转】

    转自:http://www.cnblogs.com/codingmylife/archive/2010/04/18/1714954.html C语言中有几个基本输入函数: //获取字符系列 int f ...