This page is still under construction.

Parts of this page are still being built. What you see may change.

Moveable Maze

Time limit1sMemory limit128 MB

Summary
Each turn lets you rotate one grid square by 90 degrees and then take one step along matching lines, and you must minimize turns from a start cell to a goal cell.
Level

Hard8 of 10

Topics
BFS, Graph, Simulation, Bit manipulation
Solved
No attempts yet

Problem

You are playing a puzzle game on a grid with RR rows and CC columns. Each square has a black dot at its centre, and black lines may extend from that dot toward some of its north, east, south, and west neighbours (possibly none of them, possibly all four).

Your piece starts at the centre of the square in row i1i_1, column j1j_1, and you want to move it to the centre of the square in row i2i_2, column j2j_2, using as few turns as possible.

A turn has two parts, and either part may be skipped:

  1. Rotation: you may pick any one square of the grid and rotate it 90 degrees, clockwise or counterclockwise. All of that square's lines rotate together.
  2. Movement: you may move your piece from the centre of its current square to the centre of a neighbouring square, as long as the piece never leaves the black lines. Concretely, you may move from square AA to a neighbouring square BB only if AA has a line pointing toward BB and BB has a line pointing back toward AA.

Print the minimum number of turns needed to move the piece from (i1,j1)(i_1, j_1) to (i2,j2)(i_2, j_2). It is guaranteed that the destination is reachable.

Input

The input contains several test cases.

The first line of each test case contains two integers RR and CC (1≤R,C≤201 \le R, C \le 20).

The second line contains four integers i1i_1, j1j_1, i2i_2, j2j_2 (1≤i1,i2≤R1 \le i_1, i_2 \le R and 1≤j1,j2≤C1 \le j_1, j_2 \le C): the starting row and column, then the destination row and column.

Each of the next RR lines describes one row of the grid, from north (top) to south (bottom). Every such line contains exactly CC space-separated tokens describing that row's squares from west (left) to east (right). Each token is one of:

  • the single character x, meaning the square has no line to any neighbour; or
  • a string made of some of the characters N, E, S, W (each appearing at most once), where N, E, S, W mean the square has a line toward its north, east, south, or west neighbour respectively.

It is guaranteed that the piece can be moved from (i1,j1)(i_1, j_1) to (i2,j2)(i_2, j_2).

The input ends with a line containing 0 0, which must not be processed as a test case.

Output

For each test case, print a single line containing one integer: the minimum number of turns required to move the piece from (i1,j1)(i_1, j_1) to (i2,j2)(i_2, j_2).

Hint

Rotating a square affects only that square, but you may rotate any square on the board (not only the one your piece is on), so you can prepare distant squares in advance. Because rotation and movement can happen in the same turn, aligning a path and walking along it can overlap.

For a 4×24 \times 2 board where the piece starts in row 1, column 1 and must reach row 4, column 1, one optimal sequence of 5 turns is:

  1. Rotate square (2,2)(2,2) clockwise, then step to (1,2)(1,2).
  2. Rotate square (3,2)(3,2) counterclockwise, then step to (2,2)(2,2).
  3. Rotate square (3,2)(3,2) counterclockwise, then step to (3,2)(3,2).
  4. Rotate square (3,1)(3,1) clockwise, then step to (3,1)(3,1).
  5. Rotate square (3,1)(3,1) clockwise, then step to (4,1)(4,1).

Examples4

  1. Example 1

    Input
    4 2
    1 1 4 1
    E SW
    x EW
    NW ES
    N x
    0 0
    
    Expected output
    5
    
  2. Example 2

    Input
    1 1
    1 1 1 1
    E
    0 0
    
    Expected output
    0
    
  3. Example 3

    Input
    1 2
    1 1 1 2
    N N
    0 0
    
    Expected output
    2
    
  4. Example 4

    Input
    1 3
    1 1 1 3
    EW EW EW
    0 0
    
    Expected output
    2