Grid Escape
Time limit20sMemory limit1024 MB
Point each grid room at one door so exactly K players walk out of the grid, and print the direction grid or IMPOSSIBLE.
- Level
Medium4 of 10
- Topics
- Graph, Simulation, Implementation
- Solved
- No attempts yet
Problem
You are building an escape adventure inside a rectangular grid of rooms with rows and columns. Every room has four doors, one on each side: north, south, east, and west. A door on the border of the grid leads outside. Every other door connects two neighboring rooms.
Exactly players play, one in each room. When the game starts, all doors lock and a mechanism fixes which door each room can open: in every room exactly one of the four doors opens from the inside, and that door stays the same for the whole adventure. A door between two rooms may open from one side and stay locked from the other side.
Each player moves on their own. A player may walk only through a door they opened themselves, and the door closes behind them. A player keeps walking through doors until they walk through a door that leads outside, which means they escaped, or until they have made moves without leaving the grid, which means they failed.
Choose the door that opens in each room so that exactly players escape, or report that no choice does that.
Input
The first line has the number of test cases, . Each of the next lines has three integers , , and .
Output
For each test case, print one line in the form Case #x: y. Here x is the test case number starting from 1, and y is IMPOSSIBLE when no arrangement lets exactly players escape, or POSSIBLE when one does. After a POSSIBLE line, print more lines of characters each. The -th character of the -th of those lines is N, S, E, or W, telling which door opens in the room in row , column .
Several arrangements can let exactly players escape, so print the one built by the following rule. Let .
- Number the rooms in zigzag order: row 1 from left to right, row 2 from right to left, row 3 from left to right again, and so on, giving through . Two rooms next to each other in this order always share a door.
- For from 1 to , the room opens the door leading to . The room opens the door leading to . When , this step fixes no room.
- Every remaining room opens its south door.
Constraints
Hint
A player who does not escape is stuck in a loop. From any room the open doors give one path forward, so a player either reaches the outside or walks the same set of rooms forever. That is why moves are enough to settle every player.
In a grid with a single room, the one door that opens leads outside, so that player always escapes.