hdu1195

/*
分析:
    和hdu1495一样,推状态就行了。


                                      2012-07-24
*/








#include"stdio.h"
#include"string.h"
#include"queue"
using namespace std;


int map[10][10][10][10];
char s[11];
char ans[11];
struct node
{
	int a,b,c,d;
	int step;
};


int judge(int a,int b,int c,int d)
{
	return map[a][b][c][d];
}
int BFS()
{
	queue<node>q;
	node cur,next;
	int t;


	cur.a=s[0];
	cur.b=s[1];
	cur.c=s[2];
	cur.d=s[3];
	cur.step=0;
	map[cur.a][cur.b][cur.c][cur.d]=1;
	q.push(cur);


	while(!q.empty())
	{
		cur=q.front();
		q.pop();


		if(cur.a==ans[0] && cur.b==ans[1] && cur.c==ans[2] && cur.d==ans[3])	return cur.step;



		if(cur.a==1)	next.a=9;
		else			next.a=cur.a-1;
		next.b=cur.b;
		next.c=cur.c;
		next.d=cur.d;
		if(!judge(next.a,next.b,next.c,next.d))
		{
			next.step=cur.step+1;
			map[next.a][next.b][next.c][next.d]=1;
			q.push(next);
		}

		if(cur.a==9)	next.a=1;
		else			next.a=cur.a+1;
		next.b=cur.b;
		next.c=cur.c;
		next.d=cur.d;
		if(!judge(next.a,next.b,next.c,next.d))
		{
			next.step=cur.step+1;
			map[next.a][next.b][next.c][next.d]=1;
			q.push(next);
		}

		next.a=cur.a;
		next.b=cur.b;
		next.c=cur.c;
		next.d=cur.d;
		t=next.a;
		next.a=next.b;
		next.b=t;
		if(!judge(next.a,next.b,next.c,next.d))
		{
			next.step=cur.step+1;
			map[next.a][next.b][next.c][next.d]=1;
			q.push(next);
		}
		

		if(cur.b==1)	next.b=9;
		else			next.b=cur.b-1;
		next.a=cur.a;
		next.c=cur.c;
		next.d=cur.d;
		if(!judge(next.a,next.b,next.c,next.d))
		{
			next.step=cur.step+1;
			map[next.a][next.b][next.c][next.d]=1;
			q.push(next);
		}

		if(cur.b==9)	next.b=1;
		else			next.b=cur.b+1;
		next.a=cur.a;
		next.c=cur.c;
		next.d=cur.d;
		if(!judge(next.a,next.b,next.c,next.d))
		{
			next.step=cur.step+1;
			map[next.a][next.b][next.c][next.d]=1;
			q.push(next);
		}

		next.a=cur.a;
		next.b=cur.b;
		next.c=cur.c;
		next.d=cur.d;
		t=next.a;
		next.a=next.b;
		next.b=t;
		if(!judge(next.a,next.b,next.c,next.d))
		{
			next.step=cur.step+1;
			map[next.a][next.b][next.c][next.d]=1;
			q.push(next);
		}

		next.a=cur.a;
		next.b=cur.b;
		next.c=cur.c;
		next.d=cur.d;
		t=next.c;
		next.c=next.b;
		next.b=t;
		if(!judge(next.a,next.b,next.c,next.d))
		{
			next.step=cur.step+1;
			map[next.a][next.b][next.c][next.d]=1;
			q.push(next);
		}
		

		if(cur.c==1)	next.c=9;
		else			next.c=cur.c-1;
		next.b=cur.b;
		next.a=cur.a;
		next.d=cur.d;
		if(!judge(next.a,next.b,next.c,next.d))
		{
			next.step=cur.step+1;
			map[next.a][next.b][next.c][next.d]=1;
			q.push(next);
		}

		if(cur.c==9)	next.c=1;
		else			next.c=cur.c+1;
		next.b=cur.b;
		next.a=cur.a;
		next.d=cur.d;
		if(!judge(next.a,next.b,next.c,next.d))
		{
			next.step=cur.step+1;
			map[next.a][next.b][next.c][next.d]=1;
			q.push(next);
		}

		next.a=cur.a;
		next.b=cur.b;
		next.c=cur.c;
		next.d=cur.d;
		t=next.b;
		next.b=next.c;
		next.c=t;
		if(!judge(next.a,next.b,next.c,next.d))
		{
			next.step=cur.step+1;
			map[next.a][next.b][next.c][next.d]=1;
			q.push(next);
		}

		next.a=cur.a;
		next.b=cur.b;
		next.c=cur.c;
		next.d=cur.d;
		t=next.d;
		next.d=next.c;
		next.c=t;
		if(!judge(next.a,next.b,next.c,next.d))
		{
			next.step=cur.step+1;
			map[next.a][next.b][next.c][next.d]=1;
			q.push(next);
		}
		

		if(cur.d==1)	next.d=9;
		else			next.d=cur.d-1;
		next.b=cur.b;
		next.c=cur.c;
		next.a=cur.a;
		if(!judge(next.a,next.b,next.c,next.d))
		{
			next.step=cur.step+1;
			map[next.a][next.b][next.c][next.d]=1;
			q.push(next);
		}

		if(cur.d==9)	next.d=1;
		else			next.d=cur.d+1;
		next.b=cur.b;
		next.c=cur.c;
		next.a=cur.a;
		if(!judge(next.a,next.b,next.c,next.d))
		{
			next.step=cur.step+1;
			map[next.a][next.b][next.c][next.d]=1;
			q.push(next);
		}

		next.a=cur.a;
		next.b=cur.b;
		next.c=cur.c;
		next.d=cur.d;
		t=next.d;
		next.d=next.c;
		next.c=t;
		if(!judge(next.a,next.b,next.c,next.d))
		{
			next.step=cur.step+1;
			map[next.a][next.b][next.c][next.d]=1;
			q.push(next);
		}
	}
	return -1;
}


int main()
{
	int T;
	int i;


	scanf("%d",&T);
	while(T--)
	{
		scanf("%s%s",s,ans);
		for(i=0;i<4;i++)	{s[i]-='0';ans[i]-='0';}


		memset(map,0,sizeof(map));
		printf("%d\n",BFS());
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值