Rush HourProblem code: RUSHHOUR |
All submissions for this problem are available.
Rush Hour is a popular puzzle game which is now available on many mobile devices.
There is a board of size M x N. The board is full of cars and trucks. Each car has size 1x2 and each truck has size 1x3. Cars and trucks can be either horizontal or vertical.
At each step, we can move a horizontal car or truck along its row if there is no other objects in its way. Similarly we can move a vertical car or truck along its column.
Among the cars, one belongs to the president. It is always horizontal. The object of the game is to get the president's car to the exit gate at the rightmost column in its row.
You need to write a solver for Rush Hour, which takes a map of the game and returns the minimum number of steps to solve it.
As an example, look at the figures below. The president's car is painted in yellow. It takes 5 steps to solve this game instance.
Input
The first line contains the number of test cases (about 10). Each test case has the following form.
The first line contains three numbers M, N and K. M and N are the size of the board. K are the total number of cars and trucks. (6 <= M, N <= 12, 3 <= K <= 26)
Each line in the next M lines contains N characters. Each character can be '.' or 'a'..'z'. '.' stands for an empty square. 'a'..'z' represents the ID of the car of truck that occupies the square. The president's car is always 'a'.
It is guaranteed that the president's car initial position is not at the rightmost column.
The requirement of the game is guaranteed by the input, that is each object can only have size 1x2 or 1x3.
You can assume that there is always a solution and the optimal number of steps to solve any puzzle instance given in the input is at most 10.
Each test case's input is separated by a blank line.
Output
For each test case, print in a single line the optimal number of steps to solve the corresponding puzzle.
Test case generator
Note that in general, solving Rush Hour puzzle is very hard, so in this problem you are only required to solve random generated instances of the puzzle. We briefly describe how we generated the test cases.
Given M, N, K, for each car, we let it be horizontal or vertical with equal probability. The car's size is randomly picked from 2 to 3. The car's position is also chosen randomly. To make sure that a solution exists, we start with a state in which the president's car is already at the exit gate. After that, we perform some random steps (around 10000). The final state is used as the input data.
Example
Input 2 6 6 13 b.c..e b.cdde fghaal fghjjl .giikk mmm... 6 6 10 ...... .bb.cc aad.hi fed.hi feg..j f.g..j Output 5 6
The first sample test case is explained in the figure.
| Author: | duc_admin |
| Date Added: | 3-12-2010 |
| Time Limit: | 4 sec |
| Source Limit: | 50000 Bytes |
| Languages: | ADA, ASM, BASH, BF, C, C99 strict, CAML, CLOJ, CLPS, CPP 4.0.0-8, CPP 4.3.2, CS2, D, ERL, F#, FORT, GO, HASK, ICK, ICON, JAR, JAVA, JS, LISP clisp, LISP sbcl, LUA, NEM, NICE, PAS fpc, PAS gpc, PERL, PERL6, PHP, PIKE, PRLG, PYTH, PYTH 3.1.2, RUBY, SCALA, SCM guile, SCM qobi, ST, TCL, TEXT, WSPC |
Comments
SUCCESSFUL SUBMISSIONS FOR THIS PROBLEM:
HELP
Program should read from standard input and write to standard output. After you submit a solution you can see your results by clicking on the [My Submissions] tab on the problem page. Below are the possible results:
- Accepted
Your program ran successfully and gave a correct answer. If there is a score for the problem, this will be displayed in parenthesis next to the checkmark. - Time Limit Exceeded
Your program was compiled successfully, but it didn't stop before time limit. Try optimizing your approach. - Wrong Answer
Your program compiled and ran succesfully but the output did not match the expected output. - Runtime Error
Your code compiled and ran but encountered an error. The most common reasons are using too much memory or dividing by zero. For the specific error codes see the help section. - Compilation Error
Your code was unable to compile. When you see this icon, click on it for more information.
If you are still having problems, see a sample solution here.

Fetching successful submissions

>>>As an example, look at the