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这道题,但这道题的精髓远远不在于此,也见识到了自己的学识浅薄。我也道不出个所以然,所以这道题我就不详细讲了。

还是那句话以梦为马,不负韶华。
目录
相关文章
|
存储 机器学习/深度学习 人工智能
知识图谱入门一:知识图谱介绍,Neo4j下载、安装基本使用
知识图谱入门一:知识图谱介绍,Neo4j下载、安装基本使用
1416 1
知识图谱入门一:知识图谱介绍,Neo4j下载、安装基本使用
|
存储 Kubernetes 应用服务中间件
【K8S系列】深入解析无状态服务
【K8S系列】深入解析无状态服务
892 2
|
11月前
|
Web App开发 安全 Linux
【独家揭秘2025】VMware Workstation Pro虚拟机:免费安装教程大放送,一键解锁操作系统模拟神器!
VMware Workstation Pro 是由威睿(VMware)公司开发的一款功能强大的桌面虚拟化软件,允许用户在同一台物理计算机上同时运行多个操作系统,如Windows、..
1204 2
【独家揭秘2025】VMware Workstation Pro虚拟机:免费安装教程大放送,一键解锁操作系统模拟神器!
|
10月前
|
数据采集 人工智能 搜索推荐
DeepSeek告诉你眼中不会被AI替代的职业,打工人可以这样华丽转型
在AI技术飞速发展的今天,职场正经历前所未有的变革。麦肯锡预测,到2030年全球约30%的岗位可能被AI替代,但AI并非终结者,而是转型推手。DeepSeek揭示了教育、医疗、手工艺和创意等难以被AI替代的职业领域,并提供了从“被替代者”到“AI指挥官”的转型指南。生成式人工智能(GAI)认证成为提升自我、适应AI时代的关键工具,助力职场人掌握核心技能,实现华丽转型。未来赢家将是善用AI放大自身优势的人。
|
SQL 存储 Oracle
南大通用GBase 8s数据库游标变量解析:提升数据库操作效率
南大通用GBase 8s 数据库游标变量解析:提升数据库操作效率
|
NoSQL JavaScript 算法
Higress 全新 Wasm 运行时,性能大幅提升
本文介绍 Higress 将 Wasm 插件的运行时从 V8 切换到 WebAssembly Micro Runtime (WAMR) 的最新进展。
55925 97
|
JavaScript 小程序 测试技术
iphone13到底香不香,真的这么难抢?
本文介绍了iPhone 12/13的自动化测试脚本,包括Node.js脚本实现有货通知和Python+Selenium自动化抢购脚本。Node.js脚本可在检测到有货时发送邮件通知;Python+Selenium脚本则能模拟用户操作,完成从选择型号、颜色到支付的全过程。
276 7
iphone13到底香不香,真的这么难抢?
|
安全 Java 程序员
【多线程-从零开始-肆】线程安全、加锁和死锁
【多线程-从零开始-肆】线程安全、加锁和死锁
265 0
在 Django 上实现定时任务的指南
在 Django 中实现定时任务有多种方法,包括使用 Celery 和 Celery Beat、django-background-tasks、以及 APScheduler。根据您的需求和应用场景,可以选择最适合的方案。每种方法都有其优缺点,选择时应考虑任务复杂性、系统资源、以及维护成本。通过这些工具,您可以有效地管理和调度后台任务,提高应用程序的自动化水平和运行效率。
|
安全 Ubuntu Linux
在Docker中,镜像内没有curl,kill,ipconfig等指令如何添加?
在Docker中,镜像内没有curl,kill,ipconfig等指令如何添加?