#abc273d. D - LRUD Instructions
D - LRUD Instructions
Score : points
问题描述
存在一个具有 行(水平方向)和 列(垂直方向)的网格。 表示从上数第 行、从左数第 列的方格。
有 个方格,,设置了墙壁。
高桥初始位于方格 处。
给高桥提供了 条指令。对于 ,第 条指令由一对字符 和正整数 表示。其中, 可以为 L
、R
、U
或 D
,分别代表左、右、上、下四个方向。
在给定第 个方向的情况下,高桥将按照以下动作重复执行 次:
若当前所在方格在 所代表的方向上有一个没有墙壁的相邻方格,则移动到该方格;否则不做任何操作。
对于 ,请输出在高桥按照前 条指令行动后所处的方格坐标。
以上为通义千问 qwen-max 翻译,仅供参考。
Problem Statement
There is a grid with horizontal rows and vertical columns. denotes the square at the -th row from the top and -th column from the left.
squares, , have walls.
Takahashi is initially at square .
instructions are given to Takahashi. For , the -th instruction is represented by a pair of a character and a positive integer . is one of L
, R
, U
, and D
, representing the directions of left, right, up, and down, respectively.
Given the -th direction, Takahashi repeats the following action times:
If a square without a wall is adjacent to the current square in the direction represented by , move to that square; otherwise, do nothing.
For , print the square where Takahashi will be after he follows the first instructions.
Constraints
- for all .
- is one of the characters
L
,R
,U
, andD
. - All values in the input other than are integers.
Input
The input is given from Standard Input in the following format:
Output
Print lines. For , the -th line should contain the square where Takahashi will be after he follows the first instructions, in the following format:
Sample Input 1
5 5 4 4
3
5 3
2 2
1 4
4
L 2
U 3
L 2
R 4
Sample Output 1
4 2
3 2
3 1
3 5
The given grid and the initial position of Takahashi are as follows, where #
denotes a square with a wall, T
a square where Takahashi is, and .
the other squares:
...#.
.#...
.....
...T.
..#..
Given the -st instruction, Takahashi moves squares to the left, ending up in square as follows:
...#.
.#...
.....
.T...
..#..
Given the -nd instruction, Takahashi first moves square upward, then he "does nothing" twice because the adjacent square in his direction has a wall. As a result, he ends up in square as follows:
...#.
.#...
.T...
.....
..#..
Given the -rd instruction, Takahashi first moves square to the left, then he "does nothing" once because there is no square in his direction. As a result, he ends up in square as follows:
...#.
.#...
T....
.....
..#..
Given the -th instruction, Takahashi moves squares to the right, ending up in square as follows:
...#.
.#...
....T
.....
..#..
Sample Input 2
6 6 6 3
7
3 1
4 3
2 6
3 4
5 5
1 1
3 2
10
D 3
U 3
L 2
D 2
U 3
D 3
U 3
R 3
L 3
D 1
Sample Output 2
6 3
5 3
5 1
6 1
4 1
6 1
4 1
4 2
4 1
5 1
update @ 2024/3/10 11:30:22