POJ 1077八数码问题(cantor展开+BFS)
来源:互联网 发布:淘宝联盟可以相信吗 编辑:程序博客网 时间:2024/06/05 04:12
Eight
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 28887 Accepted: 12584 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
广度搜索很好想,主要难想的地方在hash判重,这里用到的是cantor展开判重复
基本思想是,从全排列组成的数字中,找出比他小的有数有多少
对于一个数字,从第一位看起,找他后面有多少个比这一位小的位,比如有3个,那就是假设如果交换
这两位那么后面的数无论怎么排列都比以前小,也就是比原来那个数小的数有剩下位数全排列种类数的个数。
比如213456789
第一位2后面有一个数1比他小,假设交换
1(23456789)括号里面的数无论怎么排列都比原来的213456789小吧
那就是8!种
再比如312456789
第一位3后面有两个数1和2比他小,假设交换
1(32456789)括号里面的数无论怎么排列都比原来的312456789小吧
2(13456789)括号里面的数无论怎么排列都比原来的312456789小吧
那就是8!+8!种
其他位数也是都是这样运算的。
代码:
#include<iostream>#include<cstdio>#include<cstring>#include<queue>using namespace std;struct node { int a[3][3] ; int x,y ; char states[100]; node(){ memset(states,'\0',sizeof(states)); memset(a,0,sizeof(a)); x=0;y=0; }};node h;const int MAXN=1000000;///最多是9!int fac[]={1,1,2,6,24,120,720,5040,40320,362880};///康拖展开判重0!1!2!3!4!5!6!7!8!9!bool vis[MAXN];///标记int addx[4]={-1,1,0,0};int addy[4]={0,0,-1,1};int cantor(int m[3][3])///康拖展开求该序列的hash值{ int s[9]; int k=0; for(int i=0;i<3;i++){ for(int j=0;j<3;j++){ s[k++]=m[i][j]; } } 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*fac[9-i-1]); } return sum+1;}void bfs(){ queue<node> qq; qq.push(h); while(!qq.empty()){ node top=qq.front(); qq.pop(); if(cantor(top.a)==1){ printf("%s\n",top.states); return ; } int ch[3][3]; for(int i=0;i<3;i++){ for(int j=0;j<3;j++){ ch[i][j]=top.a[i][j]; } } for(int i=0;i<4;i++){ int newx=top.x+addx[i]; int newy=top.y+addy[i]; if(newx>=0&&newx<3&&newy>=0&&newy<3){ swap(ch[newx][newy],ch[top.x][top.y]); if(!vis[cantor(ch)]){ node pp; memcpy(pp.a,ch,9*sizeof(int));///快速复制数组 memcpy(pp.states,top.states,sizeof(top.states)); pp.x=newx; pp.y=newy; if(i==0)strcat(pp.states,"u"); if(i==1)strcat(pp.states,"d"); if(i==2)strcat(pp.states,"l"); if(i==3)strcat(pp.states,"r"); qq.push(pp); vis[cantor(pp.a)]=true; } swap(ch[newx][newy],ch[top.x][top.y]); } } }}int main(){ //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); int arry[9]; for(int i=0;i<9;i++){ char c[2]; scanf("%s",&c); if(c[0]=='x')arry[i]=9; else arry[i]=c[0]-'0'; } int k=0; for(int i=0;i<3;i++){ for(int j=0;j<3;j++){ h.a[i][j]=arry[k++]; if(h.a[i][j]==9){ h.x=i; h.y=j; } } } memset(vis,false,sizeof(vis)); vis[cantor(h.a)]=true; bfs(); return 0;}
0 0
- POJ 1077八数码问题(cantor展开+BFS)
- POJ 1077 Eight 八数码问题[康托展开 + BFS]
- POJ 1077 八数码(康托展开+暴力bfs)
- hdu 1043 /poj 1077 Eight(经典八数码问题,BFS+康托展开)
- poj 1077 bfs+康托展开(8数码问题)
- [POJ]1077 Eight 八数码:康托展开+BFS
- POJ 1077 Eight(BFS八数码问题)
- POJ 1077 Eight 八数码问题 BFS
- POJ 1077 Eight(bfs八数码问题)
- hdu 1043/poj 1077 Eight (八数码 经典搜索题 bfs + 康托展开)
- bfs+hash poj 1077/hdu 1043 八数码问题
- 双向BFS解八数码问题: POJ 1077
- poj 1077 Eight(经典八数码问题:bfs/Dbfs)
- 【康托展开+状压BFS】poj1077 Eight(八数码问题)
- 康托展开(八数码问题)
- 八数码问题的初次解决(康托展开+bfs)
- 蓝桥杯 历届试题 九宫重排 (八数码问题--康托展开去重 + bfs搜索)
- HDU 1043 Eight(经典八数码问题)对比POJ 1077(bfs)
- CRM系统Salesforce 客户bill触发器
- 如何解决Tomcat端口被占用问题
- 清华TUNA镜像源下载Android源码的方法
- Android开发快捷键
- iOS 之 UITableView 使用详解
- POJ 1077八数码问题(cantor展开+BFS)
- Ajax核心知识——XMLHttpRequest
- recycleview中item的刷新方法
- LinkedList与ArrayList的区别
- JVM 字节码执行实例分析
- HDU 2001 计算两点间的距离(水题)
- poj 3321 Apple Tree(dfs序+线段树)
- 打开MySQL的慢查询记录
- CF Educational Round 12, B