大暴搜。
加个剪枝,如果当前棋盘与目标棋盘相差超过限定步数,那么肯定不行了。
注意要保证步数最小,所以要从小到大枚举步数。
/* Footprints In The Blood Soaked Snow */
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 7;
int ans[maxn][maxn] = {
{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},
};
int dx[] = {2, 1, -1, -2, -2, -1, 1, 2}, dy[] = {-1, -2, -2, -1, 1, 2, 2, 1};
int maxs;
inline bool check(int A[maxn][maxn]) {
for(int i = 1; i <= 5; i++) for(int j = 1; j <= 5; j++)
if(A[i][j] != ans[i][j]) return 0;
return 1;
}
inline bool eva(int A[maxn][maxn], int s) {
int cnt = 0;
for(int i = 1; i <= 5; i++) for(int j = 1; j <= 5; j++)
if(A[i][j] != ans[i][j]) {
cnt++;
if(cnt + s > maxs) return 0;
}
return 1;
}
inline bool dfs(int A[maxn][maxn], int x, int y, int s) {
if(s == maxs && check(A)) return 1;
for(int i = 0; i < 8; i++) {
int xx = x + dx[i], yy = y + dy[i];
if(xx < 1 || xx > 5 || yy < 1 || yy > 5) continue;
swap(A[x][y], A[xx][yy]);
if(eva(A, s) && dfs(A, xx, yy, s + 1)) return 1;
swap(A[x][y], A[xx][yy]);
}
return 0;
}
inline bool solve(int A[maxn][maxn], int x, int y) {
for(maxs = 1; maxs <= 15; maxs++)
if(dfs(A, x, y, 0)) return 1;
return 0;
}
int main() {
int T; scanf("%d", &T);
while(T--) {
int x, y, A[maxn][maxn];
for(int i = 1; i <= 5; i++) {
char str[maxn]; scanf("%s", str + 1);
for(int j = 1; j <= 5; j++)
if(str[j] == '*') A[i][j] = 2, x = i, y = j;
else A[i][j] = str[j] - '0';
}
if(solve(A, x, y)) printf("%d\n", maxs);
else printf("-1\n");
}
return 0;
}
一开始写了bfs+hash,结果状态太多,炸了。
/* Footprints In The Blood Soaked Snow */
#include <cstdio>
#include <cstring>
#include <map>
#include <queue>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn = 7;
int ans[maxn][maxn] = {
{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},
};
int dx[] = {2, 1, -1, -2, -2, -1, 1, 2}, dy[] = {-1, -2, -2, -1, 1, 2, 2, 1};
struct _data {
int g[maxn][maxn], x, y, step;
};
LL ed;
map<LL, bool> vis;
queue<_data> q;
inline LL gethash(int u[maxn][maxn]) {
LL res = 0;
for(int i = 1; i <= 5; i++) for(int j = 1; j <= 5; j++) {
res *= 3;
res += u[i][j];
}
return res;
}
inline bool check(int u[maxn][maxn]) {
int cnt = 0;
for(int i = 1; i <= 5; i++) for(int j = 1; j <= 5; j++)
if(u[i][j] != ans[i][j]) {
cnt++;
if(cnt > 15) return 0;
}
return 1;
}
inline void solve() {
vis.clear();
for(; !q.empty(); q.pop());
_data st; st.step = 0;
for(int i = 1; i <= 5; i++) {
char str[maxn]; scanf("%s", str + 1);
for(int j = 1; j <= 5; j++)
if(str[j] == '*') st.g[i][j] = 2, st.x = i, st.y = j;
else st.g[i][j] = str[j] - '0';
}
LL QAQ = gethash(st.g);
if(QAQ == ed) {
printf("0\n");
return;
}
vis[QAQ] = 1;
q.push(st);
while(!q.empty()) {
_data u = q.front(); q.pop();
if(u.step > 15) {
printf("-1\n");
return;
}
for(int i = 0; i < 8; i++) {
int x = u.x + dx[i], y = u.y + dy[i];
if(x < 1 || x > 5 || y < 1 || y > 5) continue;
swap(u.g[u.x][u.y], u.g[x][y]);
LL tmp = gethash(u.g);
if(!vis[tmp]) {
if(tmp == ed) {
printf("%d\n", u.step + 1);
return;
}
vis[tmp] = 1;
_data t; t = u; t.x = x; t.y = y; t.step++;
q.push(t);
}
swap(u.g[u.x][u.y], u.g[x][y]);
}
}
}
int main() {
int T; scanf("%d", &T);
ed = gethash(ans);
while(T--) solve();
return 0;
}