Image Recognition
Time limit1sMemory limit128 MB
Design an optimal decision program for a robot that moves on a grid and reads pixel colors to identify which of d given images it starts on, minimizing worst-case movement steps.
- Level
Hard8 of 10
- Topics
- BFS, Graph, Brute force, Simulation
- Solved
- No attempts yet
Problem
Irene works for Novel Efforts in Effective Recognition of Characters (NEERC). Her new project concerns image recognition using robots.
She starts with a very simple model. There are fixed images, called digits to . Each image is a rectangle of white and black unit squares (pixels). All images are distinct (every two images differ in at least one pixel).
A robot is placed on the upper-left pixel of one of the images and runs a program written in the language below. Its task is to recognize which of the images it was placed on.
The programming language consists of the following commands:
U,D,L,R--- movement commands. The robot moves one pixel up, down, left, or right. If a movement would take the robot outside the image, the task fails.(:)--- conditional. The robot checks the color of the pixel beneath it. If it is white then runs, otherwise runs.0,1, ...,9--- recognition commands. The robot executes one of these once it knows which image it is on; the program then terminates.
Each movement command takes one time unit. Conditionals and recognition commands are instantaneous.
A program is correct if, whenever the robot is placed on the image of digit , execution ends with the command i. Among all correct programs, consider the one whose worst-case execution time --- the maximum number of movement commands executed over the possible starting images --- is as small as possible. Determine this minimal possible worst-case execution time.
Input
The first line contains three integers , , and (; ) --- the number of images, and the height and width of each image.
The rest of the input contains image descriptions. Each description has lines of length , where every character is B (black) or W (white). Descriptions are given in order from image to image and are separated by a single empty line.
Output
Print a single integer: the minimal possible worst-case execution time of a correct recognition program.
Notes

The figure shows three example images that the robot must distinguish.