蓝桥杯中使用unordered_map的注意事项

以洛谷P1379 八数码难题为准,要用unordered_map头文件只有一个万能头文件是不够的,还需要加


#include<tr1/unordered_map>
using namespace std::tr1;

#include<bits/stdc++.h>
using namespace std;

#include<tr1/unordered_map>
using namespace std::tr1;
string end1="123804765";
string str;
int dx[]={-1,0,1,0};
int dy[]={0,1,0,-1};
unordered_map<string,int>m;
queue<string>q;
int bfs(string str)
{
	q.push(str);
	 m[str]=0;
	while(!q.empty())
	{
		string t=q.front();
		q.pop();
		if(t==end1)return m[t];
		
		int distance=m[t];
		int k=t.find('0');
		for(int i=0;i<4;i++)
		{
			int a=k/3+dx[i],b=k%3+dy[i];
			if(a<0||a>=3||b<0||b>=3)continue;
			int tmp=a*3+b;
			
			swap(t[k],t[tmp]); 
			if(!m.count(t))
			{
				q.push(t);
				m[t]=distance +1;
			}
			swap(t[k],t[tmp]);
		}
	}
}
int main()
{
	cin>>str;
	int res=bfs(str);
	printf("%d",res);
	return 0;
} 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值