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. poj 1503 大数相加(java)

    代码: import java.math.*; import java.util.Scanner; public class Main { public static void main(String ...

  2. Android应用开发学习之Toast消息提示框

    作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz 本文我们来看Toast消息提示框的用法.使用Toast消息提示框一般有三个步骤: 1.  创建一个Toast对象.可 ...

  3. js网页返回页面顶部的小方法

    咳咳,在网页出现滚动条的时候,许多网站会在右下角出现一个图标,点击可以回到页面顶部 本文就记录下js实现代码: 1.在html页面body添加dom元素 <img src="toTop ...

  4. 《招聘一个靠谱的iOS》面试题参考答案(下)

    相关文章: <招聘一个靠谱的iOS>面试题参考答案(上) 说明:面试题来源是微博@我就叫Sunny怎么了的这篇博文:<招聘一个靠谱的 iOS>,其中共55题,除第一题为纠错题外 ...

  5. UITextField输入长度限制

    [_yourTextField addTarget:self action:@selector(eventEditingChange:) forControlEvents:UIControlEvent ...

  6. 基础总结篇之九:Intent应用详解

    看似尋常最奇崛,成如容易卻艱辛.北宋.王安石 看似普通的事情其实最不同寻常,并不是简简单单就可以做好的:成功看起来似乎很容易,而成功的过程却充满着艰辛. 对于我们认为很普通的事情,不屑一顾,就永远不会 ...

  7. zXing使用小结

    在android上二维码.条形码扫描,google官方为我们提供了zXing,几乎android涉及到扫描的都是用这个开源项目实现的,也有在android上使用zBar的,和其他用过的交流得知zBar ...

  8. NPOI之使用EXCEL模板创建报表

    因为项目中要用到服务器端创建EXCEL模板 无法直接调用EXCEL 查了下发现NPOI很方便很简单就实现了 其中走了点弯路 第一次弄的时候发现输出的值是文本不是数字型无法直接计算公式 然后又发现打开报 ...

  9. ie6+7+8等对background-color:rgba(),background-img渐变的兼容

    一,ie8兼容rgba()的解决办法 今天遇到了一个问题,要在一个页面中设置一个半透明的白色div.这个貌似不是难题,只需要给这个div设置如下的属性即可: background: rgba(255, ...

  10. Ninject框架的介绍

    Ninject是C#语言的一款依赖性的注入器框架,我认为之所以会出现这个框架是因为类与类由于继承或者接口与类继承而出现的,首先这 个最典型存在是因为接口,首先我们来看看这个用了框架和没有用框架的区别吧 ...