Eight
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 24351 | Accepted: 10773 | 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:
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:
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.
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
is described by this list:
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
看了两天了,真不容易啊,A*算法速度确实非常快,但是ACM很少用到,游戏中体现的作用很大
A*算法可参考文章(介绍的很好):http://www.cppblog.com/christanxw/archive/2006/04/07/5126.html
A*搜索,A*是一种启发式搜索,g为已花代价,h为估计的剩余代价,而A*是根据f=g+h作为估价函数进行排列,也就是优先选择可能最优的节点进行扩展。
对于八数码问题,以下几个问题需要知道
判断有无解问题:根据逆序数直接判断有无解,对于一个八数码,依次排列之后,每次是将空位和相邻位进行调换,研究后会发现,每次调换,逆序数增幅都为偶数,也就是不改变奇偶性,所以只需要根据初始和目标状态的逆序数正负判断即可。
HASH问题:根据的是康托展开,每一个10进制数字对应一种状态
以及估价函数H:是根据与目标解的曼哈顿距离,也就是每个数字与目标位置的曼哈顿距离之和。
以了以上的基础,便可以通过A*解决八数码问题。
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<queue> #include<vector> #include<cmath> #include<map> #include<string> using namespace std; struct Node{ int maze[3][3]; //八数码具体情况 int h,g; //两个估价函数 int x,y; //空位的位置 int Hash; //HASH值 bool operator<(const Node n1)const{ //优先队列第一关键字为h,第二关键字为g return h!=n1.h?h>n1.h:g>n1.g; //先取h为最小的,再找g为最小的 } }s,u,v,tt; int HASH[9]={1,1,2,6,24,120,720,5040,40320};//HASH的权值(0~8的康托展开,十进制数值每一位对应一个状态 int destination=322560; //目标情况的HASH值 int vis[400000]; //判断状态已遍历,初始为-1,否则为到达这步的转向(0,1,2,3表示向右,左,下,上) int pre[400000]; //路径保存,根据pre所表示的值父节点来从终点找起点 int way[4][2]={{0,1},{0,-1},{1,0},{-1,0}}; //四个方向(右,左,下,上) int get_hash(Node tmp){ //获得HASH值 int a[9],k=0; for(int i=0;i<3;i++) for(int j=0;j<3;j++) a[k++]=tmp.maze[i][j]; //把这时候的八数码状态全部存入一维数组a[k]中; int res=0; for(int i=0;i<9;i++){ int k=0; for(int j=0;j<i;j++)//当序列为1,2,3,4,5,6,7,8,0;0前面有8位,所以8*40320=322560)目标状态 if(a[j]>a[i]) k++; res+=HASH[i]*k; } return res; } bool isok(Node tmp){ //求出逆序对,判断是否有解 int a[9],k=0; for(int i=0;i<3;i++) for(int j=0;j<3;j++) a[k++]=tmp.maze[i][j]; //把这时候的八数码状态全部存入一维数组a[k]中; int sum=0; for(int i=0;i<9;i++) for(int j=i+1;j<9;j++) if(a[j]&&a[i]&&a[i]>a[j]) sum++; return !(sum&1); //由于目标解为偶数,所以状态的逆序数为偶数才可行 } /* 两种估价函数不一样的写法,但都是在当前状态下,所有的数字与当前位置只差的总和; 解法一: int get_h(Node tmp){ //获得估价函数H int ans=0; for(int i=0;i<3;i++) for(int j=0;j<3;j++) if(tmp.maze[i][j]) ans+=abs(i-(tmp.maze[i][j]-1)/3)+abs(j-(tmp.maze[i][j]-1)%3); return ans; } 解法二: */ int get_h(Node tmp){ //获得估价函数H int a[9],k=0; for(int i=0;i<3;i++) for(int j=0;j<3;j++) a[k++]=tmp.maze[i][j]; //把这时候的八数码状态全部存入一维数组a[k]中; int ans=0; for(int i=0;i<9;i++)ans+=abs(a[i]-1-i); //当前的值-当前的位置 return ans; } void astar(){ //搜索 priority_queue<Node>que; // 先取h为最小的,再找g为最小的 que.push(s); while(!que.empty()){ u=que.top(); que.pop(); for(int i=0;i<4;i++){ v=u; v.x+=way[i][0]; v.y+=way[i][1]; if(v.x>=0&&v.x<3&&v.y>=0&&v.y<3){ //判断是否出界 swap(v.maze[v.x][v.y],v.maze[u.x][u.y]); //将空位和相邻位交换 v.Hash=get_hash(v); //得到HASH值 if(vis[v.Hash]==-1){ //判断是否已遍历 vis[v.Hash]=i; //保存方向 v.g++;; //已花代价+1 pre[v.Hash]=u.Hash; //保存路径,表示经过这个点 v.h=get_h(v); //得到新的估价函数H que.push(v); //入队 } if(v.Hash==destination) return ; } } } } void print(){ string ans; ans.clear(); int nxt=destination; while(pre[nxt]!=-1){ //从终点往起点找路径 switch(vis[nxt]){ //四个方向对应 case 0:ans+='r';break; case 1:ans+='l';break; case 2:ans+='d';break; case 3:ans+='u';break; } nxt=pre[nxt]; } for(int i=ans.size()-1;i>=0;i--) putchar(ans[i]); puts(""); } int main(){ char str[100]; while(gets(str)!=NULL){ int k=0; memset(vis,-1,sizeof(vis)); memset(pre,-1,sizeof(pre)); for(int i=0;i<3;i++) //这两个for循环作用是把str字符串存入结构体s.maze[3][3]中,并转化为int型 for(int j=0;j<3;j++){ //记录空格(x)的坐标 if((str[k]<='9'&&str[k]>='0')||str[k]=='x'){ if(str[k]=='x'){ s.maze[i][j]=0; s.x=i; s.y=j; } else s.maze[i][j]=str[k]-'0'; } else j--; k++; } if(!isok(s)){ //起始状态不可行 printf("unsolvable\n"); continue; } s.Hash=get_hash(s); //初始状态的hash值为0 if(s.Hash==destination){ //起始状态为目标状态 puts(""); continue; } vis[s.Hash]=-2; s.g=0;s.h=get_h(s); astar(); print(); } return 0; }