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)算法是一种静态路网中求解最短路径最有效的直接搜索方法也是一种启发性的算法,也是解决许多搜索问题的有效算法.算法中的距离估 ...
随机推荐
- kafka 性能优化与常见问题优化处理方案
本文为博主原创,未经允许不得转载: 1. JVM参数优化设置 kafka是scala语言开发,运行在JVM上,需要对JVM参数合理设置,修改bin/kafka-start-server.sh中的jv ...
- 网络要素服务(WFS)详解
目录 1. 概述 2. GetCapabilities 3. DescribeFeatureType 4. GetFeature 4.1 Get访问方式 4.2 Post访问方式 5. Transac ...
- Go-基本类型-int-float-bool-byte-rune
- MySQL高可用九种方案
有的时候博客内容会有变动,首发博客是最新的,其他博客地址可能会未同步,认准https://blog.zysicyj.top 首发博客地址 参考视频 MMM 方案(单主) MySQL 高可用方案之 MM ...
- [转帖]解Bug之路-记一次中间件导致的慢SQL排查过程
https://zhuanlan.zhihu.com/p/242265937 解Bug之路-记一次中间件导致的慢SQL排查过程 前言 最近发现线上出现一个奇葩的问题,这问题让笔者定位了好长时间,期间排 ...
- [转帖]编译安装goofys挂载Scaleway免费75G对象存储
日常•2022年5月29日 goofys编译 goofys是一个开源的使用Go编写的s3存储桶挂载工具,主打高性能.由于使用Go编写,没有用到什么特别的依赖,自己编译也很容易.截止2022.5.2 ...
- 【转帖】linux 调优篇 :硬件调优(BIOS配置)* 壹
一. 设置内存刷新频率为Auto二. 开启NUMA三. 设置Stream Write Mode四. 开启CPU预取配置五. 开启SRIOV六. 开启SMMU 通过在BIOS中设置一些高级选项,可以有效 ...
- [转帖]关于 AREX
https://arextest.github.io/website/zh-Hans/docs/intro/ AREX 介绍 背景 对于一个初上线的简单服务,只需通过常规的自动化测试加上人工即可解 ...
- [转帖]Linux-文本处理三剑客awk详解+企业真实案例(变量、正则、条件判断、循环、数组、分析日志)
https://developer.aliyun.com/article/885607?spm=a2c6h.24874632.expert-profile.313.7c46cfe9h5DxWK 简介: ...
- 是否开启raid卡缓存的影响
开启raid卡缓存 Write back 对IO性能的影响 背景 公司买了一台服务器. 想进行一下升级 但是因为管理员担心数据丢失, 使用了write through + (raid6 + hotsp ...