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
| #include <cstdio> #include <queue> using namespace std; template <class Type> void read(Type &x) { char c; bool flag = false; while ((c = getchar()) < '0' || c > '9') c == '-' && (flag = true); x = c - '0'; while ((c = getchar()) >= '0' && c <= '9') x = (x << 3) + (x << 1) + c - '0'; flag && (x = ~x + 1); } template <class Type> void write(Type x) { x < 0 && (putchar('-'), x = ~x + 1); x > 9 && (write(x / 10), 0); putchar(x % 10 + '0'); } const int N = 20, M = 1 << 12; const int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1}; bool vis[N][N][M]; int n, m, p, g[N][N][4], key[N][N]; bool inside(int x, int y) { return x > 0 && y > 0 && x <= n && y <= m; } struct Node { int x, y, s, k; }; queue<Node> q; int main() { read(n), read(m), read(p), read(p); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) for (int k = 0; k < 4; k++) { int nx = i + dx[k], ny = j + dy[k]; inside(nx, ny) && (g[i][j][k] = -1); } for (int x1, y1, x2, y2, t; p; p--) { read(x1), read(y1), read(x2), read(y2), read(t); for (int k = 0; k < 4; k++) if (x1 + dx[k] == x2 && y1 + dy[k] == y2) g[x1][y1][k] = t; else if (x2 + dx[k] == x1 && y2 + dy[k] == y1) g[x2][y2][k] = t; } read(p); for (int x, y, t; p; p--) { read(x), read(y), read(t); key[x][y] |= 1 << t; } q.push((Node){1, 1, 0, key[1][1]}); while (!q.empty()) { Node t = q.front(); q.pop(); if (vis[t.x][t.y][t.k]) continue; vis[t.x][t.y][t.k] = true; if (t.x == n && t.y == m) { write(t.s); return 0; } for (int k = 0; k < 4; k++) { int nx = t.x + dx[k], ny = t.y + dy[k]; int &u = g[t.x][t.y][k]; if (!inside(nx, ny) || !u || (u > 0 && !(t.k >> u & 1))) continue; q.push((Node){nx, ny, t.s + 1, t.k | key[nx][ny]}); } } write(-1); return 0; }
|