Eight
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 20993 Accepted Submission(s): 5634Special Judge
Problem 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 xwhere 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, several descriptions of configuration of the 8 puzzle. One 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. Do not print a blank line between cases.
Sample Input
Sample Output
//就是类似九宫格那个游戏,不过这里9个格子大小都相等
//学了比较多的东西,才懂怎么做
这里我用的的是 A*+逆序数剪枝+hash判重 做的
A*其实也好理解,就是 bfs 升级版
这个博客写的很详细
因为,无论怎么移动,逆序数的奇偶性是不变的,所以用这个能剪枝
最大的问题就是怎么判重了,最多不过 9!种情况么,362880 ,每种情况对应一个数字,用一个 vis[ ]就能判重了,然后hash 判重就好理解了
还有许多方法,推荐一篇博客 有兴趣可以看看
A* + hash 判重 + 曼哈顿距离
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 #include2 #include 3 #include 4 #include 5 #include 6 #include 7 #include 8 #include
IDA* + 曼哈顿距离 这个空间就省下来了,改一下,十五数码也能做的,上一个就不行了,内存爆炸
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 #include2 #include 3 #include 4 #include 5 6 #define size 3 //这里规定几数码 7 8 int move[4][2]={ {-1,0},{ 0,-1},{ 0,1},{ 1,0}};//上 左 右 下 置换顺序 9 char op[4]={ 'u','l','r','d'}; 10 11 int map[size][size],map2[size*size],limit,path[100]; 12 int flag,length; 13 14 // 十五数码 的表 15 //int goal[16][2]= { {size-1,size-1},{0,0},{0,1},{0,2}, 16 // {0,3},{1,0},{1,1},{1,2}, 17 // {1,3},{2,0},{2,1},{2,2}, 18 // {2,3},{3,0},{3,1},{3,2}};//各个数字应在位置对照表 19 20 // 八数码 的表 21 int goal[9][2]= { {size-1,size-1},{ 0,0},{ 0,1},{ 0,2}, 22 { 1,0},{ 1,1},{ 1,2}, 23 { 2,0},{ 2,1}}; //各个数字应在位置对照表 24 25 int nixu(int a[size*size]) 26 { 27 int i,j,ni,w,x,y; //w代表0的位置 下标,x y 代表0的数组坐标 28 ni=0; 29 for(i=0;i a[j]) 38 ni++; 39 } 40 } 41 //x=w/size; 42 //y=w%size; 43 //ni+=abs(x-(size-1))+abs(y-(size-1)); //最后加上0的偏移量 44 if(ni%2==0) 45 return 1; 46 else 47 return 0; 48 } 49 50 int hv(int a[][size])//估价函数,曼哈顿距离,小等于实际总步数 51 { 52 int i,j,cost=0; 53 for(i=0;i 0)//不和上一次移动方向相反,对第二步以后而言105 continue;106 nx=sx+move[i][0]; //移动的四步 上左右下107 ny=sy+move[i][1];108 109 if( 0<=nx && nx