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
| #include <iostream> #include <algorithm> #include <queue> using namespace std;
int t, n, m, w, dx[4] = { 0,1,0,-1 }, dy[4] = { 1,0,-1,0 }, s1[110][2], s2[110][2], x, y, z1, z2;
struct p { int x, y, step; char c; }a[110][110];
void dfs() { queue<p> q; a[x][y].c = '1'; a[x][y].step = 0; q.push(a[x][y]); while (!q.empty()) { p s = q.front(); q.pop(); if (s.x == z1 && s.y == z2) { cout << s.step << endl; return; } int f = 1; for (int i = 0; i < w; ++i) { if (s.x == s1[i][0] && s.y == s1[i][1]) { a[s2[i][0]][s2[i][1]].c = '1'; a[s2[i][0]][s2[i][1]].step = s.step + 1; q.push(a[s2[i][0]][s2[i][1]]); f = 0; break; } } if (f) { for (int i = 0; i < 4; ++i) { int x = s.x + dx[i], y = s.y + dy[i]; if (x == -1 || x == n || y == -1 || y == m || a[x][y].c == '1') continue; a[x][y].c = '1'; a[x][y].step = s.step + 1; q.push(a[x][y]); } } } cout << "die" << endl; }
int main() { cin >> t; while (t--) { cin >> n >> m; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { cin >> a[i][j].c; a[i][j].x = i; a[i][j].y = j; } } cin >> w; for (int i = 0; i < w; ++i) cin >> s1[i][0] >> s1[i][1] >> s2[i][0] >> s2[i][1]; cin >> x >> y >> z1 >> z2; dfs(); } return 0; }
|