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:
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 |
——————————————————沉默的分割线————————————————————
思路:参见另一篇博文八数码问题。
刚研究了白书的八数码问题,找到一道题目拿来练手,这个题目需要稍微修改一下,就可以过了。首先是输入格式,字符串,那么用char s[ ][2]转换成整数。其次是打印路径。还记得很久以前的一篇博文(BFS模板):Hero in maze(拓展)吗。「表说论家不厚道撒,以前写过了」(因为结点数不会超过9!,所以数组只开了400000)
代码如下:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
const int N = 400000, HN = 400003;
int head[HN], next[N];
int st[N][9], goal[9] = {1, 2, 3, 4, 5, 6, 7, 8, 0};
int fa[N], la_dir[N], dir[N];
char name[4] = {'u','r','d','l'};
const int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
int Hash(int *st) {
int v = 0;
for(int i = 0; i < 9; i++)
v = v*10 + st[i];
return v % HN;
}
bool try_insert(int rear) {
int h = Hash(st[rear]);
int u = head[h];
while(u) {
if(!memcmp(st[u], st[rear], sizeof(st[0])))
return 0;
u = next[u];
}
next[rear] = head[h];
head[h] = rear;
return 1;
}
int bfs() {
memset(head, 0, sizeof(head));
int fron = 1, rear = 2;
fa[fron] = 1;//第一张图的父结点是自己
while (fron < rear) {
if (!memcmp(goal, st[fron], sizeof(st[0])))
return fron;
int z;
for(z = 0; z < 9; z++)
if(!st[fron][z])
break;
int x = z / 3, y = z % 3;
for(int d = 0; d < 4; d++) {
int nx = x + dx[d], ny = y + dy[d], nz = 3*nx + ny;
if(nx >= 0&&nx < 3&&ny >= 0&&ny < 3) {
memcpy(&st[rear], &st[fron], sizeof(st[0]));
st[rear][nz] = st[fron][z];
st[rear][z] = st[fron][nz];
fa[rear] = fron;//此图的父结点是第fron张图
la_dir[rear] = d;//保存走到这个点的方向d
if(try_insert(rear))
rear++;
}
}
fron++;
}
return 0;
}
void PR(int pic) {//依然是倒走迷宫
int c = 0;
for(;;) {
int fz = fa[pic];
if(fz == pic)
break;
dir[c++] = la_dir[pic];
pic = fz;
}
while(c--)
printf("%c", name[dir[c]]);
putchar('\n');
}
int main() {
char s[9][2];
for(int i = 0; i < 9; i++) {
scanf("%s", s[i]);
if(s[i][0] > '0'&&s[i][0] < '9')
st[1][i] = s[i][0] - '0';
else
st[1][i] = 0;
}
int ans = bfs();
if(ans > 0)
PR(ans);
else
puts("unsolvable");
return 0;
}