14066 - Text maze v2.0   

Description

This problem is modified from 13648 - Text maze.

There will be a 2D map composed of only 5 kinds of alphabets, which are a, b, c, d, and e.

 

First, given the position P (e.g., (2, 3)), you must find the smallest(Using ASCII code) char around it (should be at most eight choices).

Note that if there are duplicated chars, please choose the element with larger index:

 
Next, different from 13648 - Text maze, the instruction is decided by the summation (When calculating the summation, 'a' stands for 0, 'b' stands for 1, and so on.) of the previous character, the current character, and the chosen character (The one following the rule mentioned above.) .
 
If the summation exceeds 'e,' please return from 'a' to ensure that the result falls within the range of 'a' to 'e'. For example, if the summation is 6, you should return to 'b'.
 
 
Note that for the first step, the previous character will be itself.
 
 

Next,

if the summation is 'a', please move your current position to the left;

if the summation is 'b', please move your current position to the right; 

if the summation is 'c', please move your current position to the up; 

if the summation is 'd', please move your current position to the down;

if the summation is 'e', move your current position into the chosen char, and from now on, you have to find the biggest char, vise versa(If the current mode is to find the biggest char, start to find the smallest one).

 

There are two terminal situations:

(1) Your next step is outside the map (Next movement is illegal).

(2) You reach the given steps.

Once the whole process ends, please print the texts you've visited.

Input

There will be 4 parts for the input:

(1) The size of the map, n. (5 <= n <= 100)

(2) The maximum steps in the process, S. (1 <= S <= n**2 )

(3) The initial position(x, y).It is guaranteed that it is on the map.

(4) The text map with size n*n.

 

All of them are separated by a '\n'.

Output

The texts you've traversed.

Note that you dont' have to print "\n" at the end.

Sample Input  Download

Sample Output  Download

Tags




Discuss