您的位置:首页 > 其它

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;}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: