首页 > 代码库 > UVA The most distant state
UVA The most distant state
题目如下:
Problem A
The Most Distant State
Input: standard input
Output: standard output
The 8-puzzle is a square tray inwhich eight square tiles are placed. The remaining ninth square is uncovered.Each tile has a number on it. A tile that is adjacent to the blank space can beslid into that space. A game consists of a starting state and a specified goal state.The starting state can be transformed into the goal state by sliding (moving)the tiles around. The 8-puzzle problem asks you to do the transformation inminimum number of moves.
2 | 8 | 3 |
|
|
| 1 | 2 | 3 |
1 | 6 | 4 | => | 8 |
| 4 | ||
7 |
| 5 |
|
|
| 7 | 6 | 5 |
Start |
|
|
| Goal |
However, our current problem is abit different. In this problem, given an initial state of the puzzle your areasked to discover a goal state which is the most distant (in terms of number ofmoves) of all the states reachable from the given state.
Input
The first line of the input filecontains an integer representing the number of test cases to follow. A blankline follows this line.
Each test case consists of 3lines of 3 integers each representing the initial state of the puzzle. Theblank space is represented by a 0 (zero). A blank line follows each test case.
Output
For each test case first outputthe puzzle number. The next 3 lines will contain 3 integers each representingone of the most distant states reachable from the given state. The next linewill contain the shortest sequence of moves that will transform the given stateto that state. The move is actually the movement of the blank space representedby four directions : U (Up), L (Left), D (Down) and R (Right). After each testcase output an empty line.
Sample Input
12 6 4
1 3 7
0 5 8
Sample Output
Puzzle #18 1 5
7 3 6
4 0 2
UURDDRULLURRDLLDRRULULDDRUULDDR
八数码问题,求出距离初始状态最远的目标状态并打印出路径,用BFS解决,最后一个状态就是最远的状态,用哈希判重。
AC的代码如下:
UVA The most distant state