Problem Description
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, several descriptions of configuration of the 8 puzzle. One 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. Do not print a blank line between cases.

 
Sample Input
2 3 4 1 5 x 7 6 8
 
Sample Output
ullddrurdllurdruldr
#include<stdio.h>
#include<iostream>
#include<queue>
using namespace std;
typedef struct nn
{
char way;//记录路径
int fath;//记录父节点
}node1;
typedef struct nod
{
int aa[10];
int n,son;//n为9在aa中的位置
}node2;
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}},fac[10];
node1 Node[370000];//节点
void set_fac()//计算0到8的阶层
{
fac[0]=1;
for(int i=1;i<=8;i++)
fac[i]=fac[i-1]*i;//printf("%d",fac[8]);
}
int cantor(int aa[])//康托展开
{
int i,j,ans=0,k;
for(i=0;i<9;i++)
{
k=0;
for(j=i+1;j<9;j++)
if(aa[i]>aa[j])
k++;
ans+=k*fac[8-i];
}
return ans;
}
void bfs(int a[])
{
queue<node2>Q;
node2 q,p;
int e,tx,ty,tem,t=0;
for(e=0;e<9;e++) q.aa[e]=a[e];
q.n=8;q.son=0;
Node[q.son].fath=0;//把最终父节点记为0,也就是本身
Q.push(q);
while(!Q.empty())
{
q=Q.front(); Q.pop();
for(e=0;e<4;e++)
{
p=q;
tx=q.n%3+dir[e][0];ty=q.n/3+dir[e][1];
if(tx>=0&&ty>=0&&tx<3&&ty<3)
{
p.n=ty*3+tx;
tem=p.aa[p.n];p.aa[p.n]=p.aa[q.n];p.aa[q.n]=tem;
p.son=cantor(p.aa);
if(Node[p.son].fath==-1)//为-1时表示这个点没有访问过,那么放入队列
{
Node[p.son].fath=q.son;//当前节点的父节点就是上一个节点
if(e==0)Node[p.son].way='l';//一定要注意了,e=0是向右走,但我们是要往回搜,所以为了在输出时不用再进行转换,直接记录相反的方向
if(e==1)Node[p.son].way='r';
if(e==2)Node[p.son].way='u';
if(e==3)Node[p.son].way='d';
Q.push(p);
}
}
}
}
}
int main()
{
int i,j,s,ss[10],a[10];
char ch[50] ;
for(i=0;i<9;i++)//目标
a[i]=i+1;
for(i=0;i<370000;i++)
Node[i].fath=-1;
set_fac();//计算阶层
bfs(a);//开始从目标建立一树 while(gets(ch)>0)
{
for(i=0,j=0;ch[i]!='\0';i++)//把字符串变成数子
{
if(ch[i]=='x')
ss[j++]=9; //把x变为数子9
else if(ch[i]>='0'&&ch[i]<='8')
ss[j++]=ch[i]-'0';
}
s=cantor(ss);//算出初态康托值
if(Node[s].fath==-1) {printf("unsolvable\n");continue;}//不能变成目标 while(s!=0)
{
printf("%c",Node[s].way);
s=Node[s].fath;
}
printf("\n");
}
}
/*
1 2 3 4 5 6 7 8 x 2 1 4 3 5 x 6 8 7
unsolvable
2 1 4 3 5 x 6 8 7
drdlurdruldruuldlurrdd
8 5 6 4 x 3 4 1 2
rulddruulddluurddrulldrurd
8 5 6 4 x 3 4 1 2
urdluldrurdldruulddluurddr */

 

hdu1043Eight (经典的八数码)(康托展开+BFS)的更多相关文章

  1. hdu1043 经典的八数码问题 逆向bfs打表 + 逆序数

    题意: 题意就是八数码,给了一个3 * 3 的矩阵,上面有八个数字,有一个位置是空的,每次空的位置可以和他相邻的数字换位置,给你一些起始状态 ,给了一个最终状态,让你输出怎么变换才能达到目的. 思路: ...

  2. HDU 1043 Eight 【经典八数码输出路径/BFS/A*/康托展开】

    本题有写法好几个写法,但主要思路是BFS: No.1 采用双向宽搜,分别从起始态和结束态进行宽搜,暴力判重.如果只进行单向会超时. No.2 采用hash进行判重,宽搜采用单向就可以AC. No.3 ...

  3. HDU1043 Eight(八数码:逆向BFS打表+康托展开)题解

    Eight Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  4. hdu 1043 Eight (八数码问题)【BFS】+【康拓展开】

    <题目链接> 题目大意:给出一个3×3的矩阵(包含1-8数字和一个字母x),经过一些移动格子上的数后得到连续的1-8,最后一格是x,要求最小移动步数. 解题分析:本题用BFS来寻找路径,为 ...

  5. [HDOJ1043]Eight(康托展开 BFS 打表)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1043 八数码问题,因为固定了位置所以以目标位置开始搜索,把所有情况(相当于一个排列)都记录下来,用康托 ...

  6. HDU 1430 魔板(康托展开+BFS+预处理)

    魔板 Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submis ...

  7. HDU 1043 & POJ 1077 Eight(康托展开+BFS+预处理)

    Eight Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 30176   Accepted: 13119   Special ...

  8. HDU 3567 Eight II 打表,康托展开,bfs,g++提交可过c++不可过 难度:3

    http://acm.hdu.edu.cn/showproblem.php?pid=3567 相比Eight,似乎只是把目标状态由确定的改成不确定的,但是康托展开+曼哈顿为h值的A*和IDA*都不过, ...

  9. poj1077(康托展开+bfs+记忆路径)

    题意:就是说,给出一个三行三列的数组,其中元素为1--8和x,例如: 1 2 3 现在,需要你把它变成:1 2 3 要的最少步数的移动方案.可以右移r,左移l,上移u,下移dx 4 6 4 5 67 ...

随机推荐

  1. Fizz Buzz

    class Solution { public: /** * param n: As description. * return: A list of strings. */ vector<st ...

  2. OnClose()和 OnDestroy()

    OnClose()和 OnDestroy() 基于对话框的MFC程序,发现每次程序退出时,托盘的小图标不能自动消失,鼠标移上去之后才能消失,比较不爽. 后来发现我删除这个图标的代码是在自己重写的OnC ...

  3. 【产品对比分析】See做了明星衣橱想做的东西?

    不断地发现.联想.思考,让学到的东西互通起来吧!  先来两张See的界面图镇楼——          See简介: See是一个专注找同款的时尚社区,主打功能是一键拍照找同款,由社区为你提供最佳商品或 ...

  4. jQuery分页插件jBootstrapPage,一个Bootstrap风格的分页插件

    一个Bootstrap风格的分页控件,对于喜欢Bootstrap简洁美观和扁平化的同学可以关注jBootstrapPage, 目前jBootstrapPage最新版为V0.1,后续还有更多功能需要完善 ...

  5. git实现版本回退

    1. 首先查看自己的版本: ***:~/piaoshifu_object/epiao.piaoshifu.cn$ git log commit c8d5c67861d2d0e21856cc2b4f60 ...

  6. ibatis动态的传入表名、字段名

    ibatis动态的传入表名.字段名,主要传入表名和字段名的不一致. Java代码: Map<String,Object> params = new HashMap<String,Ob ...

  7. I2C总线之(三)---以C语言理解IIC

    为了加深对I2C总线的理解,用C语言模拟IIC总线,边看源代码边读波形: 如下图所示的写操作的时序图: 读时序的理解同理.对于时序不理解的朋友请参考“I2C总线之(二)---时序” 完整的程序如下: ...

  8. RSA算法原理(一)

    如果你问我,哪一种算法最重要? 我可能会回答"公钥加密算法". 因为它是计算机通信安全的基石,保证了加密数据不会被破解.你可以想象一下,信用卡交易被破解的后果. 进入正题之前,我先 ...

  9. BlockingQueue队列学习

    今天看了下BlockingQueue的几种实现,记录下以便以后复习. 首先来看一下BlockingQueue的家族成员: BlockingQueue除了先进先出外,还有两个操作:在队列为空时,获取元素 ...

  10. wireshark设置抓服务器的包

    wireshark设置抓服务器的包: