Files
python/TangDou/AcWing/BFS/AStar/179_AStar_WithoutPath_845.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

83 lines
2.3 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
typedef unordered_map<string, int> MSI;
string st, ed = "12345678x";
//上右下左
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
//伟大的结构体放光芒
struct Node {
int dist;
string str;
bool operator<(const Node &t) const {
return dist > t.dist; //距离大的反而小,距离小的反而大。所以,配合优先队列(大顶堆),小的在上,大的在下
}
};
//估值函数,用曼哈顿距离估值
int f(string str) {
int res = 0;
for (int i = 0; i < str.size(); i++)
if (str[i] != 'x') {
int t = str[i] - '1';
res += abs(i / 3 - t / 3) + abs(i % 3 - t % 3);
}
return res;
}
int astar() {
MSI dist;
priority_queue<Node> q;
//将估价函数与字符串形成数对,放入到优先队列中
q.push({f(st), st});
dist[st] = 0; //记录距离为0
while (q.size()) {
string t = q.top().str;
q.pop();
if (t == ed) break;
int k = t.find('x');
int tx = k / 3, ty = k % 3;
for (int i = 0; i < 4; i++) {
int x = tx + dx[i], y = ty + dy[i];
if (x < 0 || x >= 3 || y < 0 || y >= 3) continue; //出界
string v = t; //将t字符串复制出来一个,生成 v字符串
swap(v[k], v[x * 3 + y]); //将原字符串中x与目标位置进行交换,生成新的目标状态字符串v
if (dist[v]) continue;
dist[v] = dist[t] + 1; // 更新最小值
q.push({dist[v] + f(v), v}); // 将新的估价函数计算更新,重新入队列
}
}
//返回结果
return dist[ed];
}
int main() {
char c;
for (int i = 0; i < 9; i++) cin >> c, st += c;
//八数码定理
int nums = 0;
for (int i = 0; i < 9; i++) {
if (st[i] == 'x') continue;
for (int j = i + 1; j < 9; j++) {
if (st[j] == 'x') continue;
if (st[j] < st[i]) nums++; //逆序数
}
}
//奇数个逆序对,无解
if (nums & 1)
puts("-1");
else //运行astar算法,返回最短距离
printf("%d\n", astar());
return 0;
}