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)算法是一种静态路网中求解最短路径最有效的直接搜索方法也是一种启发性的算法,也是解决许多搜索问题的有效算法.算法中的距离估 ...
随机推荐
- linux 通过docker安装 elasticsearch-head
本文为博主原创,未经允许不得转载: 1. 使用docker安装 elasticsearch-head #拉取镜像 docker pull mobz/elasticsearch-head:5 #创建容器 ...
- feign接口自动生成工具
最近发现开发spring cloud时,编写feign接口是一件痛苦的事,不仅要编写feign接口,还有fallback.请求参数和返回值等,大量重复工作,很浪费时间. 于是便想到可以编写工具自动生成 ...
- 【ArgParse】一个开源的入参解析库
项目地址:argtable3 本地验证: 编译构建 新增验证 // examples/skull.c #include "argtable3.h" int main(int arg ...
- [转帖]SkyWalking告警使用
SkyWalking告警 SkyWalking提供了强大的监控告警功能,在监控到应用出现问题的时候,会调用webhook或者gRPC hook或者Wechat DingDing等工具报告警告信息 而且 ...
- [转帖]AMD处理器ZEN一代之国产化海光
https://huataihuang.gitbook.io/cloud-atlas-draft/os/linux/kernel/cpu/amd_hygon 2020年国产化处理器受到了广泛的关注 ...
- [转帖]Linux shell 单引号和双引号
https://www.cnblogs.com/airoot/p/15324883.html 在编写shell脚本的时候经常会用到引号,有些时候却老是忘记单引号和双引号之间的区别,所以就整理一下供以后 ...
- 【转贴】libcrypto.so.10丢失导致sshd无法运行解决方案
http://www.cnblogs.com/billy98/p/4226023.html libcrypto.so.10丢失导致sshd无法运行解决方案 服务器由于掉电开启系统后无法远程ssh,重启 ...
- 基于Spring Cache实现Caffeine、jimDB多级缓存实战
作者: 京东零售 王震 背景 在早期参与涅槃氛围标签中台项目中,前台要求接口性能999要求50ms以下,通过设计Caffeine.ehcache堆外缓存.jimDB三级缓存,利用内存.堆外.jimDB ...
- Docker容器基础入门认知-Namespce
在使用 docker 之前我一般都认为容器的技术应该和虚拟机应该差不多,和虚拟机的技术类似,但是事实上容器和虚拟机根本不是一回事. 虚拟机是将虚拟硬件.内核(即操作系统)以及用户空间打包在新虚拟机当中 ...
- React中兄弟组件通信和组件跨级传递Context的使用
React兄弟组件之间的通信 Child2组件需要去更改Child1组件中的数据. 因为Child1和Child2是兄弟组件 所以数据和事件都放在最进的父级组件中去 兄弟组件通信的简单使用 impor ...