http://poj.org/problem?id=1077
题目大意:
将分别标有数字1,2,3,…,8的八块正方形数码牌任意地放在一块3×3的数码盘上。放牌时要求不能重叠。于是,在3×3的数码盘
上出现了一个空格。现在要求按照每次只能将与空格相邻的数码牌与空格交换的原则,将任意摆放的数码盘摆成如下的状态。
1 2 3
4 5 6
7 8 x
如下图表示了一个具体的八数码问题求解。
1 x 3
4 2 5
7 8 6
d
1 2 3
4 x 5
7 8 6
r
1 2 3
4 5 x
7 8 6
d
1 2 3
4 5 6
7 8 x
输出x块的移动方向
此题是special judge 任意输出一种可能解即可。
解题思路:
状态总数是9! = 362880 种,不算太多,可以满足广搜和A*对于空间的需求。
状态可以每次都动态生成,也可以生成一次存储起来,我用的动态生成,《组合数学》书上有一种生成排列的方法叫做"序数法",我
看了一会书,把由排列到序数,和由序数到排列的两个函数写了出来,就是代码中的int order(const char *s, int n) 和void
get_node(int num, node &tmp)两个函数。
启发函数,用的是除空格外的八个数字到正确位置的网格距离。
// A* #include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <queue> using namespace std; /* 把1..n的排列映射为数字 0..(n!-1) */ int fac[] = { 1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880 };//... int order(const char *s, int n) { int i, j, temp, num; num = 0; for (i = 0; i < n-1; i++) { temp = 0; for (j = i + 1; j < n; j++) { if (s[j] < s[i]) temp++; } num += fac[s[i] -1] * temp; } return num; } bool is_equal(const char *b1, const char *b2){ for(int i=0; i<9; i++) if(b1[i] != b2[i]) return false; return true; } //hash struct node{ char board[9]; char space;//空格所在位置 }; const int TABLE_SIZE = 362880; int hash(const char *cur){ return order(cur, 9); } /* 整数映射成排列 */ void get_node(int num, node &tmp) { int n=9; int a[9]; //求逆序数 for (int i = 2; i <= n; ++i) { a[i - 1] = num % i; num = num / i; tmp.board[i - 1] = 0;//初始化 } tmp.board[0] = 0; int rn, i; for (int k = n; k >= 2; k--) { rn = 0; for (i = n - 1; i >= 0; --i) { if (tmp.board[i] != 0) continue; if (rn == a[k - 1]) break; ++rn; } tmp.board[i] = k; } for (i = 0; i < n; ++i) if (tmp.board[i] == 0) { tmp.board[i] = 1; break; } tmp.space = n - a[n-1] -1; } //启发函数: 除去x之外到目标的网格距离和 int goal_state[9][2] = {{0,0}, {0,1}, {0,2}, {1,0}, {1,1}, {1,2}, {2,0}, {2,1}, {2,2}}; int h(const char *board){ int k; int hv = 0; for(int i=0; i<3; ++i) for(int j=0; j<3; ++j){ k = i*3+j; if(board[k] != 9){ hv += abs(i - goal_state[board[k]-1][0]) + abs(j - goal_state[board[k] -1][1]); } } return hv; } int f[TABLE_SIZE], d[TABLE_SIZE];//估计函数和深度 //优先队列的比较对象 struct cmp{ bool operator () (int u, int v){ return f[u] > f[v]; } }; char color[TABLE_SIZE];//0, 未访问;1, 在队列中,2, closed int parent[TABLE_SIZE]; char move[TABLE_SIZE]; int step[4][2] = {{-1, 0},{1, 0}, {0, -1}, {0, 1}};//u, d, l, r void A_star(const node & start){ int x, y, k, a, b; int u, v; priority_queue<int, vector<int>, cmp> open; memset(color, 0, sizeof(char) * TABLE_SIZE); u = hash(start.board); parent[u] = -1; d[u] = 0; f[u] = h(start.board); open.push(u); color[u] = 1; node tmp, cur; while(!open.empty()){ u = open.top(); if(u == 0) return; open.pop(); get_node(u, cur); k = cur.space; x = k / 3; y = k % 3; for(int i=0; i<4; ++i){ a = x + step[i][0]; b = y + step[i][1]; if(0<=a && a<=2 && 0<=b && b<=2){ tmp = cur; tmp.space = a*3 + b; swap(tmp.board[k], tmp.board[tmp.space]); v = hash(tmp.board); if(color[v] == 1 && (d[u] + 1) < d[v]){//v in open move[v] = i; f[v] = f[v] - d[v] + d[u] + 1;//h[v]已经求过 d[v] = d[u] + 1; parent[v] = u; //直接插入新值, 有冗余,但不会错 open.push(v); } else if(color[v] == 2 && (d[u]+1)<d[v]){//v in closed move[v] = i; f[v] = f[v] - d[v] + d[u] + 1;//h[v]已经求过 d[v] = d[u] + 1; parent[v] = u; open.push(v); color[v] = 1; } else if(color[v] == 0){ move[v] = i; d[v] = d[u] + 1; f[v] = d[v] + h(tmp.board); parent[v] = u; open.push(v); color[v] = 1; } } } color[u] = 2;// } } void print_path(){ int n, u; char path[1000]; n = 1; path[0] = move[0]; u = parent[0]; while(parent[u] != -1){ path[n] = move[u]; ++n; u = parent[u]; } for(int i=n-1; i>=0; --i){ if(path[i] == 0) printf("u"); else if(path[i] == 1) printf("d"); else if(path[i] == 2) printf("l"); else printf("r"); } } int main(){ node start; char c; for(int i=0; i<9; ++i){ cin>>c; if(c == 'x'){ start.board[i] = 9; start.space = i; } else start.board[i] = c - '0'; } A_star(start); if(color[0] != 0) print_path(); else printf("unsolvable"); return 0; }
#include <iostream> #include <cstdio> #include <cstdlib> #define SIZE 3 using namespace std; char board[SIZE][SIZE]; //启发函数: 除去x之外到目标的网格距离和 int goal_state[9][2] = {{0,0}, {0,1}, {0,2}, {1,0}, {1,1}, {1,2}, {2,0}, {2,1}, {2,2}}; int h(char board[][SIZE]){ int cost = 0; for(int i=0; i<SIZE; ++i) for(int j=0; j<SIZE; ++j){ if(board[i][j] != SIZE*SIZE){ cost += abs(i - goal_state[board[i][j]-1][0]) + abs(j - goal_state[board[i][j]-1][1]); } } return cost; } int step[4][2] = {{-1, 0}, {0, -1}, {0, 1}, {1, 0}};//u, l, r, d char op[4] = {'u', 'l', 'r', 'd'}; char solution[1000]; int bound; //上界 bool ans; //是否找到答案 int DFS(int x, int y, int dv, char pre_move){// 返回next_bound int hv = h(board); if(hv + dv > bound) return dv + hv; if(hv == 0){ ans = true; return dv; } int next_bound = 1e9; for(int i=0; i<4; ++i){ if(i + pre_move == 3)//与上一步相反的移动 continue; int nx = x + step[i][0]; int ny = y + step[i][1]; if(0<=nx && nx<SIZE && 0<=ny && ny<SIZE){ solution[dv] = i; swap(board[x][y], board[nx][ny]); int new_bound = DFS(nx, ny, dv+1, i); if(ans) return new_bound; next_bound = min(next_bound, new_bound); swap(board[x][y], board[nx][ny]); } } return next_bound; } void IDA_star(int sx, int sy){ ans = false; bound = h(board);//初始代价 while(!ans && bound <= 100)//上限 bound = DFS(sx, sy, 0, -10); } int main(){ int sx, sy;//起始位置 char c; for(int i=0; i<SIZE; ++i) for(int j=0; j<SIZE; ++j){ cin>>c; if(c == 'x'){ board[i][j] = SIZE * SIZE; sx = i; sy = j; } else board[i][j] = c - '0'; } IDA_star(sx, sy); if(ans){ for(int i=0; i<bound; ++i) cout<<op[solution[i]]; } else cout<<"unsolvable"; return 0; }