NC51032 八数码
题目
题目描述
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 八数码的更多相关文章
- A*算法 -- 八数码问题和传教士过河问题的代码实现
前段时间人工智能的课介绍到A*算法,于是便去了解了一下,然后试着用这个算法去解决经典的八数码问题,一开始写用了挺久时间的,后来试着把算法的框架抽离出来,编写成一个通用的算法模板,这样子如果以后需要用到 ...
- 八数码问题:C++广度搜索实现
毕竟新手上路23333,有谬误还请指正. 课程设计遇到八数码问题(这也是一坨),也查过一些资料并不喜欢用类函数写感觉这样规模小些的问题没有必要,一开始用深度搜索却发现深搜会陷入无底洞,如果设定了深度限 ...
- ACM/ICPC 之 BFS-广搜进阶-八数码(经典)(POJ1077+HDU1043)
八数码问题也称为九宫问题.(本想查查历史,结果发现居然没有词条= =,所谓的历史也就不了了之了) 在3×3的棋盘,摆有八个棋子,每个棋子上标有1至8的某一数字,不同棋子上标的数字不相同.棋盘上还有一个 ...
- BFS(八数码) POJ 1077 || HDOJ 1043 Eight
题目传送门1 2 题意:从无序到有序移动的方案,即最后成1 2 3 4 5 6 7 8 0 分析:八数码经典问题.POJ是一次,HDOJ是多次.因为康托展开还不会,也写不了什么,HDOJ需要从最后的状 ...
- 双向广搜+hash+康托展开 codevs 1225 八数码难题
codevs 1225 八数码难题 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description Yours和zero在研究A*启 ...
- UVALive 6665 Dragonâs Cruller --BFS,类八数码问题
题意大概就是八数码问题,只不过把空格的移动方式改变了:空格能够向前或向后移动一格或三格(循环的). 分析:其实跟八数码问题差不多,用康托展开记录状态,bfs即可. 代码: #include <i ...
- P1379 八数码问题
aoapc上的八数码问题,在luogu上也有类似的题,p1379,经典题目,lrj给出了一个算法,同时给出了三种判重的方法.本来想用std::queue改写一下,但是出了各种问题,只好抄代码ac掉这道 ...
- [cdoj1380] Xiper的奇妙历险(3) (八数码问题 bfs + 预处理)
快要NOIP 2016 了,现在已经停课集训了.计划用10天来复习以前学习过的所有内容.首先就是搜索. 八数码是一道很经典的搜索题,普通的bfs就可求出.为了优化效率,我曾经用过康托展开来优化空间,甚 ...
- hdu 1043 Eight 经典八数码问题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1043 The 15-puzzle has been around for over 100 years ...
- A*算法解决八数码问题 Java语言实现
0X00 定义 首先要明确一下什么是A*算法和八数码问题? A*(A-Star)算法是一种静态路网中求解最短路径最有效的直接搜索方法也是一种启发性的算法,也是解决许多搜索问题的有效算法.算法中的距离估 ...
随机推荐
- MyBatis03——ResultMap和分页相关
ResultMap和分页相关 当属性名和字段名不一致的时候 解决方法 1.数据库中创建user表 字段 id.name.pwd 2.Java中的实体类 @Data public class User ...
- [转帖]会长期锁表吗?Oracle add column default 在各版本的优化
最近大家讨论到各类数据库在新增字段带默认值时是否会锁表的问题,发现Oracle在不同的版本其实是有对应优化的,更新了下之前的知识点. 主要考虑以下三种情形,例如: 不加default:alter ta ...
- [转帖]MySQL 8.0 以后的版本策略变化
https://www.modb.pro/db/1717815842220630016 产品版本变更 从2023年7月18日开始,MySQL官网出现了一个新的版本 MySQL 8.1.0,直接改变 ...
- [转帖]【KingbaseES】sys_dump逻辑备份工具详解
KingbaseES逻辑备份还原工具提供了数据库对象一级的联机备份还原功能,备份对象包括: 数据库 模式 表 视图 约束 权限 触发器 函数 序列 逻辑备份的输出格式包括: 二进制 SQL脚本 此外, ...
- [转帖]MOUNTING AN S3 BUCKET ON WINDOWS AND LINUX
https://blog.spikeseed.cloud/mount-s3-as-a-disk/#mounting-an-s3-bucket-on-windows-server-2016 Wouldn ...
- [转帖]jmeter之使用csv文件传递参数-05篇
csv文件格式,第一列为手机号,第二列为密码 1.右键添加一个配置元件---csv数据文件设置 2.设置csv数据提取路径 3.引用数据 4.设置线程循环运行两次,然后发送请求 看到结果就执行了两次, ...
- 【转帖】在ECS上配置skywalking-nginx-lua
https://help.aliyun.com/document_detail/197660.html 此处以在CentOS 7.0上的操作为例. 配置Lua运行环境. 安装工具库. yum in ...
- 最小化安装的CentOS7 上面安装Oracle12C的简单过程
首先声明自己对静默安装不熟,也害怕初问题,所以不使用静默安装的方式. 因为是最小化安装,所以必须安装GUI界面才可以,以下是过程(早上回忆的,全文字,无截图) 1. 安装GUI界面 yum group ...
- 查看java所有的线程信息
最近一直有一个困惑, 不知道如何查看所有的java的线程信息. 今天看blog时发现了一个简单方法 ps -Tp $pid 就可以了 也可以使用 ps- Lfp $pid的方式 这里简单写一下统计方法 ...
- vite构建打包产物
构建打包产物 build: { assetsInlineLimit: 10240, //静态资源(不会包含js,css)不超过10kb 转化为 base64 [不好含css js] outDir: & ...