POJ 1077(HDU 1043)Eight(八数码DBFS)
来源:互联网 发布:松岗卓翼科技工资算法 编辑:程序博客网 时间:2024/04/28 00:03
//// main.cpp// Richard//// Created by 邵金杰 on 16/9/11.// Copyright © 2016年 邵金杰. All rights reserved.//#include<iostream>#include<cstdio>#include<cstring>#include<set>#include<algorithm>using namespace std;const int maxn=400000;int ha[9]={1,1,2,6,24,120,720,5040,40320};int End[9]={1,2,3,4,5,6,7,8,0};char result[maxn];struct node{ int sta[9]; int fa; char move; int hash; node(int fa,char move,int hash):fa(fa),move(move),hash(hash) {}; node() {};};node myQueue[2][maxn];int qHead[2],qTail[2];char moves[5]="uldr";int dx[4]={-1,0,1,0};int dy[4]={0,-1,0,1};int matchingstatus;int matchingQ;int contor(int a[9]){ int res=0,k; for(int i=0;i<9;i++) { k=0; for(int j=i+1;j<9;j++) { if(a[i]>a[j]) k++; } res+=k*ha[9-i-1]; } return res;}bool equal(int a[9]){ int times=0; for(int i=0;i<9;i++) { if(a[i]==0) continue; for(int j=i+1;j<9;j++) { if(a[j]==0) continue; if(a[i]>a[j]) times++; } } return times&1;}bool inside(int x,int y){ return x<0||x>2||y<0||y>2;}bool DBFS(int a[9]){ set<int> expand[2]; int starthash=contor(a); int endhash=contor(End); for(int i=0;i<2;i++){ qHead[i]=0;qTail[i]=1; } myQueue[0][0]=node(-1,0,starthash); for(int i=0;i<9;i++) myQueue[0][0].sta[i]=a[i]; expand[0].insert(starthash); myQueue[1][0]=node(-1,0,endhash); for(int i=0;i<9;i++) myQueue[1][0].sta[i]=End[i]; expand[1].insert(endhash); int qNo,vqNo; while(qHead[0]!=qTail[0]||qHead[1]!=qTail[1]) { if(qHead[0]==qTail[0]) qNo=1; else if(qHead[1]==qTail[1]) qNo=0; else{ if(qTail[0]-qHead[0]<=qTail[1]-qHead[1]) qNo=0; else qNo=1; } vqNo=1-qNo; node now=myQueue[qNo][qHead[qNo]]; if(expand[vqNo].find(now.hash)!=expand[vqNo].end()) { matchingstatus=now.hash; matchingQ=qNo; return true; } else{ for(int i=0;i<4;i++) { int pos; for(int j=0;j<9;j++) if(now.sta[j]==0) {pos=j;break;} int x=pos/3+dx[i],y=pos%3+dy[i]; if(inside(x,y)) continue; int npos=x*3+y; memcpy(a,now.sta,sizeof(int)*9); swap(a[pos],a[npos]); node next(qHead[qNo],moves[i],contor(a)); memcpy(next.sta,a,sizeof(int)*9); if(expand[qNo].find(next.hash)!=expand[qNo].end()) continue; expand[qNo].insert(next.hash); myQueue[qNo][qTail[qNo]]=next; qTail[qNo]++; } qHead[qNo]++; } } return false;}inline char ReverseMove(char c) { if(c=='u') return 'd'; if(c=='d') return 'u'; if(c=='l') return 'r'; if(c=='r') return 'l'; return 0;}int main(){ char buf[100]; while(gets(buf)) { int a[9],k=0; int len=(int)strlen(buf); for(int i=0;i<len;i++) { if(buf[i]=='x') {a[k]=0;k++;} if(buf[i]>='1'&&buf[i]<='9') {a[k]=buf[i]-'0';k++;} } if(equal(a)) { printf("unsolvable\n"); continue; } if(DBFS(a)) { int move=0; int pos=0; if(matchingQ==0) pos=qHead[0]; else{ for(int i=0;i<qTail[0];i++){ if(myQueue[0][i].hash==matchingstatus) {pos=i;break;} } } do{ if(pos){ result[move++]=myQueue[0][pos].move; pos=myQueue[0][pos].fa; } }while(pos); reverse(result,result+move); if(matchingQ==0) { for(int i=0;i<qTail[1];i++){ if(myQueue[1][i].hash==matchingstatus) {pos=i;break;} } } else pos=qHead[1]; do{ if(pos){ result[move++]=ReverseMove(myQueue[1][pos].move); pos=myQueue[1][pos].fa; } }while(pos); for(int i=0;i<move;i++) cout<<result[i]; cout<<endl; } else { cout << "unsolvable" << endl; } } return 0;}
0 0
- POJ 1077(HDU 1043)Eight(八数码DBFS)
- poj 1077 hdu 1043 Eight 八数码问题 DBFS(双向广度优先搜索)a*算法 康拓展开
- POJ 1077 Eight 八数码 DBFS
- hdu 1043 poj 1077 Eight Time (搜索&八数码)
- HDU 1043 Eight poj 1077 (八数码 启发式搜索)
- poj 1077 Eight(经典八数码问题:bfs/Dbfs)
- POJ 1077 Eight && HDU 1043 Eight 八数码问题(A*算法)
- hdu 1043 Eight(八数码)
- HDU 1043 Eight(经典八数码问题)对比POJ 1077(bfs)
- POJ 1077(HDU 1043) Eight(八数码A*算法)
- hdu 1043 /poj 1077 Eight(经典八数码问题,BFS+康托展开)
- hdu 1043/poj 1077 Eight (八数码 经典搜索题 bfs + 康托展开)
- HDU 1043 / POJ 1077 Eight(八数码问题)
- poj 1077 & hdu 1043 Eight ( 多种解法:预处理、bfs、dbfs、IDA*、A*)
- uva 652---Eight Poj 1077 ---Eight zoj 1217---Eight (八数码解法1)
- POJ 1077 Eight(八数码A*+IDA*)
- POJ 1077 Eight(bfs八数码问题)
- POJ 1077 Eight(神奇的八数码问题)
- 基于Netty 的webSocket开发
- HTTP协议1
- 网络编程(8)—— IP地址和域名之间的转换
- 有些东西再忙也要做
- 第36篇 翻译webrtc官方文档(三)及PHP命名空间(二)
- POJ 1077(HDU 1043)Eight(八数码DBFS)
- LNK2001:unresolved external symbol "struct Distance Distance_struct"(?Distance_struct@@3UDistance@@A
- 七种网卡绑定模式
- 【33.18%】【hdu 5877】Weak Pair (3种解法)
- 《javascript高级程序设计》——变量和作用域
- memcache 提高命中率, memcached server LRU 深入分析
- day85-基于HDFS的SparkStreaming
- Sphinx比lucene的搜索引擎
- 一个好用的TListView点击列头像排序类