HDOJ 题目1043 Eight(单向BFS,康拓展开,打表)
来源:互联网 发布:喜欢印软件下载 编辑:程序博客网 时间:2024/05/21 15:40
Eight
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 16879 Accepted Submission(s): 4640
Special 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:
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 1213 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 1213 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
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
2 3 4 1 5 x 7 6 8
Sample Output
ullddrurdllurdruldr
Source
South Central USA 1998 (Sepcial Judge Module By JGShining)
Recommend
JGShining | We have carefully selected several similar problems for you: 1044 1026 1180 1067 1175
题目大意:
问序列变成12345678x的路径
ac代码
153941362015-11-05 17:54:45Accepted1043202MS8936K2668 BC++XY_
#include<stdio.h>#include<string.h>#include<iostream>#include<algorithm>#include<queue>using namespace std;char num[15];int fac[15];void Fac(){ int i; fac[1]=1; for(i=2;i<=10;i++) fac[i]=fac[i-1]*i;}int cot(char *num){ int i,j; int sum=0; for(i=0;i<8;i++) { int c=0; for(j=i+1;j<9;j++) { if(num[i]>num[j]) c++; } sum+=c*fac[9-i-1]; } return sum+1;}struct s{ int x,y; int cnt; char num[15];}a,temp;int vis[1010000];int dirx[4]={0,1,0,-1};int diry[4]={1,0,-1,0};char p[1010000];int pre[1010000];void bfs(){ int i; queue<struct s>q; memset(vis,0,sizeof(vis)); for(i=0;i<8;i++) a.num[i]=i+'1'; a.num[8]='0'; a.cnt=cot(a.num); //printf("%d\n",a.cnt); a.x=2; a.y=2; q.push(a); vis[a.cnt]=1; p[a.cnt]=-1; while(!q.empty()) { a=q.front(); q.pop(); for(int i=0;i<4;i++) { temp.x=a.x+dirx[i]; temp.y=a.y+diry[i]; if(temp.x<0||temp.x>2||temp.y<0||temp.y>2) continue; int pos2=temp.x*3+temp.y; int pos1=a.x*3+a.y; strcpy(temp.num,a.num); swap(temp.num[pos1],temp.num[pos2]); temp.cnt=cot(temp.num); // printf("%d\n",temp.cnt); if(vis[temp.cnt]) continue; vis[temp.cnt]=1; pre[temp.cnt]=a.cnt; if(i==0) p[temp.cnt]='l'; else if(i==1) p[temp.cnt]='u'; else if(i==2) p[temp.cnt]='r'; else p[temp.cnt]='d'; q.push(temp); } }}char str[1010];void print(int tt){ int x=tt; while(p[x]!=-1) { printf("%c",p[x]); x=pre[x]; } printf("\n");}int main(){ Fac(); bfs(); while(gets(str)!=NULL) { int i; int len=strlen(str); char ss[15],k=0; for(i=0;i<len;i++) { if(str[i]=='x') { ss[k++]='0'; } else if(str[i]>='1'&&str[i]<='8') ss[k++]=str[i]; } ss[k]='\0'; int tt=cot(ss); if(!vis[tt]) { printf("unsolvable\n"); } else { print(tt); } }}
0 0
- HDOJ 题目1043 Eight(单向BFS,康拓展开,打表)
- hdu1043 Eight 康拓展开+bfs打表
- HDU 1043 Eight(BFS+康拓展开)
- POJ 1077 Eight(康拓展开 BFS 双向BFS)
- HDU1043:Eight HDU3567:Eight II(康拓展开+bfs搜索)
- hdu1430(康拓展开+bfs打表)
- HDU 1043 Eight(BFS打表+状态去重)
- (已解决)hdu 1043 Eight (逆向bfs+打表+康托压缩)
- HDU 1043 Eight(反向BFS打表+康托展开)
- POJ 1077 Eight BFS + 康拓展开式
- hdu3567 Eight II 康拓展开+打表+路径回溯+映射
- hdu1043 Eight(A*/双向BFS/单项BFS打表+康托展开)
- hdu 1430 bfs + 打表 + 康拓展开
- HDU Eight( 康拓展开)
- poj 1077 Eight 八数码问题( 康拓展开+BFS状态压缩)
- HDU 1043 Eight(A* + 奇偶剪枝 + 康拓展开)
- HDU3567:Eight II(康拓展开+预处理) (B)
- HDU 1043 Eight(BFS)
- Metasploit 渗透测试笔记(Meterpreter篇)
- 3.30uitableview的知识点
- C# this.invoke()作用 多线程操作UI
- Maven打包自定义时间戳格式
- 《MySQL从入门到精通(视频教学版)》目录
- HDOJ 题目1043 Eight(单向BFS,康拓展开,打表)
- iOS的全局session
- What is an overlay?
- Nutch2.3 + Hbase 配置到爬行
- sql server 2008 r2 基础
- BOOL,float,指针变量与零值的比较
- 手机外观设备图..
- 树莓派上手实战之系统之raspi-config系统配置工具
- CNN反向求导推导