B.Grid with Arrows-The 2019 ICPC China Shaanxi Provincial Programming Contest
作者:互联网
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> const int MAXN = (int)1e6 + 5; 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); if (x <= n && x >= 1 && y >= 1 && y <= m && vis[m * (x - 1) + y] == false) { vis[m * (x - 1) + y] = true; int step = dig[m * (x - 1) + y]; if (str[m * (x - 1) + y] == 'u') dfs(x - step, y); if (str[m * (x - 1) + y] == 'd') dfs(x + step, y); if (str[m * (x - 1) + y] == 'l') dfs(x, y - step); if (str[m * (x - 1) + y] == 'r') dfs(x, y + step); } } void mflag(int x, int y) {if (x <= n && x >= 1 && y <= m && y >= 1)ind[m * (x - 1) + y]++;} void check(int x, int y) { int step = dig[m * (x - 1) + y]; if (str[m * (x - 1) + y] == 'u') mflag(x - step, y); if (str[m * (x - 1) + y] == 'd') mflag(x + step, y); if (str[m * (x - 1) + y] == 'l') mflag(x, y - step); if (str[m * (x - 1) + y] == 'r') mflag(x, y + step); } int main() { int t; scanf("%d", &t); while (t--) { scanf("%d%d", &n, &m); for(int i=1;i<=n*m;i++) vis[i]=false,ind[i]=0; for(int i=1;i<=n;i++) scanf(" %s",str+(i-1)+m+1); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) scanf("%d", &dig[m * (i - 1) + j]),check(i, j); int starti=1,startj=1; for (int i = 1; i <= n; i++) { bool tr = false; for (int j = 1; j <= m; j++) { if (ind[(i - 1) * m + j] == 0) { starti=i,startj=j; tr = true; break; } } if (tr) break; } dfs(starti,startj); bool flag = true; for(int i=1;i<=n*m;i++) if(!vis[i]) flag=false; if (flag) printf("Yes\n"); else printf("No\n"); } return 0; }
标签:Provincial,Shaanxi,BaoBao,Contest,int,text,cell,points,arrow 来源: https://www.cnblogs.com/rign/p/10988293.html