Rook Jump Maze

Problem #256

Tags: c-1 popular-algorithm classical

Who solved this?

No translations... yet

The problem originates, probably, with famous Sam Loyd's Back from Klondike. As author says, unlike most of puzzles about mazes this type probably isn't completely trivial. At least for manual solving.

Suppose, the Princess in Magic Kingdom decided to walk a bit after hearty lunch and got lost in the nearby beautiful magic swamp. She then fell asleep.

You are the hero to find her to wake her up. She shall magically teleport both of you back, so return is not a problem.

Real problem is the swamp-maze.

It is square field of small springy bumps. You start at the top-left corner of the map (with coordinates X=0, Y=0) and can jump from bump to bump. However jumps could be made only in 4 orthogonal directions (north, south, west, east). And the distance is completely defined by the properties of the given bump.

So while you know perfectly coordinates where the Princess sleeps (actually, can see her from afar and hear her snorring soundly) - you can't go there directly.

On the swamp of size 10 for example, if you arrive at the bump with coordinates X=1, Y=3 and find the bump here can send you exactly 2 squares away, you can land at (1, 5), or (1, 1), or (3, 3) or (9, 3) - there is a magical wrapping of swamp borders (e.g. it is toroidal in topologic sense).

Input data:
First line contains N X Y - size of the maze and coordinates of the Princess (zero-based).
Next here follow N lines of N positive integers each (bump jumping distances) - the swamp map.

Answer: should give directions, single letter (of N, S, W, E) for each jump, without spaces.

Example:

input:
5 0 1
2 4 4 4 4
3 3 4 3 4
3 4 3 4 4
4 4 3 4 4
3 4 4 2 3

answer:
SENNSW

We start at top-left corner, do jump of 2 down (south), then 3 right (east), then twice we jump north by 4 (wrapping every time) eventually ariving at 2 (X=3 Y=4) - and wrap south from here to return 3 west to X=0 Y=1 as requested.

This one is not the shortest way (it was preferred simply for having all 4 letters included) - the checker shall accept any answer which leads to the target cell and is not longer than N*N steps!

You need to login to get test data and submit solution.