Egiht(八种方法)

简介: 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.

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.

SampleInput

2  3  4  1  5  x  7  6  8

SampleOutput

ullddrurdllurdruldr
就是给你一个序列,然后要你通过四种移动方式,回到最初的序列,输出答案,题目有SPJ,无需输出最优方法。
这道题可谓是搜索中的精髓了,最开始我就是一个简单的双向搜索,结果TLE了,看了网上的博客加了剪枝之后过了。
但是后来和队友聊这题,发现这道题的解题思路不止一种,队友说有八种

然后找到一篇博客,里面一共讲了八种解法,应该是很全面的。
博客链接:https://www.cnblogs.com/zufezzt/p/5659276.html
至于我的代码就没什么好讲的了,确实用双向BFS加剪枝可以AC这道题,但这道题的精髓远远不在于此,也见识到了自己的学识浅薄。我也道不出个所以然,所以这道题我就不详细讲了。

还是那句话以梦为马,不负韶华。
目录
相关文章
|
10月前
|
SQL 数据库
SqlCommand.ExecuteNonQuery 方法
SqlCommand的一个类,用于包含update、insert、delete、select的Transact-sql 语句中来修改数据库中的数据,并返回结果。
|
存储 JavaScript 前端开发
JavaScript继承的几种方法
JavaScript继承的几种方法
112 0
JavaScript继承的几种方法
|
C语言 C++
求公因数的方法(C/C++)
求公因数的方法(C/C++)
261 0
求公因数的方法(C/C++)
|
Java 开发者
|
机器学习/深度学习
基于2D标靶的摄像机标定方法
基于2D标靶的摄像机标定方法
想求问一下,瑜伽的练习方法
沐鸣如果勉强坐得太久,很容易因为身体酸麻涨痛而对瑜伽坐姿练习发生退却之心,所以最初练习以“短时多次”为宜,慢慢就能享受到打坐的乐趣了练习瑜伽坐姿时,要坚持腰背挺直,下颗内收,使头部、颈部和脊椎保持在一条直线上。此外,练习全莲花坐时,注意膝盖不要上浮。>
|
存储 算法 搜索推荐
常用的外部排序方法
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_34173549/article/details/81158566 定义问题       外部排序指的是大文件的排序,即待排序的记录存储在外存储器上,待排序的文件无法一次装入内存,需要在内存和外部存储器之间进行多次数据交换,以达到排序整个文件的目的。
1755 0
|
数据库 Go
统计数据库大小的方法
原文:统计数据库大小的方法     一台服务器上所有数据库(也可能是部分数据库)的大小是我们经常需要了解的,它不仅能让我们知道目前数据库使用磁盘的比例, 而且定期搜集这些信息,还能了解数据库一段时间的数据增量,更为常用的是在做数据迁移和升级时,方便规划新服务器磁盘容量。
1248 0

热门文章

最新文章