
| Contests | Virtual Contests | Problems | Submit | Runs Status | Rank List | Forum |
In the sixties ("make love not war") a one-person variant of the game emerged. In this variant one person moves all the three pieces, not necessarily one after the other, but of course only one at a time. Goal of this game is to get all pieces onto the same location, using as few moves as possible. Find out the smallest number of moves that is necessary to get all three pieces onto the same location, for a given board layout and starting positions.
Input Specification
The input file contains several test cases. Each test case starts with the number n. Input is terminated by n=0. Otherwise, 1≤n≤50. Then follow three integers p1, p2, p3 with 1≤pi≤n denoting the starting locations of the game pieces. The colours of the arrows are given next as a m×m matrix of whitespace-separated lower-case letters. The element mij denotes the colour of the arrow between the locations i and j. Since the graph is undirected, you can assume the matrix to be symmetrical.
Output Specification
For each test case output on a single line the minimum number of moves required to get all three pieces onto the same location, or the word "impossible" if that is not possible for the given board and starting locations.
|
Sample Input
|
Sample Output
|
|