poj 1077 八数码

这道题花了我一晚上去了看了一种解法,结果最后悲剧了,只在poj上过了,在hdu上TLE,原因是因为hdu上是多组数

据,而poj上是一组数据。。。悲剧啊,学的方法有点低效。。。

不过那个打印路径方法倒是可以借鉴一下,从终点往起点递归,打印路径。。。

贴代码:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
using namespace std;
#define N 370000
int first[9];
int last[9] = {1,2,3,4,5,6,7,8,0}; 
int f[9];
int visit[N];
int p[N][9];//保存状态的数组 
int dist[N];//记录路径 
const int dx[] = {-1,0,0,1};
const int dy[] = {0,-1,1,0};
char dir[4] = {'d','r','l','u'};
void init()//好像是一个叫做康托展开的神马东东 
{
	f[0] = 1;
	for(int i=1; i<10; i++)
		f[i] = f[i-1] * i;
} 
int hash1(int *a)//这个是求该数字0-9在全排列中的第几个数,用来判重 
{
	int i,j;
	int s = 0;
	for(i=0; i<9; i++)
	{
		int cnt = 0;
		for(j=i+1; j<9; j++)
		{
			if(a[j] < a[i])
				cnt++;
		}
		s += f[8-i] * cnt;
	}
	return s;
}
int bfs(int fir,int las)
{
	int i,cur;
	memset(visit,0,sizeof(visit));
	memset(dist,0,sizeof(dist));
	memset(p,0,sizeof(p));
	visit[fir] = 1;
	dist[fir] = 0;
	for(i=0; i<9; i++)
	p[fir][i] = first[i];
	queue<int>que;
	que.push(fir);
	while(!que.empty())
	{
		int temp = que.front();
		que.pop();
		if(temp == las)
			return dist[las];
		for(cur=0; cur<9; cur++)
		{
			if(p[temp][cur] == 0)
				break;
		}
		int x = cur/3;
		int y = cur%3;
		int change[9];
		for(i=0; i<4; i++)
		{
			int xx = x + dx[i];
			int yy = y + dy[i];
			if(( xx>=0 ) && ( yy>=0 ) && (xx < 3) && (yy < 3))
			{
				memcpy(change,p[temp],sizeof(change));
				change[cur] = p[temp][xx*3+yy];
				change[xx*3+yy] = 0;
				int change1 = hash1(change);
				if(visit[change1] == 0)
				{
					visit[change1] = 1;
					dist[change1] = dist[temp] + 1;
					que.push(change1);
					memcpy(p[change1],change,sizeof(change));
				}
			}
		}
	}
	return -1;
}
void print_path(int fir,int las)//从终点递归打印路径 
{
	if(las == fir)
		return;
	int cur;
	int change[9];
	for(cur=0; cur<9; cur++)
	{
		if(p[las][cur] == 0)
			break;
	}
	int x = cur/3;
	int y = cur%3;
	for(int i=0; i<4; i++)
	{
		int xx = x + dx[i];
		int yy = y + dy[i];
		if(( xx>=0 ) && ( yy>=0 ) && (xx < 3) && (yy < 3))
		{
			memcpy(change,p[las],sizeof(change));
			change[cur] = p[las][xx*3+yy];
			change[xx*3+yy] = 0;
			int change1 = hash1(change);
			if((dist[las] == dist[change1] +1) && visit[change1])
			{
				print_path(fir, change1);
				printf("%c",dir[i]);
				break;
			}
		}
	}
	return ;
}
int main()
{
	init();
	int i,k = 0; 
	char a[30];
	while(gets(a))
	{
		//getchar();
		k = 0;
		int len = strlen(a);
		for(i=0; i<len; i++)
		{
			if((a[i] >= '1') && (a[i] <= '8'))
				first[k++] = a[i] - '0';
			else if(a[i] == 'x')
				first[k++] = 0;
		}
		//for(i=0; i<9; i++)
		//	printf("%d ",first[i]);
		puts("");
		int fir = hash1(first);//找出某一序列所对应的全排列的位次 
		int las = hash1(last);
		int ans = bfs(fir,las);
	//	printf("%d\n",ans);
		if(ans < 0)
			printf("unsolvable");
		else
			print_path(fir,las);
		puts("");
	}
	
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值