1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
| #include <bits/stdc++.h> using namespace std; const int N = 35;
const int inf = 99999999; int n, m, q, ex, ey, bx, by, cx, cy; bool mapp[N][N]; int xx[4] = {-1, 1, 0, 0}; int yy[4] = {0, 0, -1, 1}; int getnum(int ax, int ay, int t) { return ((ax - 1) * m + ay) * 4 - (4 - t); }
struct blank { int x, y, step; };
bool judge(int ax, int ay) { if (ax <= 0 || ax > n || ay <= 0 || ay > m) return 0; return mapp[ax][ay]; } bool vis[N][N]; int bfs(int dx, int dy, int sx, int sy, int tx, int ty) { queue<blank> q; memset(vis, 0, sizeof vis); blank st; st.x = sx; st.y = sy; st.step = 0; vis[sx][sy] = 1; q.push(st); while (!q.empty()) { blank uu = q.front(); q.pop(); if (uu.x == tx && uu.y == ty) return uu.step; for (int i = 0; i < 4; i++) { if (judge(uu.x + xx[i], uu.y + yy[i])) { if (vis[uu.x + xx[i]][uu.y + yy[i]]) continue; if (uu.x + xx[i] == dx && uu.y + yy[i] == dy) continue; blank nxt; nxt.x = uu.x + xx[i]; nxt.y = uu.y + yy[i]; nxt.step = uu.step + 1; q.push(nxt); vis[uu.x + xx[i]][uu.y + yy[i]] = 1; } } } return inf; } struct edge { int to, nxt, dis; } b[5005]; int head[5005], tot; void add(int u, int v, int w) { b[++tot].to = v; b[tot].nxt = head[u]; b[tot].dis = w; head[u] = tot; } bool ok[N][N][5]; void init() { for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { if (!mapp[i][j]) continue; for (int k = 0; k < 4; k++) if (judge(i + xx[k], j + yy[k])) ok[i][j][k] = 1; } for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) for (int k = 0; k < 4; k++) for (int l = k + 1; l < 4; l++) { if (ok[i][j][k] && ok[i][j][l]) { int aa = getnum(i, j, k); int bb = getnum(i, j, l); int cc = bfs(i, j, i + xx[k], j + yy[k], i + xx[l], j + yy[l]); if (cc == inf) continue; add(aa, bb, cc); add(bb, aa, cc); } } for (int i = 1; i <= n; i++) for (int j = 1; j < m; j++) { if (ok[i][j][3] && ok[i][j + 1][2]) { int aa = getnum(i, j, 3); int bb = getnum(i, j + 1, 2); add(aa, bb, 1); add(bb, aa, 1); } } for (int i = 1; i < n; i++) for (int j = 1; j <= m; j++) { if (ok[i][j][1] && ok[i + 1][j][0]) { int aa = getnum(i, j, 1); int bb = getnum(i + 1, j, 0); add(aa, bb, 1); add(bb, aa, 1); } } } queue<int> Q;
bool viss[5000 + 10]; int d[5000 + 10]; void work() { memset(d, 128 / 3, sizeof d); memset(viss, 0, sizeof viss); for (int i = 0; i < 4; i++) { if (judge(bx + xx[i], by + yy[i])) { int nw = bfs(bx, by, ex, ey, bx + xx[i], by + yy[i]); if (nw == inf) continue; int nq = getnum(bx, by, i); d[nq] = nw; Q.push(nq); viss[nq] = 1; } } while (!Q.empty()) { int uu = Q.front(); Q.pop(); viss[uu] = 0; for (int j = head[uu]; j; j = b[j].nxt) { int vv = b[j].to; if (d[vv] > d[uu] + b[j].dis) { d[vv] = d[uu] + b[j].dis; if (!viss[vv]) Q.push(vv), viss[vv] = 1; } } } int ans = inf; for (int i = 0; i < 4; i++) { int qaq = getnum(cx, cy, i); ans = min(ans, d[qaq]); } if (ans == inf) puts("-1"); else printf("%d\n", ans); } int main() { scanf("%d%d%d", &n, &m, &q); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) cin >> mapp[i][j]; init(); while (q--) { scanf("%d%d%d%d%d%d", &ex, &ey, &bx, &by, &cx, &cy); if (bx == cx && by == cy) { puts("0"); continue; } if (!mapp[cx][cy]) { puts("-1"); continue; } if (!mapp[bx][by]) { puts("-1"); continue; } work(); } return 0; }
|