用户输出
4
1222
1424
3242
4344
系统信息
Exited with return code 0
编号 | 题目 | 状态 | 分数 | 总时间 | 内存 | 代码 / 答案文件 | 提交者 | 提交时间 |
---|---|---|---|---|---|---|---|---|
#81733 | #1045. mm逗黑白猫 | Accepted | 100 | 319 ms | 12788 K | C++ / 2.1 K | C2024liuhongyi | 2023-02-17 20:51:47 |
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int MAXN = 1e3 + 5;
int dx[5] = { -1, 0, 0, 1 }, dy[5] = { 0, -1, 1, 0 }, st[5][5], ed[5][5], id, k[MAXN];
map<ll, bool> vis;
struct node {
int a[5][5], step, ans[MAXN];
node(int aa[][5], int sstep, int _ans[]) {
for (int i = 1; i <= 4; i++) {
for (int j = 1; j <= 4; j++) {
a[i][j] = aa[i][j];
}
}
step = sstep;
for (int i = 1; i <= step; i++) ans[i] = _ans[i];
}
};
ll Hash(int a[][5]) {
ll sum = 0;
for (int i = 1; i <= 4; i++) {
for (int j = 1; j <= 4; j++) {
sum = sum * 10 + a[i][j];
}
}
return sum;
}
int get(int a, int b, int c, int d) { return a * 1000 + b * 100 + c * 10 + d; }
queue<node> q;
void dbfs() {
q.push(node(st, 0, k));
vis[Hash(st)] = 1;
while (!q.empty()) {
int a[5][5], step = q.front().step + 1, ans[MAXN];
bool f = 0;
for (int i = 1; i <= 4; i++) {
for (int j = 1; j <= 4; j++) {
a[i][j] = q.front().a[i][j];
f |= (ed[i][j] != a[i][j]);
}
}
for (int i = 1; i <= step; i++) ans[i] = q.front().ans[i];
if (!f) {
printf("%d\n", step - 1);
for (int i = 1; i < step; i++) printf("%d\n", ans[i]);
exit(0);
}
q.pop();
for (int i = 1; i <= 4; i++) {
for (int j = 1; j <= 4; j++) {
for (int k = 0; k < 4; k++) {
int xx = i + dx[k], yy = j + dy[k];
if (xx >= 1 && xx <= 4 && yy >= 1 && yy <= 4) {
swap(a[i][j], a[xx][yy]);
if (!vis[Hash(a)]) {
if (i > xx)
ans[step] = get(xx, yy, i, j);
else if (i == xx && j > yy)
ans[step] = get(xx, yy, i, j);
else
ans[step] = get(i, j, xx, yy);
vis[Hash(a)] = 1;
q.push(node(a, step, ans));
}
swap(a[i][j], a[xx][yy]);
}
}
}
}
}
}
int main() {
for (int i = 1; i <= 4; i++) {
char c;
for (int j = 1; j <= 4; j++) {
cin >> c;
st[i][j] = c - '0';
}
}
for (int i = 1; i <= 4; i++) {
char c;
for (int j = 1; j <= 4; j++) {
cin >> c;
ed[i][j] = c - '0';
}
}
dbfs();
return 0;
}
用户输出
4
1222
1424
3242
4344
系统信息
Exited with return code 0
用户输出
3
1323
3242
2232
系统信息
Exited with return code 0
用户输出
6
1112
1314
1323
2333
3132
4142
系统信息
Exited with return code 0
用户输出
9
1213
1112
1314
1213
2232
2223
2434
3343
3444
系统信息
Exited with return code 0
用户输出
11
1112
1323
2131
1121
2232
2333
2324
3141
3132
3343
2333
系统信息
Exited with return code 0
用户输出
7
2131
2122
2333
1323
1314
3141
3132
系统信息
Exited with return code 0
用户输出
15
1213
1112
1314
2333
2223
2122
2434
1424
3141
3242
2232
3343
2333
3444
2434
系统信息
Exited with return code 0
用户输出
14
1424
1314
1213
1112
2232
1222
2333
2223
2434
3141
3242
3343
2333
3444
系统信息
Exited with return code 0
用户输出
10
1121
2223
2122
2333
2434
3141
3242
3343
4344
4243
系统信息
Exited with return code 0