U536193 八数码
第一个代码是本题做法
#include <bits/stdc++.h>
using namespace std;
const int N = 6, M = 110, INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
typedef long long int LL;
#define x first
#define y second
typedef pair<int, string> PIS;
int f(string start)
{int res = 0;for (int i = 0; i < start.size(); i++){if (start[i] != 'x'){auto t = start[i] - '1';res += abs(i / 3 - t / 3) + abs(i % 3 - t % 3);}}return res;
}
int bfs(string start)
{char op[] = "urdl";int dx[4] = {-1, 0, 1, 0};//代表上右下左四个方向。int dy[4] = {0, 1, 0, -1}; // 代表上右下左四个方向。string end = "12345678x";priority_queue<PIS, vector<PIS>, greater<PIS>> heap;unordered_map<string, pair<char, string>> pre;unordered_map<string, int> dist;dist[start] = 0;heap.push({f(start), start});while (heap.size()){auto t = heap.top();heap.pop();string state = t.y;if (state == end)break;int x, y;for (int i = 0; i < 9; i++){if (state[i] == 'x'){x = i / 3, y = i % 3;break;}}string source = state;for (int i = 0; i < 4; i++){int a = x + dx[i], b = y + dy[i];if (a < 0 || a >= 3 || b < 0 || b >= 3)continue;state = source;swap(state[a * 3 + b], state[x * 3 + y]);if (dist.count(state) == 0 || dist[state] > dist[source] + 1){dist[state] = dist[source] + 1;pre[state] = {op[i], source};heap.push({f(state) + dist[state], state});}}}return dist[end];
}
int main()
{ios::sync_with_stdio(false);cin.tie(0), cout.tie(0);string start, end;int seq[8];int idx = 0;for (int i = 0; i < 9; i++){char x;cin >> x;start += x;if (x != 'x')seq[idx++] = x - '0';}int res = 0;for (int i = 0; i < 8; i++){for (int j = i + 1; j < 8; j++){if (seq[i] > seq[j])res++;}}if (res & 1)//如果逆序对为奇数的话,则无法到达目标状态。cout << -1 << endl;elsecout << bfs(start) << endl;return 0;
}
第二个代码是y总的记录方案做法
#include <bits/stdc++.h>
using namespace std;
const int N = 6, M = 110, INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
typedef long long int LL;
#define x first
#define y second
typedef pair<int, string> PIS;
int f(string start)
{int res = 0;for (int i = 0; i < start.size(); i++){if (start[i] != 'x'){auto t = start[i] - '1';res += abs(i / 3 - t / 3) + abs(i % 3 - t % 3);}}return res;
}
string bfs(string start)
{char op[] = "urdl";int dx[4] = {-1, 0, 1, 0};//代表上右下左四个方向。int dy[4] = {0, 1, 0, -1}; // 代表上右下左四个方向。string end = "12345678x";priority_queue<PIS, vector<PIS>, greater<PIS>> heap;unordered_map<string, pair<char, string>> pre;unordered_map<string, int> dist;dist[start] = 0;heap.push({f(start), start});while (heap.size()){auto t = heap.top();heap.pop();string state = t.y;if (state == end)break;int x, y;for (int i = 0; i < 9; i++){if (state[i] == 'x'){x = i / 3, y = i % 3;break;}}string source = state;for (int i = 0; i < 4; i++){int a = x + dx[i], b = y + dy[i];if (a < 0 || a >= 3 || b < 0 || b >= 3)continue;state = source;swap(state[a * 3 + b], state[x * 3 + y]);if (dist.count(state) == 0 || dist[state] > dist[source] + 1){dist[state] = dist[source] + 1;pre[state] = {op[i], source};heap.push({f(state) + dist[state], state});}}}string res;while (1){if (end == start)break;res += pre[end].x;end = pre[end].y;}reverse(res.begin(),res.end());return res;
}
int main()
{ios::sync_with_stdio(false);cin.tie(0), cout.tie(0);string start, end;int seq[8];int idx = 0;for (int i = 0; i < 9; i++){char x;cin >> x;start += x;if (x != 'x')seq[idx++] = x - '0';}int res = 0;for (int i = 0; i < 8; i++){for (int j = i + 1; j < 8; j++){if (seq[i] > seq[j])res++;}}if (res & 1)//如果逆序对为奇数的话,则无法到达目标状态。cout << -1 << endl;elsecout << bfs(start) << endl;return 0;
}
第三个是P1379 八数码难题做法
#include <bits/stdc++.h>
using namespace std;
const int N = 6, M = 110, INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
typedef long long int LL;
#define x first
#define y second
typedef pair<int, string> PIS;
string start;
int f(string x)//这个代表估计函数。
{int res = 0;for (int i = 0; i < x.size(); i++){if (x[i] != '0'){int t = x[i] - '1';//t表示实际在九宫格中的位置。res += abs(i / 3 - t / 3) + abs(i % 3 - t % 3);}}return res;
}
int bfs()
{string end = "123804765";priority_queue<PIS, vector<PIS>, greater<PIS>> heap;unordered_map<string, int> dist;int dx[4] = {-1, 0, 1, 0};int dy[4] = {0, 1, 0, -1};heap.push({f(start), start});dist[start] = 0;while (heap.size()){auto t = heap.top();heap.pop();string state = t.y;if (state == end)break;int x = 0, y = 0;for (int i = 0; i < 9; i++){if (state[i] == '0'){x = i / 3;y = i % 3;break;}}string source = state;for (int i = 0; i < 4; i++){int a = x + dx[i], b = y + dy[i];if (a < 0 || a >= 3 || b < 0 || b >= 3)continue;state = source;swap(state[a * 3 + b], state[x * 3 + y]);if (dist.count(state) == 0 || dist[state] > dist[source] + 1){dist[state] = dist[source] + 1;heap.push({dist[state] + f(state), state});}}}return dist[end];
}
int main()
{ios::sync_with_stdio(false);cin.tie(0), cout.tie(0);cin >> start;cout << bfs() << endl;return 0;
}