The Most Distant State
Input: standard input
Output: standard output
The 8-puzzle is a square tray in which 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 be slid 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 in minimum 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 a bit different. In this problem, given an initial state of the puzzle your are asked to discover a goal state which is the most distant (in terms of number of moves) of all the states reachable from the given state.
Input
The first line of the input file contains an integer representing the number of test cases to follow. A blank line follows this line.
Each test case consists of 3 lines of 3 integers each representing the initial state of the puzzle. The blank space is represented by a 0 (zero). A blank line follows each test case.
Output
For each test case first output the puzzle number. The next 3 lines will contain 3 integers each representing one of the most distant states reachable from the given state. The next line will contain the shortest sequence of moves that will transform the given state to that state. The move is actually the movement of the blank space represented by four directions : U (Up), L (Left), D (Down) and R (Right). After each test case 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出所有的状态,判重用stl的哈希。
解析:题目要求出过程,所以要开一个fa数组来保存当前状态的,上一个状态,并用move数组来记录下当前的移动操作,最后用一个函数顺着fa数组求出,移动的步骤。
#include <stdio.h>
#include <string.h>
#include <set>
using namespace std;
const int MAXSTATE = 1000000;
const int dx[] = {-1, 1, 0, 0};
const int dy[] = { 0, 0,-1, 1};
typedef int State[9];
State st[MAXSTATE];
int move[MAXSTATE];
int fa[MAXSTATE];
const char dir[] = "UDLR";
set<int> vis;
void init_lookup_table() {
vis.clear();
}
bool try_to_insert(int s) {
int v = 0;
for(int i = 0; i < 9; i++) {
v = v*10 + st[s][i];
}
if(vis.count(v)) {
return false;
}
vis.insert(v);
return true;
}
void print_path(int cur){
if(cur != 1) {
print_path(fa[cur]);
putchar(dir[move[cur]]);
}
}
void bfs() {
init_lookup_table();
int front = 1,rear = 2;
while(front < rear) {
State& s = st[front];
int z;
for(z = 0; z < 9; z++) {
if(!s[z]) {
break;
}
}
int x = z/3, y = z%3;
for(int d = 0; d < 4; d++) {
int newx = x + dx[d];
int newy = y + dy[d];
int newz = 3*newx + newy;
if(newx < 0 || newx >= 3 || newy < 0 || newy >= 3) {
continue;
}
State& t = st[rear];
memcpy(&t,&s,sizeof(s));
t[newz] = s[z];
t[z] = s[newz];
if(try_to_insert(rear)) {
fa[rear] = front;
move[rear] = d;
rear++;
}
}
front++;
}
for(int i = 0; i < 3; i++) {
printf("%d %d %d\n",st[rear-1][i*3],st[rear-1][i*3+1],st[rear-1][i*3+2]);
}
print_path(rear-1);
}
int main() {
int t;
int cas = 1;
while(scanf("%d",&t) != EOF) {
while(t--) {
for(int i = 0; i < 9; i++) {
scanf("%d",&st[1][i]);
}
printf("Puzzle #%d\n",cas++);
bfs();
printf("\n\n");
}
}
return 0;
}