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,也可以直接判正确

以下代码:

#include <cstdio>
#include <cstring>
#include <queue>

;
char str[MAXN];
int dig[MAXN];
int vis[MAXN];
int ind[MAXN];
int n, m;

void dfs(int x, int y) {
    //printf("%d %d\n",x,y);
     && y >=  && y <= m && vis[m * (x - ) + y] == false) {
        vis[m * (x - ) + y] = true;
        ) + y];
        ) + y] == 'u') dfs(x - step, y);
        ) + y] == 'd') dfs(x + step, y);
        ) + y] == 'l') dfs(x, y - step);
        ) + y] == 'r') dfs(x, y + step);
    }
}

 && y <= m && y >= )ind[m * (x - ) + y]++;}

void check(int x, int y) {
    ) + y];
    ) + y] == 'u') mflag(x - step, y);
    ) + y] == 'd') mflag(x + step, y);
    ) + y] == 'l') mflag(x, y - step);
    ) + y] == 'r') mflag(x, y + step);
}

int main() {
    int t;
    scanf("%d", &t);
    while (t--) {
        scanf("%d%d", &n, &m);
        ;i<=n*m;i++) vis[i]=;
        ;i<=n;i++) scanf()+m+);
        ; i <= n; i++)
            ; j <= m; j++)
                scanf() + j]),check(i, j);
        ,startj=;
        ; i <= n; i++) {
            bool tr = false;
            ; j <= m; j++) {
                ) * m + j] == ) {
                    starti=i,startj=j;
                    tr = true;
                    break;
                }
            }
            if (tr) break;
        }
        dfs(starti,startj);
        bool flag = true;
        ;i<=n*m;i++)
            if(!vis[i]) flag=false;
        if (flag) printf("Yes\n");
        else printf("No\n");
    }
    ;
}

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. linux命令学习笔记(4):mkdir命令

    linux mkdir 命令用来创建指定的名称的目录,要求创建目录的用户在当前目录中具有写权限, 并且指定的目录名不能是当前目录中已有的目录. .命令格式: mkdir [选项] 目录... .命令功 ...

  2. linux命令学习笔记(19):find 命令概览

    Linux下find命令在目录结构中搜索文件,并执行指定的操作.Linux下find命令提供了相当多的查找条件,功能 很强大.由于find具有强大的功能,所以它的选项也很多,其中大部分选项都值得我们花 ...

  3. unity破解步骤

    1.选择unity的安装目录 C:\Programe Files (x86)\Unity\Editor 2.点击patch 3.使用random生成序列号 4.使用Cre Lic生成授权文件

  4. 制作SD卡img文件,并扩容

    /********************************************************************************** * raspi-config E ...

  5. 数据schemaAvro简介

    文章结束给大家来个程序员笑话:[M] 最近在研究Thrift和Avro以及它们的区分,通过各种渠道搜集资料,现整顿出有关Avro的一些资料,方便当前参考. 一.弁言 1. 简介 Avro是Hadoop ...

  6. oracle rac搭建

    (一)环境准备 主机操作系统 windows10 虚拟机平台 vmware workstation 12 虚拟机操作系统 redhat 5.5 x86(32位) :Linux.5.5.for.x86. ...

  7. PythonPath在Windows 下的设置

    今天在调试Evernote SDK时, 遇到PythonPath的问题. 查了很多资料,有说用系统环境变量添加PythonPath, 有说在注册表中的PythonPath添加新Default字段, 但 ...

  8. [转]unity3d中创建双面材质

    在其它三维软件中设置好的双面材质导入到unity3d中就失去了效果,不过我们可以通过自定义材质来在unity3d中实现双面材质的效果.步骤如下:1.在资源库中新建一新shader:代码如下: Shad ...

  9. 优秀开源项目之三:高性能、高并发、高扩展性和可读性的网络服务器架构State Threads

    译文在后面. State Threads for Internet Applications Introduction State Threads is an application library ...

  10. 数据库:mysql 获取刚插入行id[转]

    我们在写数据库程序的时候,经常会需要获取某个表中的最大序号数, 一般情况下获取刚插入的数据的id,使用select max(id) from table 是可以的.但在多线程情况下,就不行了. 下面介 ...