不是 AI!
2026-08-22 20:38:46
发布于:浙江
#include <bits/stdc++.h>
using namespace std;
void yz();
void bfs(int x);
int main() {
// freopen("xxx.in", "r", stdin);
// freopen("xxx.out", "w", stdout);
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int t = 1;
// cin >> t;
while (t--) yz();
}
map<int, bool> vis;
map<int, int> dis;
const int ans = 123804765;
const int dx[] = {0, 0, -1, 1};
const int dy[] = {-1, 1, 0, 0};
void yz() {
int s;
cin >> s;
bfs(s);
if (!dis.count(ans)) cout << "-1";
else cout << dis[ans];
}
void bfs(int x) {
queue<int> q;
q.push(x);
vis[x] = 1;
dis[x] = 0;
while (!q.empty()) {
int p = q.front();
q.pop();
int a[3][3];
a[0][0] = p / 100000000 % 10;
a[0][1] = p / 10000000 % 10;
a[0][2] = p / 1000000 % 10;
a[1][0] = p / 100000 % 10;
a[1][1] = p / 10000 % 10;
a[1][2] = p / 1000 % 10;
a[2][0] = p / 100 % 10;
a[2][1] = p / 10 % 10;
a[2][2] = p / 1 % 10;
int x, y;
for (int i = 0; i < 3; ++i)
for (int j = 0; j < 3; ++j)
if (a[i][j] == 0) {
x = i;
y = j;
break;
}
for (int i = 0; i < 4; ++i) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx < 0 || nx > 2 || ny < 0 || ny > 2) continue;
swap(a[x][y], a[nx][ny]);
int k = a[0][0] * 100000000 + a[0][1] * 10000000 + a[0][2] * 1000000 + a[1][0] * 100000 + a[1][1] * 10000 + a[1][2] * 1000 + a[2][0] * 100 + a[2][1] * 10 + a[2][2];
if (!vis[k]) {
vis[k] = 1;
dis[k] = dis[p] + 1;
q.push(k);
}
swap(a[x][y], a[nx][ny]);
}
}
}
- AI 的
main有东西读(我偏偏没有) - AI 的函数写在
main前面(我偏偏写后面) - AI 的方向数组
dx[], dy[]是变量(我偏偏写常量)
这里空空如也













有帮助,赞一个