#C6566. Dungeon Escape
Dungeon Escape
Dungeon Escape
You are given a dungeon composed of multiple levels. Each level is a grid with various characters:
- S: the starting point (only in the first level).
- E: the exit.
- .: walkable cell.
- #: wall (impassable cell).
- U: a staircase that goes upstairs (to a lower index level if possible).
- D: a staircase that goes downstairs (to a higher index level if possible).
You are also given a sequence of moves. Each move has a direction (U, D, L, R) and a number of steps. Starting from the cell marked S in the first level, simulate the moves step by step. At each step:
- Try to move in the given direction if the target cell is within bounds and is not a wall.
- After moving, if the new cell is a staircase (U or D), update the current level appropriately (if possible).
- If you reach the cell E at any time, print YES and stop processing the current test case.
If after performing all moves the exit is not reached, print NO.
The dungeon input may contain multiple test cases. Each test case starts with an integer \( N \) denoting the number of levels, followed by data for each level, the moves for that test case, and the sequence is terminated by a test case where \( N = 0 \).
The coordinates are zero-indexed and moves are executed one step at a time. Formally, if the adventurer is at coordinate \( (r, c) \) and a move in direction R is performed, then the new coordinate becomes \( (r, c+1) \) provided \( c+1 \) is within the grid and the cell is not a wall.
inputFormat
The input consists of several test cases. Each test case is given in the following format:
- An integer \( N \) representing the number of levels in the dungeon. A value of \( 0 \) indicates the end of input.
- For each level from 1 to \( N \):
- An integer \( R \) and \( C \) representing the number of rows and columns.
- \( R \) lines, each containing a string of length \( C \), representing the grid.
- An integer \( M \) representing the number of moves.
- \( M \) lines, each containing a character (one of U, D, L, R) and an integer indicating the number of steps for that move.
All input is read from standard input.
outputFormat
For each test case, output a single line with YES if the exit can be reached, or NO otherwise. All output should be written to standard output.
## sample1
3 3
S..
.#.
..E
2
R 2
D 2
0
YES
</p>