POJ1077&&HDU1043(八数码,IDA*+曼哈顿距离)
2016-08-25 22:20
190 查看
Eight
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 30127 | Accepted: 13108 | Special Judge |
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 puzzle1 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
//2016.8.25 #include<iostream> #include<cstdio> #include<algorithm> #include<map> using namespace std; int a[10][10], b[10], d[1000], sx, sy, deep; bool ok; map<int, bool> vis; char dir[4] = {'u', 'd', 'l', 'r'}; int dx[4] = {-1, 1, 0, 0};//分别对应上下左右四个方向 int dy[4] = {0, 0, -1, 1}; bool solve()//求逆序对判断是否有解,偶数有解,奇数无解 { int cnt = 0; for(int i = 1; i <= 9; i++) for(int j = 0; j < i; j++) if(b[i] && b[j]>b[i]) cnt++; return !(cnt%2); } int Astar()//启发函数,假设每个数字可以从别的数字头上跨过去,计算到达自己应该到的位置所需要的步数,即计算该数到达其正确位置的曼哈顿距离,h为各点曼哈顿距离之和 { int h = 0; for(int i = 1; i <= 3; i++) for(int j = 1; j <= 3; j++) if(a[i][j]!=0) { int nx = (a[i][j]-1)/3; int ny = (a[i][j]-1)%3; h += (abs(i-nx-1)+abs(j-ny-1)); } return h; } int toInt()//把矩阵转换为int型数字 { int res = 0; for(int i = 1; i <= 3; i++) for(int j = 1; j <= 3; j++) res = res*10+a[i][j]; return res; } void IDAstar(int x, int y, int step) { if(ok)return ; int h = Astar(); if(!h && toInt()==123456780)//找到答案 { for(int i = 0; i < step; i++) cout<<dir[d[i]]; cout<<endl; ok = 1; return ; } if(step+h>deep)return ;//现实+理想<现状,则返回,IDA*最重要的剪枝 int now = toInt(); if(vis[now])return ;//如果状态已经搜过了,剪枝,避免重复搜索 vis[now] = true; for(int i = 0; i < 4; i++) { int nx = x+dx[i]; int ny = y+dy[i]; if(nx>=1&&nx<=3&&ny>=1&&ny<=3) { d[step] = i; swap(a[x][y], a[nx][ny]); IDAstar(nx, ny, step+1); swap(a[x][y], a[nx][ny]); d[step] = 0; } } return; } int main() { char ch; while(cin >> ch) { ok = false; deep = 0; int cnt = 0; for(int i = 1; i <= 3; i++) { for(int j = 1; j <= 3; j++) { if(i==1&&j==1); else cin >> ch; if(ch == 'x') { a[i][j] = 0; sx = i; sy = j; }else a[i][j] = ch - '0'; b[cnt++] = a[i][j]; } } if(!solve()) { cout<<"unsolvable"<<endl; continue; } while(!ok) { vis.clear(); IDAstar(sx, sy, 0); deep++;//一层一层增加搜索的深度 } } return 0; }
相关文章推荐
- 【hdu1043 && poj 1077】八数码问题
- hdu 1034 & poj 1077 Eight 传说中的八数码问题。真是一道神题,A*算法+康托展开
- UVA 716(Commedia dell' arte-三维八数码)
- poj 2893 M × N Puzzle(八数码可行解)树状数组求解
- 求人不如求自之笔记本键盘维修实录 [IT·游戏频道 南方网] 享受精彩数码生活 劲爆游戏一网打尽
- HDU1043 eight 八数码问题
- 八数码与IDA*问题 HDU1043&&POJ1077
- HDU1043BFS 康托展开 八数码
- ZJU2004 Commedia dell'arte - 八数码问题有解的条件及其推广
- hdu1043 Eight (八数码问题,多种解法)
- 关于八数码问题中的状态判重的三种解决方法(编码、hash、<set>)
- wikioi 1225八数码难题
- CODEVS 1225 八数码难题(BFS && A*)
- hdu1043 经典的八数码问题 逆向bfs打表 + 逆序数
- 关于八数码问题中的状态判重的三种解决方法(编码、hash、<set>)
- 久邦数码Android开发工程师(3G&GO桌面)2014年3月份笔试-初面
- 2007年中国IT&数码网站市场规模达到4.1亿元
- POJ1077 HDU1043 Eight 八数码 (A*+康托展开)
- 七数码&平分石子
- 八数码问题有解条件&推广N×N,N×N×N