Cross the maze (large)
Time limit5sMemory limit512 MB
Simulate a left-hand wall-following robot in an N by N maze for up to 10000 steps and print the step count and path if it reaches the exit.
- Level
Easy3 of 10
- Topics
- Simulation, Matrix
- Solved
- No attempts yet
Problem
Edison the robot has no right hand and no eyes. Whether he walks or turns in place, he always keeps his left hand on a wall. He thinks walking backward is too dangerous, so he never does it.
Edison is inside a square maze of cells. The maze is closed off by walls on the outside, and some cells inside the maze are walls as well. Edison moves between empty cells in the four directions north, south, west and east.
To get out, Edison keeps his left hand on the wall and follows it. Standing on a cell, he looks at the directions relative to the way he is facing in this order: left, forward, right, back. He turns toward the first of them that holds an empty cell, then walks one cell that way. The direction he just walked becomes the direction he faces. If all four directions are walls, he cannot move any more.
Decide whether Edison reaches the exit cell in at most 10,000 steps, and print the path when he does. Standing on the exit cell counts as getting out of the maze.
Input
The first line contains the number of test cases . The first line of each test case contains the size of the maze . Each of the next lines contains characters. . is an empty cell and # is a wall. The next line contains four integers , , , . Edison starts on row , column , and the exit is on row , column . The top left cell is at position .
Limits
- The starting cell is one of the four corners of the maze.
- The starting cell and the exit are both empty cells, and they are different cells.
Output
For each test case, print one line in the form Case #x: y. Here is the test case number, starting from . If Edison does not reach the exit within 10,000 steps, is Edison ran out of energy. without the quotes. Otherwise is the number of steps, and the next line holds characters describing the path. In the path, E is east, S is south, W is west and N is north. Turning in place is neither counted nor printed.
Notes
At the start, Edison's left hand touches a wall among the four cells directly above, below, left of and right of him, or the outer wall of the maze. A cell that touches only at a diagonal does not count.
Because the starting cell is a corner, two of the four directions are always the outer wall. So even when several starting orientations put his left hand on a wall, the rule above produces the same path for all of them.