Robot Contest
Time limit1sMemory limit1024 MB
Write a Pulibot program that colors one shortest path from (0,0) to (H-1,W-1) with 1 and every other empty cell with 0 in an unknown maze.
- Level
Hard9 of 10
- Topics
- Simulation, BFS, Implementation
- Solved
- No attempts yet
Problem
AI researchers at the University of Szeged are holding a robot programming contest. Your friend, Hanga, has decided to take part. The goal is to program the ultimate Pulibot, modeled on the intelligence of the Puli, a famous Hungarian herding dog breed.
Pulibot is tested on a maze made of a grid of cells. Rows are numbered to from north to south, and columns are numbered to from west to east. Cell is the cell in row and column , where and .
For a cell with and , the four adjacent cells are the west cell , the south cell , the east cell , and the north cell .
Cell is a boundary cell if , , , or . Every other cell is either an obstacle or an empty cell. Each empty cell has a color, an integer from to inclusive. Initially, every empty cell has color .
For example, the maze below has and and one obstacle at cell . The obstacle is marked with a cross, boundary cells are shaded, and the numbers show the colors of the empty cells.

A path of length () from cell to cell is a sequence of pairwise distinct empty cells where each pair of consecutive cells is adjacent. A path of length contains exactly cells.
The researchers set up a maze where at least one path exists from cell to cell . Cells and are therefore empty.
Hanga does not know which cells are empty and which are obstacles. Her task is to program Pulibot so that it finds a shortest path (a path of minimum length) from cell to cell in the unknown maze. The specification and the contest rules follow.
Pulibot's Specification
The state of a cell , for and , is an integer defined as follows:
- If cell is a boundary cell, its state is .
- If cell is an obstacle cell, its state is .
- If cell is an empty cell, its state is the color of the cell.
Pulibot's program runs as a sequence of steps. In each step, Pulibot reads the states of nearby cells, then performs an instruction chosen by those states.
Suppose Pulibot is at empty cell at the start of a step. The step proceeds as follows.
- Pulibot reads the state array . is the state of cell , is the state of the west cell, is the state of the south cell, is the state of the east cell, and is the state of the north cell.
- Pulibot determines the instruction that matches the state array.
- Pulibot sets the color of cell to , then performs action . The action is one of the following: stay at cell , move to one of the 4 adjacent cells, or terminate the program.
For example, suppose Pulibot is at cell with color and reads . Its program may set the color of the current cell to and then move east, as shown in the figures below.

Robot Contest Rules
- At the start, Pulibot is placed at cell and begins executing its program.
- Pulibot may not move to a cell that is not empty.
- Pulibot's program must terminate within steps.
- After the program terminates, the empty cells must satisfy the following. There exists a shortest path from to in which every cell on the path has color . Every other empty cell has color .
- Pulibot may terminate its program at any empty cell.
The figure below shows a possible maze with . The left side shows the starting configuration, and the right side shows the expected coloring after termination.

Constraints
. Pulibot can use colors from to , inclusive.
For each maze used to test Pulibot:
- There is at least one path from cell to cell .