Under construction

Still being built. Things may change

Cross the maze (large)

Time limit5sMemory limit512 MB

Summary
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 N×NN \times N 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 TT. The first line of each test case contains the size of the maze NN. Each of the next NN lines contains NN characters. . is an empty cell and # is a wall. The next line contains four integers sxsx, sysy, exex, eyey. Edison starts on row sxsx, column sysy, and the exit is on row exex, column eyey. The top left cell is at position (1,1)(1, 1).

Limits

  • 1≤T≤301 \le T \le 30
  • 2≤N≤1002 \le N \le 100
  • 1≤sx,sy,ex,ey≤N1 \le sx, sy, ex, ey \le N
  • The starting cell (sx,sy)(sx, sy) 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 xx is the test case number, starting from 11. If Edison does not reach the exit within 10,000 steps, yy is Edison ran out of energy. without the quotes. Otherwise yy is the number of steps, and the next line holds yy 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.

Examples2

  1. Example 1

    Input
    3
    2
    .#
    #.
    1 1 2 2
    5
    .##.#
    .....
    ...#.
    .###.
    ...#.
    1 1 5 3
    3
    ...
    .#.
    ...
    1 1 3 3
    
    Expected output
    Case #1: Edison ran out of energy.
    Case #2: 22
    SEEENSESSSNNNWWSWWSSEE
    Case #3: 4
    EESS
    
  2. Example 2

    Input
    1
    2
    ..
    ..
    1 1 2 2
    
    Expected output
    Case #1: 2
    ES