[
Link]
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<ctime>
using namespace std;
struct mat {
short p[6][6];
int s;
int t;
};
short tar[6][6] = {
0, 0, 0, 0, 0, 0,
0, 1, 1, 1, 1, 1,
0, 0, 1, 1, 1, 1,
0, 0, 0, 2, 1, 1,
0, 0, 0, 0, 0, 1,
0, 0, 0, 0, 0, 0
};
bool a[6][6];
int ans = 0x3f3f3f3f;
int dx[8] = {1, -1, 1, -1, 2, -2, 2, -2};
int dy[8] = {2, -2, -2, 2, 1, -1, -1, 1};
char ch;
void Astar(const mat &hash, const int &diff, const int &step, const int &lim, const int &las) {
if (!diff) {
ans = min(ans, step);
return;
}
if (step == lim) return;
for (register int vs, vt, i = 0; i < 8; ++i) {
if (i == (las ^ 1)) continue;
vs = hash.s + dx[i];
vt = hash.t + dy[i];
if (vs > 5 || vs < 1) continue;
if (hash.t + dy[i] > 5 || hash.t + dy[i] < 1) continue;
mat tmp(hash);
int add( (tmp.p[tmp.s][tmp.t] == tar[tmp.s][tmp.t]) + (tmp.p[vs][tmp.t+dy[i]] == tar[vs][vt]) - (tmp.p[tmp.s][tmp.t] == tar[vs][vt]) - (tmp.p[vs][vt] == tar[tmp.s][tmp.t]) );
if (step + diff + add > lim) continue;
swap(tmp.p[tmp.s][tmp.t], tmp.p[vs][vt]);
tmp.s += dx[i];
tmp.t += dy[i];
Astar(tmp, diff + add, step + 1, lim, i);
}
}
int main() {
int T;
scanf("%d", &T);
while (T--) {
mat hash;
ans = 0x3f3f3f3f;
int diff(0);
for (int i = 1; i <= 5; ++i) {
for (int j = 1; j <= 5; ++j) {
scanf(" %c", &ch);
if (ch == '1') {
hash.p[i][j] = 1;
} else if (ch == '0') {
hash.p[i][j] = 0;
} else {
hash.p[i][j] = 2;
hash.s = i, hash.t = j;
}
if (hash.p[i][j] != tar[i][j]) ++diff;
}
}
for (int i = 0; i <= 15; ++i) {
Astar(hash, diff, 0, i, 8);
if (ans != 0x3f3f3f3f) break;
}
if (ans == 0x3f3f3f3f) ans = -1;
printf("%d\n", ans);
}
return 0;
}
BKDRhash记忆化
好像反而更慢了(也可能是我写丑了
没有判冲突,这个代码哇了
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<ctime>
using namespace std;
struct mat {
short p[6][6];
int s;
int t;
};
short tar[6][6] = {
0, 0, 0, 0, 0, 0,
0, 1, 1, 1, 1, 1,
0, 0, 1, 1, 1, 1,
0, 0, 0, 2, 1, 1,
0, 0, 0, 0, 0, 1,
0, 0, 0, 0, 0, 0
};
bool a[6][6];
int ans = 0x3f3f3f3f;
int dx[8] = {1, -1, 1, -1, 2, -2, 2, -2};
int dy[8] = {2, -2, -2, 2, 1, -1, -1, 1};
char ch;
int mns[1000005];
const int mod = 1000000;
int bkdrhash(const mat &x) {
int ret;
for (int i = 1; i <= 5; ++i) {
for (int j = 1; j <= 5; ++j) {
ret = ret * 31 + x.p[i][j];
}
}
if (ret < 0) ret += ((-ret)/mod+1)*mod;
ret %= mod;
return ret;
}
void Astar(const mat &hash, const int &diff, const int &step, const int &lim, const int &las) {
if (!diff) {
ans = min(ans, step);
return;
}
if (step == lim) return;
int val = bkdrhash(hash);
if (mns[val] < step) return;
mns[val] = step;
for (register int vs, vt, i = 0; i < 8; ++i) {
if (i == (las ^ 1)) continue;
vs = hash.s + dx[i];
vt = hash.t + dy[i];
if (vs > 5 || vs < 1) continue;
if (hash.t + dy[i] > 5 || hash.t + dy[i] < 1) continue;
mat tmp(hash);
int add( (tmp.p[tmp.s][tmp.t] == tar[tmp.s][tmp.t]) + (tmp.p[vs][tmp.t+dy[i]] == tar[vs][vt]) - (tmp.p[tmp.s][tmp.t] == tar[vs][vt]) - (tmp.p[vs][vt] == tar[tmp.s][tmp.t]) );
if (step + diff + add > lim) continue;
swap(tmp.p[tmp.s][tmp.t], tmp.p[vs][vt]);
tmp.s += dx[i];
tmp.t += dy[i];
Astar(tmp, diff + add, step + 1, lim, i);
}
}
int main() {
int T;
scanf("%d", &T);
while (T--) {
for (int i = 0; i < 1000005; ++i) mns[i] = 0x3f3f3f3f;
mat hash;
ans = 0x3f3f3f3f;
int diff(0);
for (int i = 1; i <= 5; ++i) {
for (int j = 1; j <= 5; ++j) {
scanf(" %c", &ch);
if (ch == '1') {
hash.p[i][j] = 1;
} else if (ch == '0') {
hash.p[i][j] = 0;
} else {
hash.p[i][j] = 2;
hash.s = i, hash.t = j;
}
if (hash.p[i][j] != tar[i][j]) ++diff;
}
}
for (int i = 0; i <= 15; ++i) {
Astar(hash, diff, 0, i, 8);
if (ans != 0x3f3f3f3f) break;
}
if (ans == 0x3f3f3f3f) ans = -1;
printf("%d\n", ans);
}
return 0;
}