题目大意很简单,在一个不大于 30 × 30 30\times 30 30×30 的棋盘中,求出一个华容道游戏的最少步数解。所有棋子的大小都是 1 × 1 1\times 1 1×1,有些格子是不可移动的,共有 q ( q ≤ 500 ) q(q\leq 500) q(q≤500)次询问,每次询问给出待移动棋子坐标,目标坐标和空白坐标。考场上性价比比较高的做法是q次 O ( N 4 ) O(N^4) O(N4)的BFS,期望得分60分。注意到棋盘的状态是固定不变的,每次BFS都会进行大量重复的状态搜索,我们可以进行 n 2 次 O ( N 2 ) n^2次O(N^2) n2次O(N2)的BFS预处理出每个可行坐标紧邻有空白格子时,将该空格移动至另一紧邻状态的最少步数。对于每次询问,BFS出将空白格子移至待移动棋子周围的最短步数,加入队列后根据预处理的信息spfa出最短路。总复杂度 O ( N 4 + q k N 2 ) O(N^4+qkN^2) O(N4+qkN2)
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 35, INF = 0x3f3f3f3f;
const int MOVE[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};
bool ori[N][N];
int n, m, qq;
struct point{
int x, y;
inline void input() {
scanf("%d%d", &x, &y);
}
inline bool operator == (const point &rhs) const {
return x == rhs.x and y == rhs.y;
}
inline point move(int k) {
point now = (point){x + MOVE[k][0], y + MOVE[k][1]};
return now;
}
inline bool psb() {
return ori[x][y] && 1 <= x && x <= n && 1 <= y && y <= m;
}
};
typedef pair<point, int> pwd;
queue<pwd> pq;
queue<point> q;
int distB[N][N], dist[N][N][4][4];
bool visit[N][N];
#define mp make_pair
inline void initBlank(point center) {
for (int k = 0; k < 4; k++) {
point tmp = center.move(k);
if (tmp.psb()) pq.push(mp(tmp, k));
}
while (! pq.empty()) {
point st = pq.front().first;
int k1 = pq.front().second; pq.pop();
while (! q.empty()) q.pop();
memset(visit, 0, sizeof(visit));
distB[st.x][st.y] = 0; q.push(st); visit[st.x][st.y] = true;
while (! q.empty()) {
point dir = q.front(); q.pop();
for (int k = 0; k < 4; k++) {
point tmp = dir.move(k);
if (! tmp.psb() or tmp == center) continue;
if (! visit[tmp.x][tmp.y]) {
distB[tmp.x][tmp.y] = distB[dir.x][dir.y] + 1;
q.push(tmp); visit[tmp.x][tmp.y] = true;
}
}
}
for (int k2 = 0; k2 < 4; k2++) if (k1 != k2) {
point tmp = center.move(k2);
if (tmp.psb() && visit[tmp.x][tmp.y]) {
dist[center.x][center.y][k1][k2] = distB[tmp.x][tmp.y];
}
}
}
}
inline void preWork() {
memset(dist, 0x3f, sizeof(dist));
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) if (ori[i][j]) {
point tmp = (point){i, j};
initBlank(tmp);
}
}
point blank, src, tar;
inline void preSpfa() {
memset(distB, 0x3f, sizeof(distB));
distB[blank.x][blank.y] = 0;
q.push(blank);
while (! q.empty()) {
point dir = q.front(); q.pop();
for (int k = 0; k < 4; k++) {
point tmp = dir.move(k);
if (tmp.psb() && ! (tmp == src))
if (distB[tmp.x][tmp.y] == INF) {
distB[tmp.x][tmp.y] = distB[dir.x][dir.y] + 1;
q.push(tmp);
}
}
}
}
int dis[N][N][4];
bool inq[N][N][4];
#define inQ(rhs) inq[rhs.first.x][rhs.first.y][rhs.second]
#define diS(rhs) dis[rhs.first.x][rhs.first.y][rhs.second]
inline int spfa() {
if (src == tar) return 0;
preSpfa();
memset(dis, 0x3f, sizeof(dis));
for (int k = 0; k < 4; k++) {
point dir = src.move(k);
if (dir.psb() && distB[dir.x][dir.y] != INF) {
dis[src.x][src.y][k] = distB[dir.x][dir.y];
pwd now = mp(src, k);
pq.push(now); inQ(now) = true;
}
}
while (! pq.empty()) {
pwd dir = pq.front(); pq.pop();
inQ(dir) = false;
point tmp1 = dir.first.move(dir.second);
pwd tmp2 = mp(tmp1, dir.second^1);
if (diS(tmp2) > diS(dir) + 1) {
diS(tmp2) = diS(dir) + 1;
if (! inQ(tmp2)) {
pq.push(tmp2); inQ(tmp2) = true;
}
}
for (int i = 0; i < 4; i++) if (i != dir.second) {
tmp1 = dir.first.move(i);
int &l = dist[dir.first.x][dir.first.y][dir.second][i];
if (! tmp1.psb() || l == INF) continue;
tmp2 = mp(dir.first, i);
if (diS(tmp2) > diS(dir) + l) {
diS(tmp2) = diS(dir) + l;
if (! inQ(tmp2)) {
pq.push(tmp2); inQ(tmp2) = true;
}
}
}
}
int ans = INF;
for (int k = 0; k < 4; k++)
ans = min(ans, dis[tar.x][tar.y][k]);
return ans == INF ? -1 : ans;
}
int main() {
cin >> n >> m >> qq;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
int tmp; scanf("%d", &tmp);
ori[i][j] = tmp;
}
preWork();
while (qq--) {
blank.input(); src.input(); tar.input();
printf("%d\n", spfa());
}
return 0;
}