UESTC Training for Search Algorithm——C
2012-05-20 01:17
477 查看
Eight Puzzle
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 callthe 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. To simplify this problem, you should print the minimum steps only.
Input
There are multiple test cases.For each test case, 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
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.Otherwise, output an integer which equals the minimum steps.Sample Input
1 2 x 4 5 3 7 8 6Sample Output
2Hint
Any violent algorithm may gain TLE. So a smart method is expected.The data used in this problem is unofficial data prepared by hzhua. So any mistake here does not imply mistake in the offcial judge data.
/*算法思想: 这是我纠结的最久的题目了,刚开始想用A*做,于是在网上找了半天的A*算法的资料,自己也学了很久 终于是学会了之后,写起来又很恼火,不会用STL里面的优先队列(实际上那时我还不知道STL里还有优 先队列,只知道一个queue,囧,听了讲座之后才知道的),后来写毛了,直接在目标状态BFS一遍,逆 着枚举所有能到达目标状态的情况及到达的最小步数,用的康托展开保存状态,把x视作0,然后加 个标记数组,判断是不是已经走过该点了,和第一题一样,最先遍历到该点的值一定是最优的,以后一定补上此题的A*算法 */ /*CODE:*/ #include<cstdio> #include<cstring> #include<cmath> #include<queue> #include<iostream> #include<cstdlib> #include<algorithm> using namespace std; const int jiecheng[9]={1,1,2,6,24,120,720,5040,40320}; //保存每个数 i 的阶乘 jiecheng[i] const int pos[9][2]={2,2,0,0,0,1,0,2,1,0,1,1,1,2,2,0,2,1}; //保存每个数(0--9)的目标状态的位子 const int zl[4][2]={1,0,-1,0,0,1,0,-1}; //方向增量 int a[9]; // int begin,end; //开始的状态和结束的状态的康托值 int ans[400000]; //保存每个能够到达目标状态的状态的到达的最优步数 int hash[400000][9]; //保存每个康拓值对应的具体状态矩阵 bool visit[400000]; //判断该状态是不是已经遍历过了 struct data { int kangtuo; //康拓值 int p; //x(0)的位子 int step; //到达这个状态的步数 }; bool in(int x,int y) //判断是不是在这个矩阵中 { return x>=0 && x<3 && y>=0 && y<3; } void change(int a[],int p1,int p2) //交换两个相邻点之间的值 { int x=a[p1]; a[p1]=a[p2]; a[p2]=x; } int kangtuo(int a[]) //对一个数组进行康托展开 { int temp=0; for(int i=0;i<9;i++) { int t=0; for(int j=0;j<i;j++) if(a[j]<a[i]) t++; temp+=(a[i]-t)*jiecheng[8-i]; //加上每个数乘以对应位子的阶乘 } return temp; } void add(int v,int a[]) //添加一个具体的状态到已经访问过的hash数组中 { for(int i=0;i<9;i++) hash[v][i]=a[i]; } void bfs() //BFS过程 { queue<data> q; q.push({end,8,0}); //初始化队列 visit[end]=true; //将目标状态的设置为已经访问过 ans[end]=0; //目标状态的到达步数为0 while(!q.empty()) { data now=q.front(); q.pop(); int x=now.p/3; //得到x坐标 int y=now.p%3; //得到y坐标 for(int i=0;i<4;i++) //枚举四个方向 if(in(x+zl[i][0],y+zl[i][1])) //如果在矩阵中 { int cur[9]; for(int i=0;i<9;i++) cur[i]=hash[now.kangtuo][i]; //得到这个状态的具体矩阵 change(cur,now.p,(x+zl[i][0])*3+(y+zl[i][1])); //交换两个点之间的值,相当于把x移到相应的方向 int temp=kangtuo(cur); //计算康托值 if(visit[temp]) continue; //如果已经遍历过这个状态了,继续遍历下一个状态 add(temp,cur); //添加一个具体的访问过的状态 visit[temp]=true; ans[temp]=now.step+1; q.push({temp,(x+zl[i][0])*3+(y+zl[i][1]),now.step+1}); //加入队列,再以它为基础,推出其他的可能遍历到的状态 } } } int main() { memset(visit,0,sizeof(visit)); //初始化visit,没有遍历过 memset(hash,0,sizeof(hash)); //初始化hash,没有一个已经遍历过的状态 memset(ans,-1,sizeof(ans)); //初始化ans,每个状态到达目标状态的值设为-1,方便输出(就不用判断是否到达了,-1就表示不能到达) int temp[]={1,2,3,4,5,6,7,8,0}; //目标状态 end=kangtuo(temp); //计算目标状态的康托值 add(end,temp); //添加一个已经遍历过的状态 bfs(); //BFS char s[30]; //用于处理输入数据 while(gets(s)) { for(int i=0;i<strlen(s);i+=2) if(s[i]>='0' && s[i]<='9') a[i/2]=s[i]-'0'; //'0'--'9'之间的字符就去他们字符表示的数字 else a[i/2]=0; //是x就变为0 begin=kangtuo(a); //计算此节点的康脱值 if(ans[begin]==-1) printf("unsolvable\n"); else printf("%d\n",ans[begin]); } return 0; }
相关文章推荐
- 2016 UESTC Training for Search Algorithm & String H - 中二少女与字符串 Trie 字典树
- 2014 UESTC Training for Search Algorithm Problem F 方老师与迷宫
- UESTC Training for Search Algorithm——B
- UESTC_Palindromic String 2015 UESTC Training for Search Algorithm & String<Problem M>
- 2016 UESTC Training for Search Algorithm & String I - 谭爷剪花布条 KMP
- UESTC Training for Search Algorithm——D
- UESTC_王之迷宫 2015 UESTC Training for Search Algorithm & String<Problem A>
- UESTC_韩爷的梦 2015 UESTC Training for Search Algorithm & String<Problem N>
- 2016 UESTC Training for Search Algorithm & String K - 卿大爷的三个女友 KMP、跳转数组
- 2016 UESTC Training for Search Algorithm & String(A B C E I K)
- UESTC Training for Search Algorithm——E
- UESTC_邱老师降临小行星 2015 UESTC Training for Search Algorithm & String<Problem B>
- UESTC Training for Search Algorithm——F
- UESTC Training for Search Algorithm——G
- UESTC_基爷与加法等式 2015 UESTC Training for Search Algorithm & String<Problem C>
- 2015 UESTC Training for Search Algorithm & String - J - 全都是秋实大哥 【KMP】
- UESTC Training for Search Algorithm——H
- UESTC_基爷的中位数 2015 UESTC Training for Search Algorithm & String<Problem D>
- 2014 UESTC Training for Search Algorithm B
- UESTC Training for Search Algorithm——I