POJ 1077 搜索利用康托展开。。。
2014-06-19 01:57
447 查看
趁世界杯间隙,赶快发一篇,本来期末复习加上世界杯想偷懒不刷题的,结果今天无意中翻ACDREAM的博客,无意中又看到了康托展开,结果发现,这尼玛不就是蓝桥杯决赛编程第一道题的裸题么,还是完美解法,遥想月初傻傻用NEXT_PERMUTATION来暴力,结果绝壁没拿满分,深感忧桑。。因此把康托展开的原理看了,手打了一遍,其实很简单,就是涉及了一点排列组合的思想,百度上面都有解释的,我就不解释了。然后再来看这道题。。。
Eight
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.
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:
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
Sample Output
八数码问题 ,如何让一个序列最后变成12345678x的形式,当然这九个数要在九宫格中的,x的四方向广搜,把INPUT的序列自动进行康托展开看看它对应的哈希值,然后再把INPUT序列X的位置还原到九宫格中进行搜索,然后搜索的出口就是123456789x所对应的哈希值了,在此我们把x等价于0。。。最后输出路径就好。话说康托展开这样一个数学问题为什么要扯上搜索,真是不好玩
。。。
先附上康托展开的代码简单模板:
Eight
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 23726 | Accepted: 10484 | Special Judge |
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 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.
Sample Input
2 3 4 1 5 x 7 6 8
Sample Output
ullddrurdllurdruldr
八数码问题 ,如何让一个序列最后变成12345678x的形式,当然这九个数要在九宫格中的,x的四方向广搜,把INPUT的序列自动进行康托展开看看它对应的哈希值,然后再把INPUT序列X的位置还原到九宫格中进行搜索,然后搜索的出口就是123456789x所对应的哈希值了,在此我们把x等价于0。。。最后输出路径就好。话说康托展开这样一个数学问题为什么要扯上搜索,真是不好玩
。。。
先附上康托展开的代码简单模板:
int kangtuo(int s[],int n) { int sum=0; for(int i=0;i<n;i++) { int num=0; for(int j=i+1;j<n;j++) if(s[j]<s[i]) num++; // cout<<num<<" "<<endl; sum+=(num*fact[n-1-i]);//fact[]是用来存阶乘的数组 //cout<<sum<<endl; } return sum+1;//因为从0开始所以加1就可以知道该序列在全排列中的位置了。 }再附上该题的代码:
#include<cstdio> #include<cstring> #include<cmath> #include<iostream> #include<queue> #include<string> #include<algorithm> using namespace std; int fact[]={1,1,2,6,24,120,720,5040,40320,362880}; bool vis[363000]; struct node{ int hx;//hash值 int xw;//x的位置 int s[9];//当前的状态 string path; }st; string ans; int dis[4][2]={-1,0,1,0,0,-1,0,1};//udlr char dir[5]="udlr"; int aim=46234;//123457880的HASH int kangtuo(int s[]) { int sum=0; for(int i=0;i<9;i++) { int num=0; for(int j=i+1;j<9;j++) if(s[j]<s[i]) num++; sum+=(num*fact[8-i]); } return sum+1; } bool check(int x,int y) { if(x<0||x>2||y<0||y>2) { return false; } return true; } bool bfs() { memset(vis,0,sizeof(vis)); queue<node> q; node a,b; q.push(st); vis[st.hx]=1; while(!q.empty()) { a=q.front(); q.pop(); int x=a.xw/3; int y=a.xw%3;//把x的位置转换到九宫格中 for(int i=0;i<4;i++) { int nx=x+dis[i][0]; int ny=y+dis[i][1]; if(check(nx,ny)==0) { continue; } b=a; b.xw=nx*3+ny;//转回123456789x的形式 b.s[a.xw]=b.s[b.xw];//把现在的'0'的原值移动到原图的'0'位置处 b.s[b.xw]=0; b.hx=kangtuo(b.s); if(vis[b.hx]==0) { vis[b.hx]=1; b.path=a.path+dir[i]; if(b.hx==aim) { ans=b.path; return true; } q.push(b); } } } return 0; } int main() { char str[305]; while(gets(str)) { int cnt=0; int len=strlen(str); for(int i=0;i<len;i++) { if(str[i]>='1'&&str[i]<='9') { st.s[cnt++]=str[i]-'0'; } else if(str[i]=='x') { st.s[cnt]=0; st.xw=cnt++; } } st.hx=kangtuo(st.s); if(st.hx==aim) { puts(""); continue; } if(bfs()) { cout<<ans<<endl; } else cout<<"unsolveable"<<endl; } return 0; }
相关文章推荐
- hdu 1043/poj 1077 Eight (八数码 经典搜索题 bfs + 康托展开)
- poj2251 三维搜索 利用优先队列
- POJ 3258 River Hopscotch(二分搜索巧妙利用)
- POJ - 1077 - 康托展开
- poj 2488 A Knights Journey(搜索,巧妙利用dir方向数组)
- POJ 1077 八数码(康托展开+暴力bfs)
- Poj 1077 Eight 八数码问题 (搜索)
- 搜索 ( 八数码问题详解:BFS,A*,IDA* )——Eight ( POJ 1077 )
- POJ 1077 Eight & HDU 1043 Eight(康托展开+BFS)
- hdu 1034 & poj 1077 Eight 传说中的八数码问题。真是一道神题,A*算法+康托展开
- POJ 3273 Monthly Expense(二分搜索巧妙利用)
- POJ 1077 Eight 八数码问题[康托展开 + BFS]
- HDU 1043 && POJ 1077 Eight bfs || 双向bfs || A*搜索
- POJ 1077 && HDU 1043 Eight A*算法,bfs,康托展开,hash 难度:3
- POJ 1077 Eight (正向BFS + 康托展开)
- POJ 1077 Eight(单向搜索)
- POJ 1077 八数码问题 练习搜索
- POJ - 1077 Eight : 八数码 -- 哈唏 康托展开 双向bfs A* IDA*
- poj 1077(BFS预处理+康托展开)
- 如何利用AJAX技术提高网站搜索引擎排名(3)