poj 1077--Eight(八数码问题,BFS,A*,全排列的哈希)
2013-05-02 20:04
453 查看
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<queue> #include<stack> using namespace std; #define inf 0x3f3f3f3f #define lc l,m,lc #define rc m+1,r,rc const int aim[10]={0,1,2,3,4,5,6,7,8,9}; #define N 100005 struct node { int state[10]; int step,dif,next,id,ord,pp; char act; friend bool operator<(const node &a,const node &b) { return (a.step+a.dif)>(b.step+b.dif); } int get_dif() { int i,ret=0; for(i=1;i<=9;i++)if(state[i]!=aim[i])ret++; return ret; } }temp,f,path[N*5]; int sump; int vis[400005]; int get_ord(int num[]) { int i,j,ret=0,tem,mu=1; for(i=1;i<9;i++)mu*=i; for(i=1;i<9;i++) { tem=0; for(j=i+1;j<=9;j++) { if(num[i]>num[j])tem++; } ret+=tem*mu; mu/=(9-i); } return ret; } void show(node x) { if(x.next==-1)return; show(path[x.next]); printf("%c",x.act); } void bfs() { priority_queue<node>q; temp. q.push(temp); vis[temp.ord]=1; while(!q.empty()) { f=q.top();q.pop(); if(!f.dif) { show(f); printf("\n"); return; } int i,j; for(i=0;i<4;i++) { temp=f; if(i==0) { j=temp.pp; if(j>6)continue; if(temp.state[j]==j)temp.dif++; if(temp.state[j+3]==j+3)temp.dif++; swap(temp.state[j],temp.state[j+3]); if(temp.state[j]==j)temp.dif--; if(temp.state[j+3]==j+3)temp.dif--; temp.act='d'; temp.pp=j+3; } else if(i==1) { j=temp.pp; if(j<4)continue; if(temp.state[j]==j)temp.dif++; if(temp.state[j-3]==j-3)temp.dif++; swap(temp.state[j],temp.state[j-3]); if(temp.state[j]==j)temp.dif--; if(temp.state[j-3]==j-3)temp.dif--; temp.act='u'; temp.pp=j-3; } else if(i==2) { j=temp.pp; if(j%3==0)continue; if(temp.state[j]==j)temp.dif++; if(temp.state[j+1]==j+1)temp.dif++; swap(temp.state[j],temp.state[j+1]); if(temp.state[j]==j)temp.dif--; if(temp.state[j+1]==j+1)temp.dif--; temp.act='r'; temp.pp=j+1; } else { j=temp.pp; if(j%3==1)continue; if(temp.state[j]==j)temp.dif++; if(temp.state[j-1]==j-1)temp.dif++; swap(temp.state[j],temp.state[j-1]); if(temp.state[j]==j)temp.dif--; if(temp.state[j-1]==j-1)temp.dif--; temp.act='l'; temp.pp=j-1; } temp.step++; temp.next=f.id; temp.ord=get_ord(temp.state); if(!vis[temp.ord]) { temp.id=sump; path[sump++]=temp; vis[temp.ord]=1; q.push(temp); } } } printf("unsolvable\n"); } int main() { char num[10][2]; while(scanf("%s",num[1])!=EOF) { memset(vis,0,sizeof vis); sump=0; if(num[1][0]=='x') { temp.state[1]=9; temp.pp=1; } else temp.state[1]=num[1][0]-'0'; int i,j,nxs=0; for(i=2;i<=9;i++) { scanf("%s",num[i]); if(num[i][0]=='x') { temp.state[i]=9; temp.pp=i; } else temp.state[i]=num[i][0]-'0'; } temp.step=0; temp.dif=temp.get_dif(); temp.next=-1; temp.id=0; temp.ord=get_ord(temp.state); path[sump++]=temp; for(i=1;i<=9;i++) { for(j=i+1;j<=9;j++) { if(temp.state[i]==9||temp.state[j]==9)continue; if(temp.state[i]>temp.state[j])nxs++; } } if(nxs&1)printf("unsolvable\n"); else bfs(); } return 0; }
#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<queue>#include<stack>using namespace std;#define inf 0x3f3f3f3f#define lc l,m,lc#define rc m+1,r,rcconst int aim[10]={0,1,2,3,4,5,6,7,8,9};#define N 100005struct node{int state[10];int step,next,ord,pp;char act;}temp,f,path[N*5];int vis[400005];int get_ord(int num[]){int i,j,ret=0,tem,mu=1;for(i=1;i<9;i++)mu*=i;for(i=1;i<9;i++){tem=0;for(j=i+1;j<=9;j++){if(num[i]>num[j])tem++;}ret+=tem*mu;mu/=(9-i);}return ret;}void show(node x){if(x.next==-1)return;printf("%c",x.act);show(path[x.next]);}void bfs(){int i,j;queue<node>q;for(i=1;i<=9;i++)temp.state[i]=i;temp.next=-1;temp.ord=0;temp.pp=9;temp.step=0;path[temp.ord]=temp;q.push(temp);vis[temp.ord]=1;while(!q.empty()){f=q.front();q.pop();for(i=0;i<4;i++){temp=f;if(i==0){j=temp.pp;if(j>6)continue;swap(temp.state[j],temp.state[j+3]);temp.act='u';temp.pp=j+3;}else if(i==1){j=temp.pp;if(j<4)continue;swap(temp.state[j],temp.state[j-3]);temp.act='d';temp.pp=j-3;}else if(i==2){j=temp.pp;if(j%3==0)continue;swap(temp.state[j],temp.state[j+1]);temp.act='l';temp.pp=j+1;}else{j=temp.pp;if(j%3==1)continue;swap(temp.state[j],temp.state[j-1]);temp.act='r';temp.pp=j-1;}temp.step++;temp.next=f.ord;temp.ord=get_ord(temp.state);if(!vis[temp.ord]){path[temp.ord]=temp;vis[temp.ord]=1;q.push(temp);}}}}int main(){char num[10][2];bfs();while(scanf("%s",num[1])!=EOF){if(num[1][0]=='x')temp.state[1]=9;else temp.state[1]=num[1][0]-'0';int i,j;for(i=2;i<=9;i++){scanf("%s",num[i]);if(num[i][0]=='x'){temp.state[i]=9;}else temp.state[i]=num[i][0]-'0';}temp.ord=get_ord(temp.state);if(!vis[temp.ord])printf("unsolvable\n");else{show(path[temp.ord]);printf("\n");}}return 0;}
相关文章推荐
- POJ 1077 Eight(BFS八数码问题)
- POJ 1077 Eight(bfs八数码问题)
- poj 1077 Eight(经典八数码问题:bfs/Dbfs)
- POJ 1077 Eight 八数码问题 BFS
- 搜索 ( 八数码问题详解:BFS,A*,IDA* )——Eight ( POJ 1077 )
- poj 1077 Eight 八数码问题( 康拓展开+BFS状态压缩)
- POJ 1077 Eight && HDU 1043 Eight 八数码问题(A*算法)
- POJ 1077 Eight(神奇的八数码问题)
- Poj 1077 eight(BFS+全序列Hash解八数码问题)
- hdu 1034 & poj 1077 Eight 传说中的八数码问题。真是一道神题,A*算法+康托展开
- POJ1077、HDU1043 Eight 八数码问题:双向BFS、A*
- BFS(八数码) POJ 1077 || HDOJ 1043 Eight
- hdu 1034 & poj 1077 Eight 传说中的八数码问题。真是一道神题,A*算法+康托展开
- POJ 1077 Eight 八数码问题 A*
- Poj 1077 eight(BFS+全序列Hash解八数码问题)
- poj 1077 hdu 1043 Eight 八数码问题 DBFS(双向广度优先搜索)a*算法 康拓展开
- POJ 1077 Eight【八数码问题】
- Poj 1077 Eight 八数码问题 (搜索)
- poj 1077-Eight(八数码+逆向bfs打表)
- POJ 1077 Eight, 八数码问题