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:
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
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
解题思路:利用BFS与康托展开状态判重进行最优解的寻找,同时记录路径。
AC代码:
#include <stdio.h>
#include <string.h>
struct node {
int op;
int state[9];
int parent;
int dis;
};
struct node queue[362880];
//一些阶乘常数
long long factory[] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880};
int vis[362880];
int dir[4][2] = {{1, 0}, {-1, 0}, {0, -1}, {0, 1}}; // r l u d
int operation[362880];
int start[9], goal[9] = {1, 2, 3, 4, 5, 6, 7, 8, 0};
int Cantor(int str[], int n) {
long long res = 0;
for (int i = 0; i < n; i++) {
int counted = 0;
for (int j = i + 1; j < n; j++) {
if (str[i] > str[j])
++counted;
}
res += counted * factory[n - i - 1];
}
if (!vis[res]) {
vis[res] = 1;
return 1;
} else {
return 0;
}
}
void print_way(int n) {
int top = 0;
operation[top] = n;
while (queue[operation[top]].parent != -1) {
top++;
operation[top] = queue[operation[top - 1]].parent;
}
for (int i = top; i >= 0; i--) {
switch (queue[operation[i]].op) {
case 0: {
printf("r");
}
break;
case 1: {
printf("l");
}
break;
case 2: {
printf("u");
}
break;
case 3: {
printf("d");
}
break;
}
}
}
int bfs() {
int front = 0, rear = 1;
memcpy(queue[front].state, start, sizeof(queue[front].state));
queue[front].dis = 0;
queue[front].op = -1;
queue[front].parent = -1;
int x, y, z;
while (rear - front) {
struct node head = queue[front];
for (int i = 0; i < 9; i++) {
if (head.state[i] == 0) z = i;
}
x = z % 3;
y = z / 3;
int newx, newy, newz;
if (!memcmp(head.state, goal, sizeof(goal))) {
print_way(front);
return 0;
}
for (int i = 0; i < 4; i++) {
newx = x + dir[i][0];
newy = y + dir[i][1];
newz = newy * 3 + newx;
if (newx >= 0 && newx < 3 && newy >= 0 && newy < 3) {
int temp_;
struct node temp;
memcpy(temp.state, head.state, sizeof(temp.state));
temp_ = temp.state[z];
temp.state[z] = temp.state[newz];
temp.state[newz] = temp_;
temp.dis = head.dis + 1;
temp.op = i;
temp.parent = front;
if (Cantor(temp.state,9)) {
queue[rear++] = temp;
}
}
}
front++;
}
printf("unsolvable\n");
return -1;
}
int main() {
char ch;
for (int i = 0; i < 9; i++) {
scanf("%c", &ch);
if (ch == ' ') {
i--;
continue;
}
if (ch == 'x') start[i] = 0;
else if (ch != ' ') start[i] = ch - '0';
}
bfs();
}