#P9641. [SNCPC2019] Grid with Arrows
[SNCPC2019] Grid with Arrows
题目描述
BaoBao has just found a grid with rows and columns in his left pocket, where the cell in the -th column of the -th row (indicated by ) contains an arrow (pointing either upwards, downwards, leftwards or rightwards) and an integer .
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 ), BaoBao will go on ticking another cell according to the arrow and the integer in cell .
- If the arrow in cell points upwards, BaoBao will go on ticking cell if it exists.
- If the arrow in cell points downwards, BaoBao will go on ticking cell if it exists.
- If the arrow in cell points leftwards, BaoBao will go on ticking cell if it exists.
- If the arrow in cell points rightwards, BaoBao will go on ticking cell 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 , indicating the number of test cases. For each test case:
The first line contains two integers and (), indicating the number of rows and columns of the grid.
For the following lines, the -th line contains a string consisting of lowercased English letters (, $s_{i, j} \in \{\text{`u' (ascii: 117)}, \text{`d' (ascii: 100)}, \text{`l' (ascii: 108)}, \text{`r'(ascii: 114)}\}$), where indicates the direction of arrow in cell .
- If , the arrow in cell points upwards.
- If , the arrow in cell points downwards.
- If , the arrow in cell points leftwards.
- If , the arrow in cell points rightwards.
For the following lines, the -th line contains integers (), where indicates the integer in cell .
It's guaranteed that the sum of of all test cases does not exceed .
输出格式
For each test case output one line. If BaoBao can find a proper initial cell, print Yes
(without quotes), otherwise print No
(without quotes).
2
2 3
rdd
url
2 1 1
1 1 2
2 2
rr
rr
1 1
1 1
Yes
No
提示
For the first sample test case, BaoBao can select cell as the initial cell, so that he can tick all the cells exactly once in the following order: .
For the second sample test case, BaoBao can only tick at most cells no matter which cell is selected as the initial cell.