原题链接 http://poj.org/problem?id=1077
Description
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
1 2 3 x 4 6 7 5 8
is described by this list:
1 2 3 x 4 6 7 5 8
Output
Sample Input
2 3 4 1 5 x 7 6 8
Sample Output
ullddrurdllurdruldr
A*算法
一般地,节点n的评估函数f(n)可表示如下
f(n) = h(n) + d(n)
h(n) 是 启发式函数,与启发信息相关。启发函数h(n)的设计非常重要,它无固定的设计模式,主要取决于面向具体问题结构的知识和求解的技巧。d(n)是开始节点到节点n的深度信息。
A*算法最为核心的部分,就在评估函数的设计。在A*搜索算法的评估函数中,d(n)表示从起始节点到当前节点的代价,通常用当前节点在搜索树中的深度表示。启发式函数h(n)表示当前节点到目标节点的评估值,是启发式搜索算法最为关键的部分。
A*搜索算法的评估函数包括两个约束条件:
(1) h(n)不大于节点n到目标节点t的实际最小代价h*(n),即:h(n) <= h*(n)。
(2) 任意节点的评估值f必须不小于父节点的f值,即f单调递增(如果存在小于的情况,即子结点f值小于父结 点,则从父结点经过子结点再回到父结点,父结点f值会变小……搜索结果会形成一个循环)。
满足这两个条件,可以证明A*能够获得问题的最优解。简单证明一下,选择节点x,节点x一定是可扩展节点中的具有最小估计值的节点,如果x是目标节点,则其估计值f(x)就是实际最小代价,不大于其他节点的估计值,又条件(1),所以f(x)不大于其它任意节点的实际最小代价,即为最优解。
一般地,A*搜索算法可以描述如下:
A* ( )
open ← {s};
closed ← Ø
while open != Ø do
remove leftmost state from open, call it x
if x is a goal then return success
else
generate children of x
for each child c do
if c is already on open list then
if c was reached by a shorter path then
update f(c)
else if c is already on closed list then
if c was reached by a shorter path then
remove c from closed list
add c to open list and update f(c)
else
assign c a heuristic value f(c)
add c to open list
put x on closed list and reorder states on open by f
return failure
(注:伪代码中如果扩展结点在closed表中,会比较f值的大小,但是f本身是非递减,从起点开始扩展的点大于等于起点的f值,而后每次又从open去取f最小的值进行扩展,所以 closed中存储的都是当前状态f值最小的点,不会出现所谓的shorter path;等价于如果扩展结点在closed列表,直接continued)
A*算法与BFS:可以这样说,BFS是A*算法的一个特例。对于一个BFS算法,从当前节点扩展出来的每一个节点(如果没有被访问过的话)都要放进队列进行进一步扩展。也就是说BFS的估计函数h永远等于0,没有一点启发式的信息,可以认为BFS是“最烂的”A*算法。而A*搜索算法采用最小代价优先的策略,即在所有将扩展的节点中,选择具有最小代价的节点作为下一个扩展节点。其终止准则是如果选择的节点是目标节点,即停止A*搜索算法。A*()算法中open表是一个优先队列,保存即将要展开的节点。closed表是一个数据结构,例如数组或者链表,保存展开过的节点。s是开始节点。open表可用最小堆实现。(不能在加入open表之前判断是否是目标节点并返回,这样不能保证是由最优路径到达目标节点的)
Source Code
#include <iostream>
#include <queue>
using namespace std;
#define Max 365000 // 9! = 362880
struct node {
int x, y;
char state[10];
int cantorValue;
int f;
friend bool operator<(node a,node b) {
return a.f > b.f;
}
};
char state[Max]; // 0:未查询 1:在open列表中 2:在closed列表中
int h[Max]; //估价函数,这里用曼哈顿距离
int depth[Max]; //深度
int parent[Max], opration[Max];
int index;
priority_queue<node> Q;
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;
}
//计算曼哈顿距离(只计算1~8,确保每次移动h(n)最多减少1,而g(n)会增加1,这样f = g + h为非递减序列)
int goal_state[9][2] = {{0,0}, {0,1}, {0,2}, {1,0}, {1,1}, {1,2}, {2,0}, {2,1}, {2,2}};
int manhattan(const char *board){
int i, x, y, num;
int dis = 0;
for (i = 0; i < 9; i++) {
x = i / 3;
y = i % 3;
num = board[i] - '0';
if (num != 9) {
dis += (abs(x - goal_state[num - 1][0]) + abs(y - goal_state[num - 1][1]));
}
}
return dis;
}
//输出路径
void print_opration() {
int step[50];
int len, cur, i;
len = cur = 0;
while (parent[cur] != -1) {
step[len ++] = opration[cur];
cur = parent[cur];
}
for (i = len - 1; i >= 0; i--) {
switch (step[i]){
case 0:
printf("d");
break;
case 1:
printf("u");
break;
case 2:
printf("r");
break;
case 3:
printf("l");
break;
}
}
printf("\n");
}
bool A_star(node start) {
int i, j, nextX, nextY;
int a, b;
char temp;
while (!Q.empty()) {
Q.pop();
}
Q.push(start);
while (!Q.empty()) {
Top = Q.top();
Q.pop();
if (Top.cantorValue == 0) {
print_opration();
return true;
}
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 (state[Next.cantorValue] == 0) { //不在open也在不closed中,插入open
parent[Next.cantorValue] = Top.cantorValue;
opration[Next.cantorValue] = i;
h[Next.cantorValue] = manhattan(Next.state);
depth[Next.cantorValue] = depth[Top.cantorValue] + 1;
Next.f = depth[Next.cantorValue] + h[Next.cantorValue];
Q.push(Next);
state[Next.cantorValue] = 1;
}
//在open列表中(说明h[]即曼哈顿距离肯定已经计算过),并且新的f值小于原先的f, 因为f = g + h,而h不变,所以只需要比较g的大小
else if (state[Next.cantorValue] == 1 && (depth[Top.cantorValue] + 1 < depth[Next.cantorValue])) {
parent[Next.cantorValue] = Top.cantorValue;
opration[Next.cantorValue] = i;
depth[Next.cantorValue] = depth[Top.cantorValue] + 1;
Next.f = depth[Next.cantorValue] + h[Next.cantorValue];
Q.push(Next); //直接插入新值, 有冗余,但不会错
}
//在closed列表中,比较两个X的估价值,注意是同一个节点的两个不同路径的估价值
else if (state[Next.cantorValue] == 2 && (depth[Top.cantorValue] + 1 < depth[Next.cantorValue])) {
/* parent[Next.cantorValue] = Top.cantorValue;
opration[Next.cantorValue] = i;
depth[Next.cantorValue] = depth[Top.cantorValue] + 1;
Next.f = depth[Next.cantorValue] + h[Next.cantorValue];
Q.push(Next);
state[Next.cantorValue] = 1;
*/
continue;
}
}
state[Top.cantorValue] = 2;
}
return false;
}
int main() {
int i;
char init[10];
index = 0;
memset(h, -1, sizeof(h));
memset(state, 0, sizeof(state));
node start;
for (i = 0; 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);
state[start.cantorValue] = 1;
depth[start.cantorValue] = 0;
h[start.cantorValue] = manhattan(start.state);
start.f = h[start.cantorValue] + depth[start.cantorValue];
parent[start.cantorValue] = -1;
opration[start.cantorValue] = -1;
if (!A_star(start)) {
printf("unsolvable\n");
}
return 0;
}