10085 - The most distant state
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
__________________________________________________________________________________________
Rezaul Alam Chowdhury
"A fool looks for happiness in the distance, those who are intelligent grow it under their own feet."
#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
using namespace std;
const int MAXN = 1000000;
char input[30];
int state[9], goal[9] = {1, 2, 3, 8, 0, 4, 7, 6, 5};
int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
char path_dir[5] = "UDLR";
int st[MAXN][9];
int father[MAXN], path[MAXN], ans;
const int MAXHASHSIZE = 1000003;
int head[MAXHASHSIZE], next[MAXN];
void init_lookup_table() {
memset(head, 0, sizeof(head));
}
typedef int State[9];
int hash(State &s) {
int v = 0;
for (int i = 0; i < 9; i ++) {
v = v * 10 + s[i];
}
return v % MAXHASHSIZE;
}
int try_to_insert(int s) {
int h = hash(st[s]);
int u = head[h];
while (u) {
if (memcmp(st[u], st[s], sizeof(st[s])) == 0) return 0;
u = next[u];
}
next[s] = head[h];
head[h] = s;
return 1;
}
void bfs() {
init_lookup_table();
father[0] = path[0] = -1;
int front = 0, rear = 1;
memcpy(st[0], state, sizeof(state));
try_to_insert(0);
while (front < rear) {
int *s = st[front];
int j;
for (j = 0; j < 9; j ++) {
if (! s[j]) {
break;
}
}
int x = j / 3, y = j % 3;
for (int i = 0; i < 4; i ++) {
int dx = x + dir[i][0];
int dy = y + dir[i][1];
int pos = dx * 3 + dy;
if (dx >= 0 && dx < 3 && dy >= 0 && dy < 3) {
int *newState = st[rear];
memcpy(newState, s, sizeof(int) * 9);
newState[j] = s[pos];
newState[pos] = 0;
if (try_to_insert(rear)) {
father[rear] = front;
path[rear] = i;
rear ++;
}
}
}
front ++;
}
ans = rear - 1;
}
void print_path(int cur) {
if (cur != 0) {
print_path(father[cur]);
printf("%c", path_dir[path[cur]]);
}
}
int main()
{
int T, cas = 1;
scanf("%d", &T);
while (T --) {
for (int i = 0; i < 9; i ++) {
scanf("%d", &state[i]);
}
bfs();
printf("Puzzle #%d\n", cas ++);
for (int i = 0; i < 3; i ++) {
printf("%d %d %d\n", st[ans][i * 3], st[ans][i * 3 + 1], st[ans][i * 3 + 2]);
}
print_path(ans);
printf("\n\n");
}
return 0;
}

本文介绍了一个关于8谜题的变化版本,目标是找到从初始状态出发可达的最远状态,并提供了一种通过广度优先搜索算法来解决该问题的方法。

522

被折叠的 条评论
为什么被折叠?



