poj 1184 聪明的打字员

#include <iostream>
#include <map>
#include <string> 
#include <queue> 

using namespace std;

struct point
{
	int step;
 	string s;
};
string e;
map<string,int> my;
queue<point> q;
int BFS(point st)
{
  	point t, tt;
	string ss; int i;
  	while(!q.empty())
	  	q.pop();
  	q.push(st);
  	my[st.s] = 1;
  	while(!q.empty())
  	{
    	t = q.front();
		q.pop();
		for(ss = t.s, i = 0; i < 6; i++)
      	if(ss[i] != e[i])
		  	break;
    	if(i == 6) 
			return t.step;
    	ss = t.s; 
		swap(ss[0], ss[ss[6] - '0']);//Swap0:
    	if(!my.count(ss))
    	{
      		tt.s = ss; 
  			tt.step = t.step+1;
      		q.push(tt);
  			my[ss] = 1;
    	} 
    	ss = t.s;
		swap(ss[5], ss[ss[6] - '0']); //Swap1
    	if(!my.count(ss))
    	{
      		tt.s = ss; 
  			tt.step = t.step+1;
      		q.push(tt);
  			my[ss]=1;
   		}
    	ss=t.s; 
		if(ss[ss[6]-'0'] != '9' && ss[ss[6]-'0'] != e[ss[6]-'0']) 
			ss[ss[6]-'0'] += 1; //Up:
    	if(!my.count(ss))
    	{
      		tt.s = ss;
	  		tt.step = t.step+1;
      		q.push(tt);
	  		my[ss]=1;
    	}
    	ss = t.s;
		if(ss[ss[6]-'0'] != '0' && ss[ss[6]-'0'] != e[ss[6]-'0']) 
			ss[ss[6]-'0']-=1;//Down: 
    	if(!my.count(ss))
    	{
      		tt.s = ss;
	  		tt.step = t.step+1;
      		q.push(tt); my[ss] = 1;
    	}
    	ss = t.s; 
    	if(ss[6]-'0' != 0)//left
    	{
      		if(ss[6]!='5') //1 2 3 4位置相同才能移动光标 
      		{
        		if(ss[ss[6]-'0'] == e[ss[6]-'0'])
					ss[6]-=1; 
      		}
      		else ss[6]-=1;
    	}
    	if(!my.count(ss))
    	{
      		tt.s = ss;
  			tt.step = t.step + 1;
      		q.push(tt);
  			my[ss] = 1;
    	}
    	ss = t.s; 
    	if(ss[6]-'0' != 5)//Right
    	{
      		if(ss[6] != '0') //1 2 3 4位置相同才能移动光标 
      		{
        		if(ss[ss[6] - '0'] == e[ss[6]-'0'])
					ss[6] += 1; 
      		}
      		else
		  		ss[6] += 1;
    	}
    	if(!my.count(ss))
    	{
      		tt.s = ss;
	  		tt.step = t.step+1;
      		q.push(tt);
	  		my[ss]=1;
    	}	
  	}
}
int main()
{
  	point st;
  	while( cin >> st.s >> e )
  	{
    	my.clear();
    	st.s += '0'; 
		st.step=0;
    	cout<<BFS(st)<<endl;
  	}
  	return 0;
}
参考链接:http://blog.csdn.net/liang5630/article/details/11993027
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值