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

Difficulty:

1.康拓展开:类似于状态压缩将一个数组映射为一个数。

适用条件:当知道这个数组的个数是一定的。

2.一位数组和二维数组的互换。

http://wenku.baidu.com/link?url=YeNqa-MsEwqOZTvOR__alZS12Xog8OZDlnIXVA5WORuM0lCPu9LmkFrSsbYFBuFVj_h5F0hf6I4NVZD076lwG_xEAmWJft2HtRKWMIWAkBa

思路:将二维数组看成一个状态,再进行bfs,同时要将二维数组先变形为一位数组再根据康拓展开变形为一个数。

打表,反向搜索。

注意:这里有多解,不止一个答案,方向顺序不同,答案不同。

#include <cstdio>
#include <iostream>
#include<cstring>
using namespace std;
struct Node
{
int a[9];
}Q[500000];//转态表示
int n,eid,cnt,flag,x0,ya,z0,tmpid,x,y;
int vis[500000];
Node s,t;
char ss[5];
int dx[]={0,0,-1,1};
int dy[]={1,-1,0,0};
char dir[]="lrdu";//反向搜索,所以反着写。
char di[500000];
int A[9];
int net[500000];
int getid(int *a)//康拓展开
{
int v=0;
for(int i=0;i<=8;i++)
{
int cnt=0;
for(int j=i+1;j<=8;j++)
if(a[i]>a[j])
cnt++;
v+=cnt*A[8-i];
}
return v;
}
void bfs()
{
memset(vis,0,sizeof(vis));
int front=0,rear=0;
Q[rear++]=s;
//net[eid]=-1;
vis[eid]=1;
while(front<rear)
{
Node q=Q[front++];
tmpid=getid(q.a);//一维数组映射为一个数
for(int i=0;i<9;i++)
{
if(q.a[i]==0)
{x0=i/3;
ya=i%3;//一维数组转换为二维数组。
z0=i;
break;
}}
Node add=q;;
for(int i=0;i<4;i++)
{
x=x0+dx[i];
y=ya+dy[i];
if(x<0||x>=3||y<0||y>=3)
continue;
int z=x*3+y;//二维数组转换为一维数组
add.a[z0]=q.a[z];
add.a[z]=0;
int qid=getid(add.a);
if(vis[qid]==0)
{
vis[qid]=1;
net[qid]=tmpid;//转态转移
di[qid]=dir[i];//记录操作
Q[rear++]=add;
}
add=q;
}
//front++;
}
}
int main()
{ A[0]=1;
for(int i=1;i<=8;i++)
A[i]=A[i-1]*i;//康拓展模板
for(int i=0;i<=8;i++)
if(i==8)
s.a[i]=0;
else
s.a[i]=i+1;
eid=getid(s.a);
bfs();
while(~scanf("%s",ss))
{
if(ss[0]=='x')
s.a[0]=0;
else
s.a[0]=ss[0]-'0';
for(int i=1;i<=8;i++)
{
scanf("%s",ss);//可过滤空格
if(ss[0]=='x')
s.a[i]=0;
else
s.a[i]=ss[0]-'0';
}
int sid=getid(s.a);
if(vis[sid])
{
while(sid!=eid)//上面打的表中推出一步一步的转态转换。
{
putchar(di[sid]);
sid=net[sid];
}
}
else
printf("unsolvable");
puts("");
}
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

Eight(South Central USA 1998)(八数码) 分类: bfs 2015-07-05 22:34 1人阅读 评论(0) 收藏的更多相关文章

  1. C/C++文字常量与常变量的概念与区别 分类: C/C++ 2015-06-10 22:56 111人阅读 评论(0) 收藏

    以下代码使用平台是Windows 64bits+VS2012. 在C/C++编程时,经常遇到以下几个概念:常量.文字常量.符号常量.字面常量.常变量.字符串常量和字符常量,网上博客资料也是千篇千律,不 ...

  2. 快速幂取模 分类: ACM TYPE 2014-08-29 22:01 95人阅读 评论(0) 收藏

    #include<stdio.h> #include<stdlib.h> //快速幂算法,数论二分 long long powermod(int a,int b, int c) ...

  3. Java 函数参数传递方式详解 分类: Java Game 2014-08-15 06:34 82人阅读 评论(0) 收藏

    转:http://zzproc.iteye.com/blog/1328591 在阅读本文之前,根据自己的经验和理解,大家可以先思考并选择一下Java函数的参数传递方式:  A. 是按值传递的?  B. ...

  4. Hibernate检索方式 分类: SSH框架 2015-07-10 22:10 4人阅读 评论(0) 收藏

    我们在项目应用中对数据进行最多的操作就是查询,数据的查询在所有ORM框架中也占有极其重要的地位.那么,如何利用Hibernate查询数据呢?Hibernate为我们提供了多种数据查询的方式,又称为Hi ...

  5. iOS自定义字体及类目 分类: ios技术 2015-05-15 16:34 195人阅读 评论(0) 收藏

    1:获取字体文件 从各种渠道下载字体文件ttf, 网站或者从别的ipa里扣出来.(以fzltxh.ttf为例) 2:将fzltxh.ttf文件拷贝到工程中 3:在Info.plist中添加项: Fon ...

  6. 全方位分析Objcetive-C Runtime 分类: ios技术 2015-03-11 22:29 77人阅读 评论(0) 收藏

    本文详细整理了 Cocoa 的 Runtime 系统的知识,它使得 Objective-C 如虎添翼,具备了灵活的动态特性,使这门古老的语言焕发生机.主要内容如下: 引言 简介 与Runtime交互 ...

  7. IOS之富文本编辑 分类: ios技术 2015-03-06 22:51 89人阅读 评论(0) 收藏

    之前做项目时遇到一个问题:          使用UITextView显示一段电影的简介,由于字数比较多,所以字体设置的很小,行间距和段间距也很小,一大段文字挤在一起看起来很别扭,想要把行间距调大,结 ...

  8. 【从0到1学Web前端】CSS伪类和伪元素 分类: HTML+CSS 2015-06-02 22:29 1065人阅读 评论(0) 收藏

    1.CSS中的伪类 CSS 伪类用于向某些选择器添加特殊的效果. 语法: selector : pseudo-class {property: value} CSS 类也可与伪类搭配使用 select ...

  9. CSS_Spirte实现原理 分类: HTML+CSS 2015-04-28 22:58 531人阅读 评论(0) 收藏

    CSS Spirte就是所谓的把很多的小图标合并成一张大的图片,然后使用CSS的background-position属性,来动态的定位自己需要图标的位置.这样做的目的主要是减少HTTP请求,加快网页 ...

随机推荐

  1. OPStackComputeNodeMaintain

    1,yum -y install openstack-nova-compute计算节点配置完成后 其配置文件默认非注释行内容如下;

  2. 【转载】视频编码(H264概述)

    一视频编码介绍 1.1 视频压缩编码的目标 1)保证压缩比例 2)保证恢复的质量 3)易实现,低成本,可靠性 1.2 压缩的出发点(可行性) 1)时间相关性 在一组视频序列中,相邻相邻两帧只有极少的不 ...

  3. Chosen 基本使用

    点击下载Chosen 引入文件 chosen.css jquery-1.7.1.min.js chosen.jquery.js 绑定数据: for (var i = 0; i < data.le ...

  4. Monkey Tradition(中国剩余定理)

    Monkey Tradition Time Limit: 2000MS   Memory Limit: 32768KB   64bit IO Format: %lld & %llu Submi ...

  5. [HeadFirst-HTMLCSS学习笔记][第六章严格的HTML]

    远古 古老的html 4.01和XHTML 1.1 页面 必须用Doctype挑明,再html元素上面 html 4.01 <!DOCTYPE html PUBLIC "-//W3C/ ...

  6. VS删除未使用的命名空间

    把鼠标光变放在Using+命名空间区域,右键——组织 using->移除未使用的 using

  7. 自定义上传按钮 <input type="file" name = "file"/> (将file隐藏在button下)

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  8. JS如何判断IE和火狐与Chrome浏览器

    var isIE=navigator.userAgent.toUpperCase().indexOf("MSIE")?true:false; 类似的可以写var isFirefox ...

  9. HTML5 本地裁剪图片并上传至服务器(转)

    很多情况下用户上传的图片都需要经过裁剪,比如头像啊什么的.但以前实现这类需求都很复杂,往往需要先把图片上传到服务器,然后返回给用户,让用户确定裁剪坐标,发送给服务器,服务器裁剪完再返回给用户,来回需要 ...

  10. 0119——UIImageView的一些属性 和 简单动画实现

    1.contentMode view.contentMode = UIViewContentModeScaleAspectFill; 2.是否实现触摸 3.简单实现动画 图片的名字为campFire0 ...