原题链接 http://poj.org/problem?id=1077
Eight
Description
The 15-puzzle has been around for over 100 years; even if you don't know it by that name, you've seen it. It is constructed with 15 sliding tiles, each with a number from 1 to 15 on it, and all packed into a 4 by 4 frame with one tile missing. Let's call the missing tile 'x'; the object of the puzzle is to arrange the tiles so that they are ordered as:
where the only legal operation is to exchange 'x' with one of the tiles with which it shares an edge. As an example, the following sequence of moves solves a slightly scrambled puzzle:
The letters in the previous row indicate which neighbor of the 'x' tile is swapped with the 'x' tile at each step; legal values are 'r','l','u' and 'd', for right, left, up, and down, respectively.
Not all puzzles can be solved; in 1870, a man named Sam Loyd was famous for distributing an unsolvable version of the puzzle, and
frustrating many people. In fact, all you have to do to make a regular puzzle into an unsolvable one is to swap two tiles (not counting the missing 'x' tile, of course).
In this problem, you will write a program for solving the less well-known 8-puzzle, composed of tiles on a three by three
arrangement.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 x
where the only legal operation is to exchange 'x' with one of the tiles with which it shares an edge. As an example, the following sequence of moves solves a slightly scrambled puzzle:
1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 5 6 7 8 5 6 7 8 5 6 7 8 5 6 7 8 9 x 10 12 9 10 x 12 9 10 11 12 9 10 11 12 13 14 11 15 13 14 11 15 13 14 x 15 13 14 15 x r-> d-> r->
The letters in the previous row indicate which neighbor of the 'x' tile is swapped with the 'x' tile at each step; legal values are 'r','l','u' and 'd', for right, left, up, and down, respectively.
Not all puzzles can be solved; in 1870, a man named Sam Loyd was famous for distributing an unsolvable version of the puzzle, and
frustrating many people. In fact, all you have to do to make a regular puzzle into an unsolvable one is to swap two tiles (not counting the missing 'x' tile, of course).
In this problem, you will write a program for solving the less well-known 8-puzzle, composed of tiles on a three by three
arrangement.
Input
You will receive a description of a configuration of the 8 puzzle. The description is just a list of the tiles in their initial positions, with the rows listed from top to bottom, and the tiles listed from left to right within a row, where the tiles are represented by numbers 1 to 8, plus 'x'. For example, this puzzle
is described by this list:
1 2 3 x 4 6 7 5 8
is described by this list:
1 2 3 x 4 6 7 5 8
Output
You will print to standard output either the word ``unsolvable'', if the puzzle has no solution, or a string consisting entirely of the letters 'r', 'l', 'u' and 'd' that describes a series of moves that produce a solution. The string should include no spaces and start at the beginning of the line.
Sample Input
2 3 4 1 5 x 7 6 8
Sample Output
ullddrurdllurdruldr
Source Code
/*
双向BFS, 从初始状态和目标状态同时开始搜索,当遇到有已经被搜索过的状态时停止
双向BFS优化了单次查询时间,但如果多次查询效率没有反向搜索好(因为目标状态固定)
*/
#include <iostream>
#include <queue>
using namespace std;
#define Max 365000 // 9! = 362880
//x,y记录当前X所在位置,state保存当前排列状态
//cantorValue记录当前状态对应的康托展开值
struct node {
int x, y;
char state[10];
int cantorValue;
};
//searched保存当前状态是否出现过
//parent保存前一状态
//opration保存前一状态到当前状的操作(上下左右)
int searched[Max], parent[Max], opration[Max];
int index;
//Q负责初始状态,Q1负责目标状态
queue<node> Q, Q1;
node Next, Top;
int dir[4][2] = { 1,0, -1,0, 0,1, 0,-1};
/*
康托展开:X=a[n]*(n-1)!+a[n-1]*(n-2)!+...+a[i]*(i-1)!+...+a[1]*0!
例:3 5 7 4 1 2 9 6 8 展开为 98884。因为X=2*8!+3*7!+4*6!+2*5!+0*4!+0*3!+2*2!+0*1!+0*0!=98884.
解释:
排列的第一位是3,3以后的序列中比3小的数有2个1,2,以这样的数开始的排列有8!个,因此第一项为2*8!
排列的第二位是5,5以后的序列中比5小的数有3个4、1、2,这样的排列有7!个,因此第二项为3*7!
…………
以此类推,直至0*0!
*/
int Cantor(char * buf, int len) {
//bit记录之后的序列中比当前位小的值的个数
int cantor[9] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320};
int i, j, bit;
int result = 0;
for (i = 0; i < len; i ++) {
bit = 0;
for (j = i + 1; j < len; j++) {
if (buf[i] > buf[j]) {
bit ++;
}
}
result += (bit * cantor[len - i - 1]);
}
return result;
}
//判断是否越界
bool isInside(int x, int y) {
if (x < 0 || y < 0 || x > 2 || y > 2) {
return false;
}
return true;
}
bool BFS(node start, node end) {
int i, j, nextX, nextY;
int a, b, step[30];
char temp;
while (!Q.empty()) {
Q.pop();
}
while (!Q1.empty()) {
Q1.pop();
}
Q.push(start);
Q1.push(end);
while (!Q.empty() || !Q1.empty()) {
//初始状态开始的BFS
if (!Q.empty()) {
Top = Q.front();
Q.pop();
for (i = 0; i < 4; i++) {
nextX = Top.x + dir[i][0];
nextY = Top.y + dir[i][1];
if (!isInside(nextX, nextY)) {
continue;
}
//下一个状态
Next.x = nextX;
Next.y = nextY;
for (j = 0; j < 9; j ++) {
Next.state[j] = Top.state[j];
}
a = Next.x * 3 + Next.y;
b = Top.x * 3 + Top.y;
temp = Next.state[a];
Next.state[a] = Next.state[b];
Next.state[b] = temp;
Next.cantorValue = Cantor(Next.state, 9);
if (!searched[Next.cantorValue]) {
searched[Next.cantorValue] = 1;
parent[Next.cantorValue] = Top.cantorValue;
opration[Next.cantorValue] = i;
Q.push(Next);
} else if (searched[Next.cantorValue] == 2) {
int len = 0;
//top到next的这一步
step[len ++] = i;
int cur = Top.cantorValue;
while (parent[cur] != -1) {
step[len ++] = opration[cur];
cur = parent[cur];
}
for (j = len - 1; j >= 0; j--) {
switch (step[j])
{
case 0:
printf("d");
break;
case 1:
printf("u");
break;
case 2:
printf("r");
break;
case 3:
printf("l");
break;
}
}
cur = Next.cantorValue;
while (parent[cur] != -1) {
switch (opration[cur])
{
case 0:
printf("u");
break;
case 1:
printf("d");
break;
case 2:
printf("l");
break;
case 3:
printf("r");
break;
}
cur = parent[cur];
}
printf("\n");
return true;
}
}
}
//目标状态开始的BFS
if (!Q1.empty()) {
Top = Q1.front();
Q1.pop();
for (i = 0; i < 4; i++) {
nextX = Top.x + dir[i][0];
nextY = Top.y + dir[i][1];
if (!isInside(nextX, nextY)) {
continue;
}
//下一个状态
Next.x = nextX;
Next.y = nextY;
for (j = 0; j < 9; j ++) {
Next.state[j] = Top.state[j];
}
a = Next.x * 3 + Next.y;
b = Top.x * 3 + Top.y;
temp = Next.state[a];
Next.state[a] = Next.state[b];
Next.state[b] = temp;
Next.cantorValue = Cantor(Next.state, 9);
if (!searched[Next.cantorValue]) {
searched[Next.cantorValue] = 2;
parent[Next.cantorValue] = Top.cantorValue;
opration[Next.cantorValue] = i;
Q1.push(Next);
} else if (searched[Next.cantorValue] == 1) {
int len = 0;
int cur = Next.cantorValue;
while (parent[cur] != -1) {
step[len ++] = opration[cur];
cur = parent[cur];
}
for (j = len - 1; j >= 0; j--) {
switch (step[j])
{
case 0:
printf("d");
break;
case 1:
printf("u");
break;
case 2:
printf("r");
break;
case 3:
printf("l");
break;
}
}
//top到next的这一步
switch (i)
{
case 0:
printf("u");
break;
case 1:
printf("d");
break;
case 2:
printf("l");
break;
case 3:
printf("r");
break;
}
cur = Top.cantorValue;
while (parent[cur] != -1) {
switch (opration[cur])
{
case 0:
printf("u");
break;
case 1:
printf("d");
break;
case 2:
printf("l");
break;
case 3:
printf("r");
break;
}
cur = parent[cur];
}
printf("\n");
return true;
}
}
}
}
return false;
}
int main() {
int i;
char init[10];
while (cin >> init[0]) {
memset(searched, 0, sizeof(searched));
node start, end;
if (init[0] == 'x') {
init[0] = '0' + 9;
start.x = 0;
start.y = 0;
}
for (i = 1; i < 9; i++) {
cin >> init[i];
if (init[i] == 'x') {
init[i] = '0' + 9;
start.x = i / 3;
start.y = i % 3;
}
}
for (i = 0; i < 9; i++) {
start.state[i] = init[i];
}
start.cantorValue = Cantor(init, 9);
searched[start.cantorValue] = 1;
parent[start.cantorValue] = -1;
opration[start.cantorValue] = -1;
for (i = 0; i < 9; i++) {
end.state[i] = '0' + i + 1;
}
end.x = 2;
end.y = 2;
end.cantorValue = 0;
searched[end.cantorValue] = 2;
parent[end.cantorValue] = -1;
opration[end.cantorValue] = -1;
if(!BFS(start, end)) {
printf("unsolvable\n");
}
}
return 0;
}