poj2046---BFS

好吧,表示这道题hash不会用,其实有很多题hash我都不会写= =!是用set判重的,空间占用很大,时间也不少。

写了个启发函数,其实也不算什么启发,就是在节点里保存距最终状态还有多少步step,并按step从小到大排序,用优先队列保存,囧,,不料wa了,不知哪里挫了。。。

#include<set>
#include<queue>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int n,m;
int map[4][10];
struct TNode
{
	int id[5],cnt,step,later[40];
	string lf;
}st;
string str;
set<string>ans;
queue<TNode>qu;
void init()
{
	int i,j,k,i1,j1;
	bool tmp;
	str="";
	for(i=k=0;i<4;i++)
	{
		str+=i+1+'0';
		str+='1';
		map[i][0]=(i+1)*10+1;
		for(j=1;j<8;j++)
		{
			scanf("%d",&map[i][j]);
			if(map[i][j]%10==1) 
				map[i][j]=0,
				st.id[k++]=i*8+j,
				str+="00";
			else
				str+=map[i][j]/10+'0',
				str+=map[i][j]%10+'0';
		}
	}
	st.lf=str,st.cnt=0;
	str="";
	for(i=1;i<=4;i++) 
	{
		for(j=1;j<=7;j++)
			str+=i+'0',
			str+=j+'0';
		str+="00";
	}
	st.step=0;
	for(i=0;i<4;i++) for(j=0;j<8;j++)
	{
		if(map[i][j]==0) continue;
		for(i1=0,tmp=0;i1<4;i1++)
		{
			for(j1=0;j1<8;j1++)
			{
				if(map[i1][j1]==map[i][j]+1)
				{
					st.later[i*8+j]=i1*8+j1,
						tmp=1;
					break;
				}
			}
			if(tmp) break;
		}
	}
}
int bfs()
{
	int i,j,k,mid;
	TNode tmp1,tmp2;
	ans.clear();
	ans.insert(st.lf);
	while(!qu.empty()) qu.pop();
	qu.push(st);
	while(!qu.empty())
	{
		tmp1=qu.front();
		qu.pop();
		if(str==tmp1.lf) return tmp1.cnt;
		for(i=0;i<4;i++)
		{
			tmp2=tmp1;
			tmp2.cnt=tmp1.cnt+1;
			k=tmp1.id[i];
			if(tmp1.lf[2*k-1]=='7'||tmp1.lf[2*k-1]=='0') continue;
			j=tmp2.later[k-1];
			tmp2.lf[2*k]=tmp2.lf[2*j];
			tmp2.lf[2*k+1]=tmp2.lf[2*j+1];
			tmp2.lf[2*j]=tmp2.lf[2*j+1]='0';
			tmp2.id[i]=j;
			tmp2.later[k-1]=k;
			tmp2.later[k]=tmp2.later[j];
			if(ans.count(tmp2.lf)) continue;
			else
			{
				ans.insert(tmp2.lf);
				qu.push(tmp2);
			}
		}
	}
	return -1;
}
int main()
{
	scanf("%d",&n);
	while(n--)
	{
		init();
		printf("%d\n",bfs());
	}
	return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值