atcoder#ABC273D. [ABC273D] LRUD Instructions
[ABC273D] LRUD Instructions
Score : points
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:
5 5 4 4
3
5 3
2 2
1 4
4
L 2
U 3
L 2
R 4
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
.....
..#..
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
6 3
5 3
5 1
6 1
4 1
6 1
4 1
4 2
4 1
5 1