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
| #include <bits/stdc++.h> #define int long long #define INF 0x3f3f3f3f3f3f3f3f #define rep(i, a, b) for (int i = (a); i <= (b); i++) #define per(i, a, b) for (int i = (a); i >= (b); i--) #define pb push_back using namespace std; typedef pair<int, int> pii; const int N = 5e4 + 29;
int dx[5] = {0, 1, -1, 0, 0}; int dy[5] = {0, 0, 0, -1, 1}; int n, m, k, q, cnt; string str[N]; map<pii, int> mp; vector<int> a[N], g[N]; int dfn[N], low[N], ts, scc_cnt, id[N]; bool st[N]; stack<int> s;
void tarjan(int u) { dfn[u] = low[u] = ++ts; s.push(u); st[u] = true; for (auto v : a[u]) { if (!dfn[v]) { tarjan(v); low[u] = min(low[u], low[v]); } else low[u] = min(low[u], dfn[v]); } if (dfn[u] == low[u]) { int t; scc_cnt++; do { t = s.top(); s.pop(); st[t] = false; id[t] = scc_cnt; } while (u ^ t); } }
bool bfs(int s, int t) { queue<int> q; q.push(s); while (q.size()) { int u = q.front(); q.pop(); if (u == t) return true; for (auto v : g[u]) q.push(v); } return false; }
void tbcsolve() { cin >> n >> m >> k >> q; rep (i, 1, n) { cin >> str[i]; str[i] = '~' + str[i]; rep (j, 1, m) mp[{i, j}] = ++cnt; } rep (i, 1, n) { rep (j, 1, m) { if (str[i][j] == '#') continue; rep (o, 1, 4) { int x = i + dx[o], y = j + dy[o]; if (x < 1 || x > n || y < 1 || y > m || str[x][y] == '#') continue; a[mp[{i, j}]].pb(mp[{x, y}]); } } } while (k--) { int x_1, y_1, x_2, y_2; cin >> x_1 >> y_1 >> x_2 >> y_2; a[mp[{x_1, y_1}]].pb(mp[{x_2, y_2}]); } rep (i, 1, cnt) if (!dfn[i]) tarjan(i); rep (i, 1, cnt) { for (auto j : a[i]) { if (id[i] == id[j]) continue; g[id[i]].pb(id[j]); } } while (q--) { int x_1, y_1, x_2, y_2; cin >> x_1 >> y_1 >> x_2 >> y_2; cout << bfs(id[mp[{x_1, y_1}]], id[mp[{x_2, y_2}]]) << endl; } }
void tbcinit() { }
signed main() { freopen ("map.in", "r", stdin); freopen ("map.out", "w", stdout); ios:: sync_with_stdio(0), cin.tie(0), cout.tie(0); int SHENSELF = 1; while (SHENSELF--) { tbcinit(); tbcsolve(); } return 0; }
|