BaoBao has just found a grid with $n$ rows and $m$ columns in his left pocket, where the cell in the $j$-th column of the $i$-th row (indicated by $(i, j)$) contains an arrow (pointing either upwards, downwards, leftwards or rightwards) and an integer $a_{i, j}$.

BaoBao decides to play a game with the grid. He will first select a cell as the initial cell and tick it. After ticking a cell (let's say BaoBao has just ticked cell $(i, j)$), BaoBao will go on ticking another cell according to the arrow and the integer in cell $(i, j)$.

  • If the arrow in cell $(i, j)$ points upwards, BaoBao will go on ticking cell $(i-a_{i, j}, j)$ if it exists.
  • If the arrow in cell $(i, j)$ points downwards, BaoBao will go on ticking cell $(i+a_{i, j}, j)$ if it exists.
  • If the arrow in cell $(i, j)$ points leftwards, BaoBao will go on ticking cell $(i, j-a_{i, j})$ if it exists.
  • If the arrow in cell $(i, j)$ points rightwards, BaoBao will go on ticking cell $(i, j+a_{i, j})$ if it exists.

If the cell BaoBao decides to tick does not exist, or if the cell is already ticked, the game ends.

BaoBao is wondering if he can select a proper initial cell, so that he can tick every cell in the grid exactly once before the game ends. Please help him find the answer.

There are multiple test cases. The first line contains an integer $T$, indicating the number of test cases. For each test case:

The first line contains two integers $n$ and $m$ ($1 \le n \times m \le 10^5$), indicating the number of rows and columns of the grid.

For the following $n$ lines, the $i$-th line contains a string $s_i$ consisting of lowercased English letters ($|s_i| = m$, $s_{i, j} \in \{\text{'u' (ascii: 117)}, \text{'d' (ascii: 100)}, \text{'l' (ascii: 108)}, \text{'r' (ascii: 114)}\}$), where $s_{i, j}$ indicates the direction of arrow in cell $(i, j)$.

  • If $s_{i, j} = \text{'u'}$, the arrow in cell $(i, j)$ points upwards.
  • If $s_{i, j} = \text{'d'}$, the arrow in cell $(i, j)$ points downwards.
  • If $s_{i, j} = \text{'l'}$, the arrow in cell $(i, j)$ points leftwards.
  • If $s_{i, j} = \text{'r'}$, the arrow in cell $(i, j)$ points rightwards.

For the following $n$ lines, the $i$-th line contains $m$ integers $a_{i, 1}, a_{i, 2}, \dots, a_{i, m}$ ($1 \le a_{i, j} \le \max(n, m)$), where $a_{i, j}$ indicates the integer in cell $(i, j)$.

It's guaranteed that the sum of $n \times m$ of all test cases does not exceed $10^6$.

For each test case output one line. If BaoBao can find a proper initial cell, print "Yes" (without quotes), otherwise print "No" (without quotes).


题目概要:给定一个地图,每个地图的点给定下一步的方向和步长,问能否寻找到一点,可以遍历整个地图

为了进行操作,我们先将每个点的入度进行统计,先从0入度的点进行一次bfs(因为dfs好写,先写了dfs,看来数据不是很严格),看是否所有点都访问过了,如果有没有访问过的,说明不能遍历,特别的,如果没有0入度的点,说明任一点都可以通达,我们既可以随便dfs,也可以直接判正确

以下代码:

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <queue>
  4.  
  5. ;
  6. char str[MAXN];
  7. int dig[MAXN];
  8. int vis[MAXN];
  9. int ind[MAXN];
  10. int n, m;
  11.  
  12. void dfs(int x, int y) {
  13. //printf("%d %d\n",x,y);
  14. && y >= && y <= m && vis[m * (x - ) + y] == false) {
  15. vis[m * (x - ) + y] = true;
  16. ) + y];
  17. ) + y] == 'u') dfs(x - step, y);
  18. ) + y] == 'd') dfs(x + step, y);
  19. ) + y] == 'l') dfs(x, y - step);
  20. ) + y] == 'r') dfs(x, y + step);
  21. }
  22. }
  23.  
  24. && y <= m && y >= )ind[m * (x - ) + y]++;}
  25.  
  26. void check(int x, int y) {
  27. ) + y];
  28. ) + y] == 'u') mflag(x - step, y);
  29. ) + y] == 'd') mflag(x + step, y);
  30. ) + y] == 'l') mflag(x, y - step);
  31. ) + y] == 'r') mflag(x, y + step);
  32. }
  33.  
  34. int main() {
  35. int t;
  36. scanf("%d", &t);
  37. while (t--) {
  38. scanf("%d%d", &n, &m);
  39. ;i<=n*m;i++) vis[i]=;
  40. ;i<=n;i++) scanf()+m+);
  41. ; i <= n; i++)
  42. ; j <= m; j++)
  43. scanf() + j]),check(i, j);
  44. ,startj=;
  45. ; i <= n; i++) {
  46. bool tr = false;
  47. ; j <= m; j++) {
  48. ) * m + j] == ) {
  49. starti=i,startj=j;
  50. tr = true;
  51. break;
  52. }
  53. }
  54. if (tr) break;
  55. }
  56. dfs(starti,startj);
  57. bool flag = true;
  58. ;i<=n*m;i++)
  59. if(!vis[i]) flag=false;
  60. if (flag) printf("Yes\n");
  61. else printf("No\n");
  62. }
  63. ;
  64. }

B.Grid with Arrows-The 2019 ICPC China Shaanxi Provincial Programming Contest的更多相关文章

  1. C.0689-The 2019 ICPC China Shaanxi Provincial Programming Contest

    We call a string as a 0689-string if this string only consists of digits '0', '6', '8' and '9'. Give ...

  2. 计蒜客 39272.Tree-树链剖分(点权)+带修改区间异或和 (The 2019 ACM-ICPC China Shannxi Provincial Programming Contest E.) 2019ICPC西安邀请赛现场赛重现赛

    Tree Ming and Hong are playing a simple game called nim game. They have nn piles of stones numbered  ...

  3. 计蒜客 39280.Travel-二分+最短路dijkstra-二分过程中保存结果,因为二分完最后的不一定是结果 (The 2019 ACM-ICPC China Shannxi Provincial Programming Contest M.) 2019ICPC西安邀请赛现场赛重现赛

    Travel There are nn planets in the MOT galaxy, and each planet has a unique number from 1 \sim n1∼n. ...

  4. 计蒜客 39279.Swap-打表找规律 (The 2019 ACM-ICPC China Shannxi Provincial Programming Contest L.) 2019ICPC西安邀请赛现场赛重现赛

    Swap There is a sequence of numbers of length nn, and each number in the sequence is different. Ther ...

  5. 计蒜客 39270.Angel's Journey-简单的计算几何 ((The 2019 ACM-ICPC China Shannxi Provincial Programming Contest C.) 2019ICPC西安邀请赛现场赛重现赛

    Angel's Journey “Miyane!” This day Hana asks Miyako for help again. Hana plays the part of angel on ...

  6. 计蒜客 39268.Tasks-签到 (The 2019 ACM-ICPC China Shannxi Provincial Programming Contest A.) 2019ICPC西安邀请赛现场赛重现赛

    Tasks It's too late now, but you still have too much work to do. There are nn tasks on your list. Th ...

  7. The 2019 ACM-ICPC China Shannxi Provincial Programming Contest (西安邀请赛重现) J. And And And

    链接:https://nanti.jisuanke.com/t/39277 思路: 一开始看着很像树分治,就用树分治写了下,发现因为异或操作的特殊性,我们是可以优化树分治中的容斥操作的,不合理的情况只 ...

  8. The 2018 ACM-ICPC China JiangSu Provincial Programming Contest快速幂取模及求逆元

    题目来源 The 2018 ACM-ICPC China JiangSu Provincial Programming Contest 35.4% 1000ms 65536K Persona5 Per ...

  9. The 2018 ACM-ICPC China JiangSu Provincial Programming Contest J. Set

    Let's consider some math problems. JSZKC has a set A=A={1,2,...,N}. He defines a subset of A as 'Meo ...

随机推荐

  1. popupTheme和theme

    popupTheme是指toolBar中弹出的menu的Theme. 那么,如果想让ToolBar的文字是白色,如果你设置Toolbar的Theme是 "ThemeOverlay.AppCo ...

  2. ffmpeg命令选项解释

    ffmpeg作为媒体文件处理软件,基本用法如下: ffmpeg -i INPUTfile [OPTIONS] OUTPUTfile 输入输出文件通常就是待处理的多媒体文件了.可以是纯粹的音频文件,纯粹 ...

  3. React 版 V2EX 社区( react & react-router & axios & antd ui)

    目录 项目简介 在线演示 截图演示 踩坑 项目简介(1/4) Github: https://github.com/bergwhite/v2ex-react 项目使用React.Reac-router ...

  4. Oracle12c多租户如何启动关闭CDB或PDB (PDB自动启动)

    Oracle 数据库 12 c 中介绍了多租户选项允许单个容器数据库 (CDB) 来承载多个单独的可插拔数据库 (PDB).下面我们一起来启动和关闭容器数据库 (CDB) 和可插拔数据库 (PDB). ...

  5. 配置IIS服务:无法找到该页 您正在搜索的页面可能已经删除、更名或暂时不可用。

    1.配置IIS服务器时,在默认网站创建虚拟目录XXX.然后右击启动页面.aspx,“浏览” 2.  出现错误: 无法找到该页 您正在搜索的页面可能已经删除.更名或暂时不可用. ------------ ...

  6. AjaxMethod的使用

        AjaxMethod的使用 使用AjaxMethod要满足一下几点: 1.如果还没有ajax.dll文件,就先下载一个来 2.将ajax.dll添加到项目引用中:在VS的解决方案资源管理器中右 ...

  7. cisco 2901 配置拨号上网

    1.输入en,然后输入密码确认后按conf t2.Router(config)# vpdn enable        interface dialer 1   // 进入拨号器13.Router(c ...

  8. Windchill 基本业务对象

    容器容器是Windchill对象存放的地方:在Windchill中主要的容器有站点.组织.产品.存储库.项目.在Windchill中所有容器对象的父类为wt.inf.container.WTConta ...

  9. js页面跳转常用的几种方式(转)

    js页面跳转常用的几种方式 转载  2010-11-25   作者:    我要评论 js实现页面跳转的几种方式,需要的朋友可以参考下. 第一种: 复制代码代码如下: <script langu ...

  10. css中的定位属性position(转)

    css中的定位属性position   同样的也是上课的时候发现学生难以理解的一些问题拿出来记录一下,希望帮助初学者. 在css中定位属性position的运用在页面中是很常用的,特别是一些结合js来 ...