链接:http://poj.org/problem?id=1077
题目:
Language:
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 |
解题思路:
这是一个经典的8数码问题,对于这个问题,我们可以用搜索的方法来解决。用bfs()来找一条最短的可行路。对于这个问题,我们有几点要注意一下:1.这个问题的状态是9个数字(我们把x看成0)的一个排列,每种排列都代表一种状态,用bfs()搜索,就是从当前的一个排列,转移到另一个可行的排列,我们可以用一个结构体来表示这种状态;2.说到bfs(),我们自然就会想到vis[]数组,标志当前的一个状态是否有访问过,防止重复访问,算是剪枝吧,对于这个问题,一个状态有9个数字,最直接的想法是开一个9维的数组,这样显然是不太可行的,会爆内存,要解决这个问题,我们可以用hash的方法,将9个数字表示的一个状态映射成一个数字,这样一共就只有9!种不同的状态了,对于这个hash,有一种比较经典的做法,康拓展开,大意是这样的,hash = a * 8!+ b * 7! + c * 6! + d * 5! + e * 4! + f * 3!+ g * 2! + h * 1! + i * 0!;其中a表示的是,从第一个数字开始往后,比第一个数字小的数的个数,b表示,从第二个数字开始往后,比第二个数字小的数的个数,依次类推;3.要记录当前的路径,我们可以开一个全局数组path,一个全局数组pre,分别记录,到当前状态hash值的路径,以及前一个状态的hash值;4.还有一点,最好用G++编译器提交,用C++会超时,估计是因为G++对stl有优化。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
struct P
{
int step;
char c;
int a[9];
};
P state, dest;
char dir[5] = "udlr";
bool vis[362888];
int pre[362888];
char path[362888];
char ans[362888];
int dx[4] = {-1, 1, 0, 0};
int dy[4] = { 0, 0, -1, 1};
int f[10], step, h;
bool fun(P a, P b)
{
for(int i = 0; i < 9; i++)
{
if(a.a[i] != b.a[i]) return false;
}
return true;
}
int hash(P p)
{
int ret = 0;
for(int i = 0; i < 9; i++)
{
int num = 0;
for(int j = i + 1; j < 9; j++)
{
if(p.a[j] < p.a[i]) num++;
}
ret += f[8-i] * num;
}
return ret;
}
int bfs()
{
memset(vis, false, sizeof(vis));
queue<P> q;
q.push(state);
while(!q.empty())
{
P src = q.front(); q.pop();
if(vis[hash(src)]) continue;
vis[hash(src)] = true;
int x, y, z, t;
for(int i = 0; i < 9; i++)
{
if(0 == src.a[i])
{
t = i;
break;
}
}
x = t / 3; y = t % 3;
for(int i = 0; i < 4; i++)
{
int tx = x + dx[i], ty = y + dy[i];
if(tx >= 0 && tx <= 2 && ty >= 0 && ty <= 2)
{
P p;
for(int j = 0; j < 9; j++)
{
p.a[j] = src.a[j];
}
z = tx * 3 + ty;
swap(p.a[t], p.a[z]);
if(vis[hash(p)]) continue;
p.step = src.step + 1;
pre[hash(p)] = hash(src);
path[hash(p)] = dir[i];
if(fun(p, dest))
{
step = p.step;
h = hash(p);
return 1;
}
q.push(p);
}
}
}
return 0;
}
int main()
{
for(int i = 0; i < 8; i++) dest.a[i] = i + 1;
dest.a[8] = 0;
f[0] = 1;
for(int i = 1; i < 10; i++) f[i] = i * f[i-1];
char c; int i = 0;
while(c = getchar())
{
if(c >= '1' && c <= '8')
{
state.a[i++] = c - '0';
}
if('x' == c)
{
state.a[i++] = 0;
}
if(9 == i) break;
}
state.step = 0;
if(bfs())
{
for(int i = 0; i < step; i++)
{
ans[i] = path[h];
h = pre[h];
}
for(int i = step - 1; i >= 0; i--)
{
printf("%c", ans[i]);
}
puts("");
}
else
{
puts("unsolvable");
}
return 0;
}