八数码问题(8-Puzzle Problem)

P1379 八数码难题 - 洛谷

题目概述:在 \(3 \times 3\) 的棋盘上摆放着 \(8\) 个棋子,棋子的编号分别为 \(1\) 到 \(8\),空格则用 \(0\) 表示。与空格直接相连的棋子可以移至空格中,这样原来棋子的位置就成为空格。现给出一种初始布局,求到达目标布局的最少步数。为简单起见,目标布局总是如下:

123
804
765

本题是一道经典的搜索题,下面将介绍几种常见的搜索算法。以下所有代码均需要 C++11 标准。

朴素 BFS

通过对题目的简单分析,很容易写出朴素的 BFS 代码。进行访问标记时,可以利用哈希的思想,将矩阵转化为整数,再用 std::unordered_set 存储。由于本题的数据范围较小,朴素的 BFS 算法也能通过本题测试,但是效率较低。具体代码如下:

#include <bits/stdc++.h>

using namespace std;
const int tar_x = 2, tar_y = 2, target = 123804765;
const int dx[] = { 1, -1, 0, 0 };
const int dy[] = { 0, 0, 1, -1 }; struct Status {
int maze[5][5]; // matrix
int x, y; // coordinate of blank space
int t; // step number explicit Status(int num) {
memset(maze, 0, sizeof(maze));
t = 0;
for (int i = 3; i >= 1; --i) {
for (int j = 3; j >= 1; --j) {
maze[i][j] = num % 10;
num /= 10;
if (maze[i][j] == 0)
x = i, y = j;
}
}
} int to_int() const {
int ans = 0;
for (int i = 1; i <= 3; ++i)
for (int j = 1; j <= 3; ++j)
ans = ans * 10 + maze[i][j]; // hash
return ans;
}
}; int bfs(int num) {
queue<Status> q;
unordered_set<int> vis;
q.emplace(num);
vis.insert(num);
while (!q.empty()) {
Status now = q.front();
q.pop();
if (now.x == tar_x && now.y == tar_y && now.to_int() == target)
return now.t; // exit
++now.t;
int x = now.x, y = now.y;
for (int i = 0; i < 4; ++i) {
int tx = x + dx[i], ty = y + dy[i];
if (tx < 1 || tx > 3 || ty < 1 || ty > 3)
continue;
swap(now.maze[x][y], now.maze[tx][ty]);
now.x = tx;
now.y = ty;
if (!vis.count(now.to_int())) {
q.push(now); // expand
vis.insert(now.to_int());
}
now.x = x;
now.y = y;
swap(now.maze[x][y], now.maze[tx][ty]); // backtrack
}
}
return -1; // unused value
} int main() {
int num;
cin >> num;
cout << bfs(num) << endl;
return 0;
}

双向 BFS

对于本题这类已知初始状态和目标状态的题目,可以考虑双向 BFS。在搜索开始前,同时将初始状态和目标状态放进 BFS 队列中。搜索过程中,标记每个状态被访问时的搜索方向以及从对应起点出发的步数。当一种状态被两个方向同时搜到,也就是两个方向相遇时,这两个方向的步数之和就是所求答案。BFS 的性质保证了这一答案一定是最小值。这样的算法称为 Meet in the Middle,通过将实际拓展的层数减半,大大提高了搜索效率,避免了许多不必要的状态拓展。具体代码如下:

#include <bits/stdc++.h>

using namespace std;
const int target = 123804765;
const int dx[] = { 1, -1, 0, 0 };
const int dy[] = { 0, 0, 1, -1 }; struct Status {
int maze[5][5]; // matrix
int x, y; // coordinate of blank space
bool d; // bfs direction (true: forward, false: back)
int t; // step number explicit Status(int num) {
memset(maze, 0, sizeof(maze));
t = 0;
if (num == target)
d = false;
else
d = true;
for (int i = 3; i >= 1; --i) {
for (int j = 3; j >= 1; --j) {
maze[i][j] = num % 10;
num /= 10;
if (maze[i][j] == 0)
x = i, y = j;
}
}
} int to_int() const {
int ans = 0;
for (int i = 1; i <= 3; ++i)
for (int j = 1; j <= 3; ++j)
ans = ans * 10 + maze[i][j]; // hash
return ans;
}
}; int bfs(int num) {
queue<Status> q;
unordered_map<int, pair<int, bool>> vis;
q.emplace(target); // target state
vis[target] = make_pair(0, false);
q.emplace(num); // starting state
vis[num] = make_pair(0, true);
while (!q.empty()) {
Status now = q.front();
q.pop();
if (vis.count(now.to_int()) && vis[now.to_int()].second != now.d)
return now.t + vis[now.to_int()].first; // meet in the middle
++now.t;
int x = now.x, y = now.y;
for (int i = 0; i < 4; ++i) {
int tx = x + dx[i], ty = y + dy[i];
if (tx < 1 || tx > 3 || ty < 1 || ty > 3)
continue;
swap(now.maze[x][y], now.maze[tx][ty]);
now.x = tx;
now.y = ty;
if (!vis.count(now.to_int()) || vis[now.to_int()].second != now.d) {
q.push(now); // expand
vis[now.to_int()] = make_pair(now.t, now.d);
}
now.x = x;
now.y = y;
swap(now.maze[now.x][now.y], now.maze[tx][ty]); // backtrack
}
}
return -1; // unused value
} int main() {
int num;
cin >> num;
cout << bfs(num) << endl;
return 0;
}

A*

A* 算法是一种启发式搜索,即利用估值函数进行剪枝,以避免盲目搜索中许多不必要的状态拓展。A* 算法以 BFS 为基础,用优先队列代替 BFS 队列,以估值函数为优先级。A* 算法中,每个状态的估值函数由两部分组成,即 \(f(x)=g(x)+h(x)\),其中 \(g(x)\) 是已经走过的步数,\(h(x)\) 是预估到达终点至少还要走的步数,两者之和 \(f(x)\) 即这一状态的估值函数。因此,为确保算法正确,\(h(x)\) 的值一定不大于实际距离终点的步数,即 \(f(x)\) 的值一定不大于实际总步数。本题中,可以使用每个棋子到目标位置的曼哈顿距离作为其 \(h(x)\)。容易证明,该函数满足上述条件。具体代码如下:

#include <bits/stdc++.h>

using namespace std;
const int dx[] = { 1, -1, 0, 0 };
const int dy[] = { 0, 0, 1, -1 };
const int pos_x[] = { 2, 1, 1, 1, 2, 3, 3, 3, 2 };
const int pos_y[] = { 2, 1, 2, 3, 3, 3, 2, 1, 1 }; struct Status {
int maze[5][5]; // matrix
int x, y; // coordinate of blank space
int t; // step number explicit Status(int num) {
memset(maze, 0, sizeof(maze));
t = 0;
for (int i = 3; i >= 1; --i) {
for (int j = 3; j >= 1; --j) {
maze[i][j] = num % 10;
num /= 10;
if (maze[i][j] == 0)
x = i, y = j;
}
}
} int h() const {
int ans = 0;
for (int i = 1; i <= 3; ++i)
for (int j = 1; j <= 3; ++j)
if (maze[i][j] != 0)
ans += abs(i - pos_x[maze[i][j]]) + abs(j - pos_y[maze[i][j]]); // Manhattan distance
return ans;
} int to_int() const {
int ans = 0;
for (int i = 1; i <= 3; ++i)
for (int j = 1; j <= 3; ++j)
ans = ans * 10 + maze[i][j]; // hash
return ans;
} bool operator<(const Status& other) const {
return h() + t > other.h() + other.t; // compare by f(x)
}
}; int a_star(int num) {
priority_queue<Status, vector<Status>> pq;
set<int> vis;
pq.push(Status(num));
vis.insert(num);
while (!pq.empty()) {
if (pq.top().h() == 0)
return pq.top().t; // exit
Status now = pq.top();
pq.pop();
++now.t;
int x = now.x, y = now.y;
for (int i = 0; i < 4; ++i) {
int tx = x + dx[i], ty = y + dy[i];
if (tx < 1 || tx > 3 || ty < 1 || ty > 3)
continue;
swap(now.maze[now.x][now.y], now.maze[tx][ty]);
now.x = tx;
now.y = ty;
if (!vis.count(now.to_int())) {
pq.push(now); // expand
vis.insert(now.to_int());
}
now.x = x;
now.y = y;
swap(now.maze[now.x][now.y], now.maze[tx][ty]); // backtrack
}
}
return -1; // unused value
} int main() {
int num;
cin >> num;
cout << a_star(num) << endl;
return 0;
}

IDA*

IDA* 就是基于迭代加深搜索的 A* 算法。所谓迭代加深,就是在 DFS 的基础上控制其搜索深度,一旦超过深度限制就停止搜索,若当前深度无法得到答案,则再增加深度限制。迭代加深搜索结合了 DFS 与 BFS 的优点,不需要占用大量空间,支持回溯,同时可以快速找到最优解,避免剪枝不充分而造成的大量无用搜素,并且不需要判重。此外,由于迭代加深算法基于 DFS,相对于 BFS 而言,其实现难度更低,代码量更少。IDA* 则是在迭代加深搜素的基础上加上了估值函数的剪枝。有关估值函数的内容,在 A* 部分 已经说明,此处不再赘述。具体代码如下:

#include <bits/stdc++.h>

using namespace std;
const int dx[] = { 1, -1, 0, 0 };
const int dy[] = { 0, 0, 1, -1 };
const int pos_x[] = { 2, 1, 1, 1, 2, 3, 3, 3, 2 };
const int pos_y[] = { 2, 1, 2, 3, 3, 3, 2, 1, 1 };
int lim; // depth limit
int m[5][5]; int h() {
int ans = 0;
for (int i = 1; i <= 3; ++i)
for (int j = 1; j <= 3; ++j)
if (m[i][j] != 0)
ans += abs(i - pos_x[m[i][j]]) + abs(j - pos_y[m[i][j]]); // Manhattan distance
return ans;
} bool dfs(int x, int y, int t, int lx, int ly) {
int dis = h();
if (t + dis > lim)
return false; // prune with f(x)
if (dis == 0)
return true; // exit
for (int i = 0; i < 4; ++i) {
int tx = x + dx[i], ty = y + dy[i];
if (tx < 1 || tx > 3 || ty < 1 || ty > 3)
continue;
if (tx == lx && ty == ly)
continue; // very important
swap(m[x][y], m[tx][ty]);
if (dfs(tx, ty, t + 1, x, y))
return true; // expand
swap(m[x][y], m[tx][ty]); // backtrack
}
return false;
} int main() {
int num;
cin >> num;
int sx, sy;
for (int i = 3; i >= 1; --i) {
for (int j = 3; j >= 1; --j) {
m[i][j] = num % 10;
num /= 10;
if (m[i][j] == 0)
sx = i, sy = j;
}
}
lim = 0;
while (!dfs(sx, sy, 0, -1, -1))
++lim; // IDA*
cout << lim << endl;
return 0;
}

转载请注明出处。原文地址:https://www.cnblogs.com/na-sr/p/8-puzzle.html

八数码问题(8-Puzzle Problem)的更多相关文章

  1. 八数码问题 Eight Digital Problem

    八数码问题 利用启发式搜索,找出以下问题的最优解. #include <iostream> #include <vector> #include <algorithm&g ...

  2. POJ 2893 M × N Puzzle——八数码有解条件

    题意:给定M*N的数码图,问能否移动到最终状态 分析 有解的判定条件可见 八数码有解条件 值得一提的是,这道题求逆序对卡树状数组,只能用归并排序. #include<cstdio> #in ...

  3. 2019HDU多校第四场 Just an Old Puzzle ——八数码有解条件

    理论基础 轮换与对换 概念:把 $S$ 中的元素 $i_1$ 变成 $i_2$,$i_2$ 变成 $i_3$ ... $i_k$ 又变成 $i_1$,并使 $S$ 中的其余元素保持不变的置换称为循环, ...

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

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

  5. poj 1077-Eight(八数码+逆向bfs打表)

    The 15-puzzle has been around for over 100 years; even if you don't know it by that name, you've see ...

  6. HDU 1043 Eight (BFS&#183;八数码&#183;康托展开)

    题意  输出八数码问题从给定状态到12345678x的路径 用康托展开将排列相应为整数  即这个排列在全部排列中的字典序  然后就是基础的BFS了 #include <bits/stdc++.h ...

  7. HDU 1043 Eight(八数码)

    HDU 1043 Eight(八数码) 00 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)   Problem Descr ...

  8. Eight(经典题,八数码)

    Eight Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  9. HDU 3567 Eight II(八数码 II)

    HDU 3567 Eight II(八数码 II) /65536 K (Java/Others)   Problem Description - 题目描述 Eight-puzzle, which is ...

随机推荐

  1. c++之元组std::tuple常见用法

    元组,c++11中引入的新的类型,可类比std::pair. 但是std::pair只能支持两个元素. 理论上, 元组支持0~任意个元素. 本文演示环境: VS2015 up3 0.头文件 #incl ...

  2. 【LeetCode】983. 最低票价 Minimum Cost For Tickets(C++ & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetco ...

  3. Bean拷贝工具

    Apache BeanUtils Spring BeanUtils cglib BeanCopier Hutool BeanUtil Mapstruct Dozer 1.Apache  BeanUti ...

  4. Causal Inference

    目录 Standardization 非参数情况 Censoring 参数模型 Time-varying 静态 IP weighting 无参数 Censoring 参数模型 censoring 条件 ...

  5. vue路由history模式刷新404问题解决方案

    更改router 的base // biz是二级目录,路由文件改成 const router = new VueRouter({ mode: 'history', // base: process.e ...

  6. 单芯片CS5265替代VL102+PS176|设计USB TYPEC转HDMI方案|替代VL102+PS176

    一.PS176概述PS176是一个显示端口 (DP)至HDMI 2.0视频接口转换器适用于需要视频协议转换的电缆适配器.电视接收器.监视器和其他应用.它将接受任何显示端口输入格式,包括DP 1.1a. ...

  7. VUE3 之 循环渲染

    1. 概述 老话说的好:单打独斗是不行的,要懂得合作. 言归正传,今天我们来聊聊 VUE3 的 循环渲染. 2. 循环渲染 2.1 循环渲染数组 <body> <div id=&qu ...

  8. vue源码中computed和watch的解读

    computed 会基于其内部的 响应式依赖 进行缓存. 只在相关 响应式依赖发生改变 时 它们才会重新求值. 可以在将模板中使用的常量放在计算属性中. watch 监听数据变化,并在监听回调函数中返 ...

  9. JSP的数据从数据库中获取导入type=date的input标签中

    jsp 页面最开始加上 <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %> & ...

  10. Error: Cannot find module '@dcloudio/uni-cli-i18n' 解决方案

    这个错误是因为node_modules缺少了   '@dcloudio/uni-cli-i18n' 以下是错误信息  解决方案: yarn add -D @dcloudio/uni-cli-i18n ...