atcoder#AGC033B. [AGC033B] LRUD Game
[AGC033B] LRUD Game
Score : points
Problem Statement
We have a rectangular grid of squares with horizontal rows and vertical columns. Let denote the square at the -th row from the top and the -th column from the left. On this grid, there is a piece, which is initially placed at square .
Takahashi and Aoki will play a game, where each player has a string of length .
Takahashi's string is , and Aoki's string is . and both consist of four kinds of letters: L
, R
, U
and D
.
The game consists of steps. The -th step proceeds as follows:
- First, Takahashi performs a move. He either moves the piece in the direction of , or does not move the piece.
- Second, Aoki performs a move. He either moves the piece in the direction of , or does not move the piece.
Here, to move the piece in the direction of L
, R
, U
and D
, is to move the piece from square to square , , and , respectively. If the destination square does not exist, the piece is removed from the grid, and the game ends, even if less than steps are done.
Takahashi wants to remove the piece from the grid in one of the steps. Aoki, on the other hand, wants to finish the steps with the piece remaining on the grid. Determine if the piece will remain on the grid at the end of the game when both players play optimally.
Constraints
- and consists of the four kinds of letters
L
,R
,U
andD
.
Input
Input is given from Standard Input in the following format:
Output
If the piece will remain on the grid at the end of the game, print YES
; otherwise, print NO
.
2 3 3
2 2
RRL
LUD
YES
Here is one possible progress of the game:
- Takahashi moves the piece right. The piece is now at .
- Aoki moves the piece left. The piece is now at .
- Takahashi does not move the piece. The piece remains at .
- Aoki moves the piece up. The piece is now at .
- Takahashi moves the piece left. The piece is now at .
- Aoki does not move the piece. The piece remains at .
4 3 5
2 2
UDRRR
LLDUD
NO
5 6 11
2 1
RLDRRUDDLRL
URRDRLLDLRD
NO