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>
;
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的更多相关文章
- 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 ...
- 计蒜客 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 ...
- 计蒜客 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. ...
- 计蒜客 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 ...
- 计蒜客 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 ...
- 计蒜客 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 ...
- The 2019 ACM-ICPC China Shannxi Provincial Programming Contest (西安邀请赛重现) J. And And And
链接:https://nanti.jisuanke.com/t/39277 思路: 一开始看着很像树分治,就用树分治写了下,发现因为异或操作的特殊性,我们是可以优化树分治中的容斥操作的,不合理的情况只 ...
- The 2018 ACM-ICPC China JiangSu Provincial Programming Contest快速幂取模及求逆元
题目来源 The 2018 ACM-ICPC China JiangSu Provincial Programming Contest 35.4% 1000ms 65536K Persona5 Per ...
- 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 ...
随机推荐
- linux命令学习笔记(57):ss命令
ss是Socket Statistics的缩写.顾名思义,ss命令可以用来获取socket统计信息,它可以显示和netstat 类似的内容.但ss的优势在于它能够显示更多更详细的有关TCP和连接状态的 ...
- 【遍历二叉树】04二叉树的层次遍历【Binary Tree Level Order Traversal】
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树,返回他的层次遍历的 ...
- Gym 101142 I.Integral Polygons(计算几何)
题意:给定一个凸包,现在让你连接凸包上两点,把凸包变为两个多边形,满足两个多边形的面积都是整数. 思路:我们知道整点的三角形面积S=叉积/2,则S要么是整数,要么是整数+0.5.那么多边形有多个三角形 ...
- bzoj 4199: [Noi2015]品酒大会 后缀树
题目大意: 给定一个长为n的字符串,每个下标有一个权\(w_i\),定义下标\(i,j\)是r相似的仅当\(r \leq LCP(suf(i),suf(j))\)且这个相似的权为\(w_i,w_j\) ...
- NET Remoting 最简单示例
NET Remoting 最简单示例 2014-01-21 15:29 10492人阅读 评论(4) 收藏 举报 分类: .NET(6) 版权声明:本文为博主原创文章,未经博主允许不得转载. 学习 ...
- 【转】Pro Android学习笔记(八):了解Content Provider(下中)
在之前提供了小例子BookProvider,我们回过头看看如何将通过该Content Provider进行数据的读取. (1)增加 private void addBook(String name , ...
- Dubbo注册中心的四种配置方式详解
Dubbo目前支持4种注册中心,(multicast,zookeeper,redis,simple) 推荐使用Zookeeper注册中心. 一.Multicast注册中心 不需要启动任何中心节点,只要 ...
- modbus读输入状态与读线圈状态的区别?
01 读线圈状态 描述 读从机离散量输出口的 ON/OFF 状态,不支持广播.附录B列出由不同控制器型号支持最大的参数清单. 查询 查询信息规定了要读的起始线圈和线圈量,线圈的起始地址为零,1-16个 ...
- 与input有关的一些操作
单选 : 通过name指定为一组,只能选择一个 一组一个选项 <input type=" name="sex"/>男 <input type=&quo ...
- c语言基础 c和指针
句子 c规定数组名代表数组首元素的地址 如果&a 则代表整个数组 没有内存哪来的指针 数据类型的本质:固定大小内存的别名 变量的本质:(一段连续)内存空间的别名,内存空间的标号 指针是一种数据 ...