#C2680. Dungeon Explorer
Dungeon Explorer
Dungeon Explorer
You are given a square dungeon of size \(N\times N\) where each cell contains one of three characters: '.' representing an open path, '#' representing a wall, or 'M' representing a magic wall. The hero starts at the top-left cell (1,1) and must reach the bottom-right cell (N,N).
Before each query, the dungeon grid may be modified: for a query that specifies a cell position, if that cell contains a magic wall ('M'), it will update to an open path ('.') if and only if it has exactly two open neighboring cells. The four possible neighbors are those directly up, down, left, and right of the cell.
After processing each query, determine if the hero can reach the exit by moving only in the four cardinal directions. The input consists of several test cases; a test case starts with the integer \(N\), followed by \(N\) lines of dungeon grid data, then an integer \(Q\) representing the number of queries, and finally \(Q\) queries. The end of input is signaled by a line with a single 0.
inputFormat
The input is read from standard input (stdin) and is composed of multiple test cases. Each test case is structured as follows:
- An integer \(N\) — the size of the square grid.
- Next \(N\) lines, each line containing exactly \(N\) characters ('.', '#' or 'M') that represent the dungeon grid.
- An integer \(Q\) — the number of queries.
- Following \(Q\) lines each containing two space-separated integers \(x\) and \(y\) (1-indexed) indicating the cell to process.
- A line with a single 0 indicating the end of input.
outputFormat
For each query, print to standard output (stdout) a single line containing YES
if the hero can reach the exit after processing that query, or NO
otherwise.
4
.M#M
.#M.
M..M
###.
2
1 2
2 3
0
YES
YES
</p>