八數碼問題(8-Puzzle Problem)——多種搜索算法
P1379 八數碼難題 - 洛谷
題目概述
在 \(3 \times 3\) 的棋盤上擺放着 \(8\) 個棋子,棋子的編號分別為 \(1\) 到 \(8\),空格則用 \(0\) 表示。與空格直接相連的棋子可以移至空格中,這樣原來棋子的位置就成為空格。現給出一種初始布局,求到達目標布局的最少步數。為簡單起見,目標布局總是如下:
123 804 765
本題是一道經典的搜索題,下面將介紹幾種常見的搜索算法。以下所有代碼均需要 C++11 標准。
朴素 BFS
通過對題目的簡單分析,很容易寫出朴素的 BFS 代碼。進行訪問標記時,可以利用哈希的思想,將矩陣轉化為整數,再用 std::unordered_set
存儲。由於本題的數據范圍較小,朴素的 BFS 算法也能通過本題測試,但是效率較低。具體代碼如下:
View Code
#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,通過將實際拓展的層數減半,大大提高了搜索效率,避免了許多不必要的狀態拓展。具體代碼如下:
View Code
#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)\)。容易證明,該函數滿足上述條件。具體代碼如下:
View Code
#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* 部分 已經說明,此處不再贅述。具體代碼如下:
View Code
#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