POJ~1077~八数码~双BFS解题报告

八数码:

题目描述:

The 15-puzzle has been around for over 100 years; even if you don't know it by that name, you've seen it. It is constructed with 15 sliding tiles, each with a number from 1 to 15 on it, and all packed into a 4 by 4 frame with one tile missing. Let's call the missing tile 'x'; the object of the puzzle is to arrange the tiles so that they are ordered as: 
 1  2  3  4 

 5  6  7  8 

 9 10 11 12 

13 14 15  x 

where the only legal operation is to exchange 'x' with one of the tiles with which it shares an edge. As an example, the following sequence of moves solves a slightly scrambled puzzle: 
 1  2  3  4    1  2  3  4    1  2  3  4    1  2  3  4 

 5  6  7  8    5  6  7  8    5  6  7  8    5  6  7  8 

 9  x 10 12    9 10  x 12    9 10 11 12    9 10 11 12 

13 14 11 15   13 14 11 15   13 14  x 15   13 14 15  x 

           r->           d->           r-> 

The letters in the previous row indicate which neighbor of the 'x' tile is swapped with the 'x' tile at each step; legal values are 'r','l','u' and 'd', for right, left, up, and down, respectively. 

Not all puzzles can be solved; in 1870, a man named Sam Loyd was famous for distributing an unsolvable version of the puzzle, and 
frustrating many people. In fact, all you have to do to make a regular puzzle into an unsolvable one is to swap two tiles (not counting the missing 'x' tile, of course). 

In this problem, you will write a program for solving the less well-known 8-puzzle, composed of tiles on a three by three 
arrangement. 

Input:

You will receive a description of a configuration of the 8 puzzle. The description is just a list of the tiles in their initial positions, with the rows listed from top to bottom, and the tiles listed from left to right within a row, where the tiles are represented by numbers 1 to 8, plus ‘x’. For example, this puzzle
1 2 3

x 4 6

7 5 8

is described by this list:

1 2 3 x 4 6 7 5 8

Output:

You will print to standard output either the word ``unsolvable’’, if the puzzle has no solution, or a string consisting entirely of the letters ‘r’, ‘l’, ‘u’ and ‘d’ that describes a series of moves that produce a solution. The string should include no spaces and start at the beginning of the line.。

Sample Input:

 2  3  4  1  5  x  7  6  8 

Sample Output:

ullddrurdllurdruldr

题目大意:

其实这道题很简单理解,就是简单的九宫格排序问题,而只能移动上下左右这四个方向,并且还要满足1 2 3 4 5 6 7 8 x这样的排序即可,所以这道题可以用双BFS+康托正向定理去解就很快解了。

思路分析:

在这里插入图片描述
这道题有许多种解法,这里讲一下双BFS解法,这里我们可以想象有一颗搜索树,我们将其中心对称复制多一颗树,这样就有2颗树,一颗树相对于另一颗树,它所操作都是相对于是反方向的,那么就可以想象有一棵搜索树为1 2 3 4 5 6 7 8 9,一颗树是题目所给输入依次,把x当做9看待,当这2颗树所搜的Hash值被2者访问过,说明就找到了目标了(可以结合上面图来看),具体看代码,下面给出Hash(康托定理的原理):

康托定理:

X=a[n]*(n-1)!+a[n-1]*(n-2)!+...+a[i]*(i-1)!+...+a[1]*0!,
其中a[i]为当前未出现的元素中是排在第几个(从0开始)。这就是康
托展开

 1. 举个简单的例子吧,例如(1234)这四种数字全排列,如果要
 求2134的排在全排列第几个,可以这样算:

 2. 首位是2,比他小的有1,那么就有1*4-1)!。
 3. 第二位是1,没有比他小,那么就有0*4-2)!。
 4. 第三位是3,比他小的有 12,但是12都已经出现在前面了,所以
0*4-3!
 5. 第四位是4,比他小的有123,但是都出现在前面,所以有0*(0)!
 6. 最后将其全部相加就是该数字在全排列中的排序。

代码:

#include<iostream>
#include<map>
#include<queue>
#include<stack>
#include<cstdio>
#include<set>
#include<vector>
#include<algorithm>
#include<string.h>
#include<string> 
#define me(a,b) memset((a),(b),sizeof((a)))//定义memset 
using namespace std;
typedef struct Node
{
	int num;//Hash值 
	int ans;//位置 
	int s;//记录次数 
	int c1[10];//记录九宫格元素 
}Lemon;
const int maxn=364000;//最大为!9 
int visit1[maxn],visit2[maxn];//分别为正方向和反方向的标记 
int pre1[maxn],pre2[maxn];//分别为正方向和反方向的父节点 
char pr1[maxn],pr2[maxn];//分别为正方向和反方向 
int di[5][2]={{1,0},{-1,0},{0,1},{0,-1}};//正方向 
char di2[5]={"durl"};//正方向符号变量 
int di1[5][2]={{0,-1},{0,1},{-1,0},{1,0}};//反方向 
char di3[5]={"rldu"};//反方向符号变量 
int fc[]={1,1,2,6,24,120,720,5040,40320,362880};//0~9的阶乘 
int Hash(int s1[])//康托定理正向解密(相当于找出Hash值) 
{
	int sum=1;
	for(int i=0;i<9;i++)
	{
		int temp=0;
		for(int j=i+1;j<9;j++)
		{
			if(s1[j]<s1[i])
			{
				temp++;
			}
		}
		sum+=temp*fc[9-i-1];
	}
	return sum;
}
int LemonDoubleBFS(Lemon Frist)
{
	me(visit1,0);
	me(visit2,0);
	//这2步可以不用,因为他只要一次输出 
	queue<Lemon> q;
	queue<Lemon> p;
	//创建正反队列 
	//正为q,反为p 
	visit1[Frist.num]=1; 
	pre1[Frist.num]=-1;
	Frist.s=0;//记录所要的次数 
	q.push(Frist);//将正方向的队列入队 
	for(int i=0;i<9;i++)
	{
		Frist.c1[i]=i+1;//这里使得反方向从1开始 
	}
	Frist.s=0;
	Frist.num=Hash(Frist.c1);
	Frist.ans=8;
	visit2[Frist.num]=1;
	pre2[Frist.num]=-1;
	p.push(Frist);//将反方向的队列入队 
	int time=0;//记录次数 
	Lemon now;
	while(!q.empty() || !p.empty())
	{
		time++;
		while(!q.empty())
		{
			now = q.front();
			q.pop();
			if(time == now.s)
			{
				break;
			}
			if(visit2[now.num])//如果反方向队列找过这个点,说明可以退出了 
			{
				return now.num;
			}
			int x1=now.ans/3;
			int y1=now.ans%3;
			//找出位置 
			for(int i=0;i<4;i++)
			{
				Lemon tp=now;
				int x2=x1+di[i][0];
				int y2=y1+di[i][1];
				if(x2<0 || y2<0 || x2>2 || y2>2)//判断边界 
				{
					continue;
				}
				else
				{
					tp.ans=x2*3+y2;//改变位置 
					tp.c1[now.ans]=tp.c1[tp.ans];
					tp.c1[tp.ans]=9;
					//改变2个板块的元素 
					tp.num=Hash(tp.c1);//找出Hash值 
					if(!visit1[tp.num])//判断是否出现过 
					{
						tp.s++;
						visit1[tp.num]=1;
						pr1[tp.num]=di2[i];//记录符号变量 
						pre1[tp.num]=now.num;//记录父子点 
						q.push(tp);//入队 
					}
				}
			}
		}
		//因为正方向和反方向是对称,所以操作几乎一样。所以下方不解释 
		while(!p.empty())
		{
			now = p.front();
			p.pop();
			if(time == now.s)
			{
				break;
			}
			if(visit1[now.num])
			{
				return now.num;
			}
			int x1=now.ans/3;
			int y1=now.ans%3;
			for(int i=0;i<4;i++)
			{
				Lemon tp=now;
				int x2=x1+di1[i][0];
				int y2=y1+di1[i][1];
				if(x2<0 || y2<0 || x2>2 || y2>2)
				{
					continue;
				}
				else
				{
					tp.ans=x2*3+y2;
					tp.c1[now.ans]=tp.c1[tp.ans];
					tp.c1[tp.ans]=9;
					tp.num=Hash(tp.c1);
					if(!visit2[tp.num])
					{
						tp.s++;
						visit2[tp.num]=1;
						pr2[tp.num]=di3[i];
						pre2[tp.num]=now.num;
						p.push(tp);
					}
				}
			}
		}
	}
	return -1;
}
void print1(int g)//输出正方向 
{
	if(pre1[g]==-1)
	{
		return;
}
		print1(pre1[g]);
		putchar(pr1[g]);
}
void print2(int g)//输出反方向的 
{
	if(pre2[g]==-1)
	{
		return;
	}
		putchar(pr2[g]);
		print2(pre2[g]);
}
int main()
{
	Lemon Frist;
	char ch;
	for(int i=0;i<9;i++)
	{
		cin >> ch;
		if(ch=='x')
		{
			Frist.c1[i]=9;
			Frist.ans=i;
		}
		else
		{
			Frist.c1[i]=ch-'0';
		}
	}
	Frist.num=Hash(Frist.c1);
	int g=LemonDoubleBFS(Frist);
	if(g==-1)//判断是否有结果 
	{
		cout << "unsolvable";
	}
	else//这里讲一下正方向和反方向的回溯是不一样的。 
	{
		print1(g);
		print2(g);
	}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值