题目链接:http://poj.org/problem?id=1077

Eight
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 33267   Accepted: 14404   Special Judge

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 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

Source

题解:

1.对于棋盘,所有的情况为:9! <4e5<1e6,所以使用枚举的方法是不会超时的,前提是数据较弱(如POJ1077),或者一次性枚举预处理掉所有情况(HDU1043)。

2.对于判重:使用STL的set肯定会超时的。一开始想利用十进制数,每一位保存一个格子,结果9个格子就是9位数,数组根本开不了那么大,所以此种尝试失败。后来看到康拓,不太理解为什么能用(不会有冲突吗?),但还是先用着,有时间再研究一下。如果下次遇到类似123456789这九个数需要进行哈希的,就使用康拓试试。

3.推荐阅读:八数码的八境界   ——  来自liugoodness,特此鸣谢。

写法:

1.正向BFS:http://www.cnblogs.com/DOLFAMINGO/p/7538587.html

2.反向BFS:http://www.cnblogs.com/DOLFAMINGO/p/7538586.html

3.双向BFS:http://www.cnblogs.com/DOLFAMINGO/p/7538585.html

4.  A*算法  :http://www.cnblogs.com/DOLFAMINGO/p/7538584.html

5.IDA*算法:http://www.cnblogs.com/DOLFAMINGO/p/7538583.html

POJ1077 Eight —— 经典的搜索问题的更多相关文章

  1. python-反射、新式类与经典类搜索的优先级

    preface include: getattr setattr delattr hasattr class webserver(object): def __init__(self,num): se ...

  2. [LeetCode]N-Queens 八皇后问题扩展(经典深层搜索)

    The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens ...

  3. HDU 1312 Red and Black(最简单也是最经典的搜索)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1312 Red and Black Time Limit: 2000/1000 MS (Java/Oth ...

  4. PHP程序设计经典300例

    不知道怎么转载,原文源自:http://bbs.php100.com/u-htm-uid-330857.html 来自:php100钟泽锋 第一例<?php $s_html="< ...

  5. python中的__new__与__init__,新式类和经典类(2.x)

    在python2.x中,从object继承得来的类称为新式类(如class A(object))不从object继承得来的类称为经典类(如class A()) 新式类跟经典类的差别主要是以下几点: 1 ...

  6. 搜索+剪枝——POJ 1011 Sticks

    搜索+剪枝--POJ 1011 Sticks 博客分类: 算法 非常经典的搜索题目,第一次做还是暑假集训的时候,前天又把它翻了出来 本来是想找点手感的,不想在原先思路的基础上,竟把它做出来了而且还是0 ...

  7. python2中的__new__与__init__,新式类和经典类-乾颐堂

    在python2.x中,从object继承得来的类称为新式类(如class A(object))不从object继承得来的类称为经典类(如class A()) 新式类跟经典类的差别主要是以下几点: 1 ...

  8. HDU 1175 连连看(超级经典的bfs之一)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1175 连连看 Time Limit: 20000/10000 MS (Java/Others)     ...

  9. HDU 1180 诡异的楼梯(超级经典的bfs之一,需多回顾)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1180 诡异的楼梯 Time Limit: 2000/1000 MS (Java/Others)     ...

随机推荐

  1. Spoj-ANTP Mr. Ant & His Problem

    Mr. Ant has 3 boxes and the infinite number of marbles. Now he wants to know the number of ways he c ...

  2. Python脚本实现单据体首行过滤

    编写的Python脚本 可以看到,实际代码只有3句,即实现单据体首行过滤代码(其实最最主要的是无需写组件动态即时注册),并有注册到[采购订单]"表单构建插件"上.界面运行时,实际效 ...

  3. HDU 5033 Building(北京网络赛B题) 单调栈 找规律

    做了三天,,,终于a了... 11724203 2014-09-25 09:37:44 Accepted 5033 781MS 7400K 4751 B G++ czy Building Time L ...

  4. linux 内核源码arch/ 目录的前世今生

    历史的痕迹:在最新的linux-2.6.31/arch/arm/文件夹下,仍然保留Linux最初向ARM处理器移植的痕迹,最初的移植由黑客完成,在老的移植的代码文件的头部保留着黑客的名字:最初的ARM ...

  5. golang并发编程goroutine+channel(一)

    go语言的设计初衷除了在不影响程序性能的情况下减少复杂度,另一个目的是在当今互联网大量运算下,如何让程序的并发性能和代码可读性达到极致.go语言的并发关键词 "go" go dos ...

  6. 在 Edison 上自动启动 Arduino Sketch

    前言 原创文章,转载引用务必注明链接,水平有限,如有疏漏,欢迎指正. 本文使用Markdown写成,为获得更好的阅读体验和正常的链接.图片显示,请访问我的博客原文: http://www.cnblog ...

  7. Android App 隐藏标题栏+状态栏+导航栏

    1. 隐藏当前Activity标题栏 在当前Activity中调用:this.requestWindowFeature(Window.FEATURE_NO_TITLE); 2. 隐藏当前Activit ...

  8. Android——坐标系及转化

    一.坐标系 Android应用层坐标系原点在左上角,坐标范围(0,0)——(width,height). Android底层坐标系原点在屏幕中央,坐标范围(-1000,,1000)——(1000,10 ...

  9. mysql (primary key)_(unique key)_(index) difference

    MYSQL  index  MYSQL索引用来快速地寻找那些具有特定值的记录,所有MySQL索引都以B-树的形式保存.如果没有索引,执行查询时MySQL必须从第一个记录开始扫描整个表的所有记录,直至找 ...

  10. 【转载】.NET 框架浅析

    经过了对C#较长时间的学习以及实践,不断地感受着.NET 框架--这个最本质,然而却似乎让人捉摸不透的概念.特别是经过拜读Anytao博主的<你必须知道的.NET>文章,使我对其概念关系得 ...