八中oj——2176 【NOI2001】聪明的打字员 代码

#include
#include
#include
#include
#include
#include
using namespace std;

struct node
{
	char s[7];
	int w,d;
}first,aim;
queue  que;
bool vis[7][10][10][10][10][10][10];
int  bfs()
{
	que.push(first);
	vis[first.w][first.s[0]-'0'][first.s[1]-'0'][first.s[2]-'0'][first.s[3]-'0'][first.s[4]-'0'][first.s[5]-'0']=1;
	while (!que.empty())
	{
		node p=que.front();
		que.pop();
		if(strcmp(p.s,aim.s) == 0)
			return p.d;
		p.d++;
		if(p.w>0)
		{
			swap(p.s[0],p.s[p.w]);
			if(vis[p.w][p.s[0]-'0'][p.s[1]-'0'][p.s[2]-'0'][p.s[3]-'0'][p.s[4]-'0'][p.s[5]-'0']==0)
			{
				que.push(p);
				vis[p.w][p.s[0]-'0'][p.s[1]-'0'][p.s[2]-'0'][p.s[3]-'0'][p.s[4]-'0'][p.s[5]-'0']=1;
			}
			swap(p.s[0],p.s[p.w]);
			
			p.w-=1;
			if(vis[p.w][p.s[0]-'0'][p.s[1]-'0'][p.s[2]-'0'][p.s[3]-'0'][p.s[4]-'0'][p.s[5]-'0']==0)
			{
				que.push(p);
				vis[p.w][p.s[0]-'0'][p.s[1]-'0'][p.s[2]-'0'][p.s[3]-'0'][p.s[4]-'0'][p.s[5]-'0']=1;
			}
			p.w+=1;
		}
		if(p.w<5)
		{
			swap(p.s[5],p.s[p.w]);
			if(vis[p.w][p.s[0]-'0'][p.s[1]-'0'][p.s[2]-'0'][p.s[3]-'0'][p.s[4]-'0'][p.s[5]-'0']==0)
			{
				que.push(p);
				vis[p.w][p.s[0]-'0'][p.s[1]-'0'][p.s[2]-'0'][p.s[3]-'0'][p.s[4]-'0'][p.s[5]-'0']=1;
			}
			swap(p.s[5],p.s[p.w]);
			p.w+=1;
			if(vis[p.w][p.s[0]-'0'][p.s[1]-'0'][p.s[2]-'0'][p.s[3]-'0'][p.s[4]-'0'][p.s[5]-'0']==0)
			{
				que.push(p);
				vis[p.w][p.s[0]-'0'][p.s[1]-'0'][p.s[2]-'0'][p.s[3]-'0'][p.s[4]-'0'][p.s[5]-'0']=1;
			}
			p.w-=1;
		}
		if(p.s[p.w]<'9')
		{
			p.s[p.w]+=1;
			if(vis[p.w][p.s[0]-'0'][p.s[1]-'0'][p.s[2]-'0'][p.s[3]-'0'][p.s[4]-'0'][p.s[5]-'0']==0)
			{
				que.push(p);
				vis[p.w][p.s[0]-'0'][p.s[1]-'0'][p.s[2]-'0'][p.s[3]-'0'][p.s[4]-'0'][p.s[5]-'0']=1;
			}
			p.s[p.w]-=1;
		}
		if(p.s[p.w]>'0')
		{
			p.s[p.w]-=1;
			if(vis[p.w][p.s[0]-'0'][p.s[1]-'0'][p.s[2]-'0'][p.s[3]-'0'][p.s[4]-'0'][p.s[5]-'0']==0)
			{
				que.push(p);
				vis[p.w][p.s[0]-'0'][p.s[1]-'0'][p.s[2]-'0'][p.s[3]-'0'][p.s[4]-'0'][p.s[5]-'0']=1;
			}
			p.s[p.w]+=1;
		}
	}
	return -1;
}

int main()
{
	scanf("%s",first.s);
	scanf("%s",aim.s);
	first.w=first.d=0;
	int ans=bfs();
	printf("%d",ans);
	return 0;
}

你可能感兴趣的:(c++)