题目链接

题目

题目描述

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.

输入描述

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

输出描述

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.

示例1

输入

 2  3  4  1  5  x  7  6  8

输出

ullddrurdllurdruldr

题解

知识点:BFS。

很显然用bfs搜索,但状态保存是个问题,可以用c++自带的map进行状态保存,也可以用康托展开对局面字符串转化为整型保存(我居然还不会qwq)。

要注意的是,数据再复杂点可以卡普通的bfs,这时候需要优化搜索,可以用双向bfs或者A*,可以节省大量时间。这里我用了双向bfs(因为不会A*2333)。

最后注意无解情况可能超时,建议计数跳出(也有逆序对的方法)。

时间复杂度 \(O(?)\)

空间复杂度 \(O(1)\)

代码

#include <bits/stdc++.h>

using namespace std;

struct node {
string s;
int x, y;
};
const int dir[4][2] = { {-1,0},{0,-1},{0,1},{1,0} };
char dirs[4] = { 'u','l','r','d' }; string bfs(node init, node ans) { map<string, string> vis1, vis2;
vis1[init.s] = "";
vis2[ans.s] = ""; queue<node> q1, q2;
q1.push(init);
q2.push(ans); int cnt = 0;
while (!q1.empty() && !q2.empty()) {
if (cnt >= 10000) break;
cnt++;
node a = q1.front();
node b = q2.front();
q1.pop();
q2.pop();
if (vis2.count(a.s)) return vis1[a.s] + vis2[a.s];
else if (vis1.count(b.s)) return vis1[b.s] + vis2[b.s];
for (int i = 0;i < 4;i++) {
node aa;
aa.x = a.x + dir[i][0];
aa.y = a.y + dir[i][1];
if (aa.x >= 0 && aa.x < 3 && aa.y >= 0 && aa.y < 3) {
aa.s = a.s;
swap(aa.s[a.x * 3 + a.y], aa.s[aa.x * 3 + aa.y]);
if (!vis1.count(aa.s)) vis1[aa.s] = vis1[a.s] + dirs[i], q1.push(aa);
} node bb;
bb.x = b.x + dir[i][0];
bb.y = b.y + dir[i][1];
if (bb.x >= 0 && bb.x < 3 && bb.y >= 0 && bb.y < 3) {
bb.s = b.s;
swap(bb.s[b.x * 3 + b.y], bb.s[bb.x * 3 + bb.y]);
if (!vis2.count(bb.s)) vis2[bb.s] = dirs[3 - i] + vis2[b.s], q2.push(bb);
}
}
}
return "unsolvable";
} int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); node init, ans;
for (int i = 0;i < 9;i++) {
char tmp;
cin >> tmp;
init.s += tmp;
if (tmp == 'x') {
init.x = i / 3;
init.y = i % 3;
}
}
ans.s = "12345678x";
ans.x = 2;
ans.y = 2;
cout << bfs(init, ans) << '\n';
return 0;
}

NC51032 八数码的更多相关文章

  1. A*算法 -- 八数码问题和传教士过河问题的代码实现

    前段时间人工智能的课介绍到A*算法,于是便去了解了一下,然后试着用这个算法去解决经典的八数码问题,一开始写用了挺久时间的,后来试着把算法的框架抽离出来,编写成一个通用的算法模板,这样子如果以后需要用到 ...

  2. 八数码问题:C++广度搜索实现

    毕竟新手上路23333,有谬误还请指正. 课程设计遇到八数码问题(这也是一坨),也查过一些资料并不喜欢用类函数写感觉这样规模小些的问题没有必要,一开始用深度搜索却发现深搜会陷入无底洞,如果设定了深度限 ...

  3. ACM/ICPC 之 BFS-广搜进阶-八数码(经典)(POJ1077+HDU1043)

    八数码问题也称为九宫问题.(本想查查历史,结果发现居然没有词条= =,所谓的历史也就不了了之了) 在3×3的棋盘,摆有八个棋子,每个棋子上标有1至8的某一数字,不同棋子上标的数字不相同.棋盘上还有一个 ...

  4. BFS(八数码) POJ 1077 || HDOJ 1043 Eight

    题目传送门1 2 题意:从无序到有序移动的方案,即最后成1 2 3 4 5 6 7 8 0 分析:八数码经典问题.POJ是一次,HDOJ是多次.因为康托展开还不会,也写不了什么,HDOJ需要从最后的状 ...

  5. 双向广搜+hash+康托展开 codevs 1225 八数码难题

    codevs 1225 八数码难题  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond   题目描述 Description Yours和zero在研究A*启 ...

  6. UVALive 6665 Dragon’s Cruller --BFS,类八数码问题

    题意大概就是八数码问题,只不过把空格的移动方式改变了:空格能够向前或向后移动一格或三格(循环的). 分析:其实跟八数码问题差不多,用康托展开记录状态,bfs即可. 代码: #include <i ...

  7. P1379 八数码问题

    aoapc上的八数码问题,在luogu上也有类似的题,p1379,经典题目,lrj给出了一个算法,同时给出了三种判重的方法.本来想用std::queue改写一下,但是出了各种问题,只好抄代码ac掉这道 ...

  8. [cdoj1380] Xiper的奇妙历险(3) (八数码问题 bfs + 预处理)

    快要NOIP 2016 了,现在已经停课集训了.计划用10天来复习以前学习过的所有内容.首先就是搜索. 八数码是一道很经典的搜索题,普通的bfs就可求出.为了优化效率,我曾经用过康托展开来优化空间,甚 ...

  9. hdu 1043 Eight 经典八数码问题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1043 The 15-puzzle has been around for over 100 years ...

  10. A*算法解决八数码问题 Java语言实现

    0X00 定义 首先要明确一下什么是A*算法和八数码问题? A*(A-Star)算法是一种静态路网中求解最短路径最有效的直接搜索方法也是一种启发性的算法,也是解决许多搜索问题的有效算法.算法中的距离估 ...

随机推荐

  1. 聊一聊数字孪生与3D可视化

    前言 在当代科技发展的背景下,数字孪生和3D可视化技术逐渐成为各行业的关键工具和解决方案.数字孪生是一种将实物事物与数字模型相结合的概念,通过将物理世界和数字世界实时连接,创造出一个对实体进行虚拟建模 ...

  2. Meta AI新发布的超大规模语言模型-OPT-175B

    ​ Meta AI在2022年5月3日新发布的OPT-175B模型,该模型是现阶段第一个模型参数超过千亿级别的开放模型,其次该模型与GPT-3相比,更加开放及便于访问. 具体开放性表现在如下几个方面: ...

  3. LaTeX 公式识别问题

    问题 想要方便的图片公式识别工具来写Latex(论文)/markdown(笔记)文件 工具推荐 1.mathpix 识别成功率最高(无论是多行,表格表现都非常良好),最好用的工具,但是收费高且付费麻烦 ...

  4. 【Git】用法小记

    解决windows环境下的CRLF与unix环境下的LF问题,windows提交时CRLF=>LF,签出时LF=>CRLF,unix环境保留 git config --global cor ...

  5. css - 使用 figure 和 figcaption 快速实现 图片加文字的垂直方向的布局 ( 不支持ie9以下版本 )

    一,属性介绍 1. 浏览器支持 注释:Internet Explorer 8 以及更早的版本不支持 <figure> 标签.Internet Explorer 9, Firefox, Op ...

  6. java - classpath 的配置

    classpath C:\Program Files\Java\jdk\jre\lib\rt.jar

  7. QT5.9移植到海思HI3520设备上运行

    前言: 在海思HI3520DV300上调试QT5.9.0有一小段时间了,这里将遇到的比较典型的问题做一个记录,以备后续查询,也可给同行一个参考.本人只使用过QT5.9.0这一个版本,如有描述错误欢迎指 ...

  8. 在TypeScript项目中搭配Axios封装后端接口调用

    前言 本来是想发 next.js 开发笔记的,结果发现里面涉及了太多东西,还是拆分出来发吧~ 本文记录一下在 TypeScript 项目里封装 axios 的过程,之前在开发 StarBlog-Adm ...

  9. [转帖]nginx源码层面探究request_time、upstream_response_time、upstream_connect_time与upstream_header_time指标具体含义与区别

    https://www.cnblogs.com/AcAc-t/p/nginx_request_time_upstream_respone_time_analysis.html 背景概述 最近计划着重分 ...

  10. [转帖]TPC-C 、TPC-H和TPC-DS区别

    https://zhuanlan.zhihu.com/p/339886289 针对数据库不同的使用场景TPC组织发布了多项测试标准. TPC-C: TPC Benchmark C于1992年7月获得批 ...