poj 1077--Eight(八数码问题,BFS,A*,全排列的哈希)

#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,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,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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值