HDU 1043 双向广搜 八数码 康托展开 逆序数

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;
char c;
int index[10] = {1,1,2,6,24,120,720,5040,40320};
int dir[4][2] = {1,0,-1,0,0,1,0,-1};
int vis[400000],found,state,father1[400000],father2[400000],move1[400000],move2[400000];
struct node
{
	int state,loc;
	char map[10];
}s1,s2;
queue<node>q1;
queue<node>q2;
int ct(char map[])
{
	int ans = 0;
	for(int i = 0; i < 9; i++)
	{
		int num = 0;
		for(int j = i + 1; j < 9; j++)
		{
			if(map[i] > map[j]) num++;
		}
		ans += num * index[8 - i];
	}
	return ans;
}
void expend(queue<node> &q,int flag)
{
	int x,y,xx,yy,k;
	node cur,end;
	cur = q.front();
	q.pop();
	k = cur.loc;
	x = k / 3;
	y = k % 3;
	for(int i = 0; i < 4; i++)
	{
		
		xx = x + dir[i][0];
		yy = y + dir[i][1];

		if(xx >= 0 && yy >= 0 && xx < 3 && yy < 3)
		{
			end = cur;
			end.loc = xx*3 + yy;
			end.map[cur.loc] = cur.map[end.loc];
			end.map[end.loc] = 9;			
			end.state = ct(end.map);
			if(flag == 1 && vis[end.state] != 1)
			{
				move1[end.state] = i;
				father1[end.state] = cur.state; 
				if(vis[end.state] == 2) 
				{
					found = 1;
					state = end.state;
					return;
				}
				vis[end.state] = 1;
				q.push(end);
			}
			else if(flag == 2 && vis[end.state] != 2)
			{
				move2[end.state] = i;
				father2[end.state] = cur.state; 				
				if(vis[end.state] == 1)
				{
					found = 1;
					state = end.state;
					return;
				}
				vis[end.state] = 2;
				q.push(end);				
			}
		}
	}
}
int ReverseOrder(char map[])
{
	int ans = 0;
	for(int i = 0; i< 9; i++)
	{
		if(map[i] == 9) continue;
		for(int j = i + 1; j < 9; j++)
		{
			if(map[j] == 9) continue;
			if(map[i] > map[j]) ans++;
		}
	}
	return ans;
}
void read()
{
	int i = 0;
	if(c == 'x') 
	{
		s1.map[i++] = 9;
		s1.loc = 0; 
	}
	else if(c < '9' && '0' < c)
	{
		s1.map[i++] = c - '0';
	}
	while(scanf("%c",&c))
	{
		if(c == 'x') 
		{
			s1.map[i++] = 9;
			s1.loc = i - 1; 
		}
		else if(c < '9' && '0' < c)
		{
			s1.map[i++] = c - '0';
		}		
		if(i == 9) return;
	}
}
void bfs2()
{
	while(!q1.empty()) q1.pop();
	while(!q2.empty()) q2.pop();
	q1.push(s1);
	q2.push(s2);
	while(!q1.empty() || !q2.empty())
	{
		if(!q1.empty()) expend(q1,1);
		if(found) return;
		if(!q2.empty()) expend(q2,2);
		if(found) return;
	}
}
void path1(int st)
{
	if(father1[st] != -1)
	{
		path1(father1[st]);
		if(move1[st] == 0) printf("d");
		else if(move1[st] == 1) printf("u");
		else if(move1[st] == 2) printf("r");
		else if(move1[st] == 3) printf("l");
	}
	return;
}
void path2(int st)
{
	while(father2[st] != -1)
	{
		if(move2[st] == 0) printf("u");
		else if(move2[st] == 1) printf("d");
		else if(move2[st] == 2) printf("l");
		else if(move2[st] == 3) printf("r");		
		st = father2[st];
	}
}
int main()
{
//	freopen("t.txt","r",stdin);
	int i;
	while(~scanf(" %c",&c))
	{
		found = 0;
		memset(vis,0,sizeof(vis));
              if(c=='x')
              {
                     s1.map[0]=9;
                     s1.loc=0;
              }
              else
                     s1.map[0]=c-'0';
              for(i=1;i<9;i++)
              {
                     scanf(" %c",&c);
                     if(c=='x')
                     {
                            s1.map[i]=9;
                            s1.loc=i;
                     }
                     else
                            s1.map[i]=c-'0';
              }
		
		s1.state = ct(s1.map);  vis[s1.state] = 1;  father1[s1.state] = -1;
		if(s1.state == s2.state) 
		{
			puts("");
			continue;
		}
		if(ReverseOrder(s1.map) % 2 == 1) 
		{
			printf("unsolvable\n");
			continue;
		}
		for(i = 0; i < 9; i++)
		{
			s2.map[i] = i + 1;	
		}
		s2.loc = 8;
		s2.state = ct(s2.map);  vis[s2.state] = 2;  father2[s2.state] = -1; 
		bfs2();
        if(found == 0) 
        {
        	printf("unsolvable\n");
        	continue;
        }
        path1(state);
        path2(state);
        printf("\n");
		
	}
} 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值