Robot Contest
문제
AI researchers at the University of Szeged are holding a robot programming contest. Your friend, Hanga, has decided to take part in the contest. The objective is to program the ultimate Pulibot, admiring the great intelligence of the famous Hungarian herding dog breed, the Puli.
Pulibot will be tested on a maze consisting of a grid of cells. The rows of the grid are numbered from to from north to south and the columns of the grid are numbered from to from west to east. We refer to the cell located at row and column of the grid (, ) as cell .
Consider a cell such that and . There are cells adjacent to cell :
- cell is referred to as the cell west of cell ;
- cell is referred to as the cell south of cell ;
- cell is referred to as the cell east of cell ;
- cell is referred to as the cell north of cell .
Cell is called a boundary cell of the maze if or or or holds. Each cell that is not a boundary cell of the maze is either an obstacle cell or an emptycell. Additionally, each empty cell has a color, represented by a nonnegative integer between and , inclusive. Initially, the color of each empty cell is .
For example, consider a maze with and , containing a single obstacle cell :
The only obstacle cell is denoted by a cross. Boundary cells of the maze are shaded. The numbers written to each empty cell represent their respective colors.
A path of length () from cell to cell is a sequence of pairwise distinct *empty* cells in which for each () the cells and are adjacent.
Note that a path of length contains exactly cells.
At the contest, the researchers set up a maze in which there exists at least one path from cell to cell . Note that this implies that cells and are guaranteed to be empty.
Hanga does not know which cells of the maze are empty and which cells are obstacles.
Your task is to help Hanga to program Pulibot so that it is capable of finding a shortest path (that is, a path of minimum length) from cell to cell in the unknown maze set up by the researchers. The specification of Pulibot and the rules of the contest are described below.
Pulibot's Specification
Define the state of a cell for each and as an integer so that:
- if cell is a boundary cell then its state is ;
- if cell is an obstacle cell then its state is ;
- if cell is an empty cell then its state is the color of the cell.
Pulibot's program is executed as a sequence of steps. In each step, Pulibot recognizes the states of nearby cells and then performs an instruction. The instruction it performs is determined by the recognized states. A more precise description follows.
Suppose that at the beginning of the current step, Pulibot is at cell , which is an empty cell. The step is performed as follows:
- First, Pulibot recognizes the current state array, that is, the array , consisting of the state of cell and of all adjacent cells:
- is the state of cell .
- is the state of the cell to the west.
- is the state of the cell to the south.
- is the state of the cell to the east.
- is the state of the cell to the north.
- Then, Pulibot determines the instruction which corresponds to the recognized state array.
- Finally, Pulibot performs that instruction: it sets the color of cell to color and then it performs action , which is one of the following actions:
- stay at cell ;
- move to one of the adjacent cells;
- terminate the program.
For example, consider the scenario displayed on the left of the following figure. Pulibot is currently at cell with the color . Pulibot recognizes the state array . Pulibot may have a program which, upon recognizing this array, sets the color of the current cell to and then moves to the east, as displayed in the middle and on the right of the figure:
Robot Contest Rules
- At the start, Pulibot is placed at cell and begins to execute its program.
- Pulibot is not allowed to move to a cell which is not empty.
- Pulibot's program must terminate after at most steps.
- After the termination of Pulibot's program, empty cells in the maze should be colored such that:
- There exists a shortest path from to for which the color of each cell included in the path is .
- The color of every other empty cell is .
- Pulibot may terminate its program at any empty cell.
For example, the following figure shows a possible maze with . The starting configuration is displayed on the left and the expected coloring of empty cells after termination is displayed on the right: