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
| #include #define pii pair #define tii tuple #define all(a) a.begin(), a.end() using namespace std; const int maxn = 1e3 + 10; char ch[maxn][maxn]; int n, m, ans[maxn][maxn][4]; int vis[maxn][maxn][4]; vector vec; bool istan(int x, int y, int dir) { char tem = ch[x][y]; if (tem == '/' || tem == '\\') return 1; if (tem == '-' && (dir == 0 || dir == 1)) return 1; if (tem == '|' && (dir == 2 || dir == 3)) return 1; return 0; } void dfs(int x, int y, int dir) { if (x < 1 || x > n || y < 1 || y > m) return; if (vis[x][y][dir]) return; vec.push_back(tii(x, y, dir)); vis[x][y][dir] = 1; if (ch[x][y] == '/') { if (dir == 0) { dfs(x, y + 1, 3); } else if (dir == 1) { dfs(x, y - 1, 2); } else if (dir == 2) { dfs(x + 1, y, 1); } else { dfs(x - 1, y, 0); } } else if (ch[x][y] == '\\') { if (dir == 0) { dfs(x, y - 1, 2); } else if (dir == 1) { dfs(x, y + 1, 3); } else if (dir == 2) { dfs(x - 1, y, 0); } else { dfs(x + 1, y, 1); } } else if (ch[x][y] == '-') { if (dir == 0) { dfs(x + 1, y, 1); } else if (dir == 1) { dfs(x - 1, y, 0); } else if (dir == 2) { dfs(x, y - 1, 2); } else dfs(x, y + 1, 3); } else if (ch[x][y] == '|') { if (dir == 0) dfs(x - 1, y, 0); else if (dir == 1) dfs(x + 1, y, 1); else if (dir == 2) { dfs(x, y + 1, 3); } else { dfs(x, y - 1, 2); } } } void solve1(int x, int y, int dir) { vec.clear(); dfs(x, y, dir); reverse(all(vec)); set s; for (auto [a, b, c] : vec) { if (istan(a, b, c)) s.insert(pii(a, b)); ans[a][b][c] = s.size(); } } void solve2(int x, int y, int dir) { vec.clear(); dfs(x, y, dir); set s; for (auto [a, b, c] : vec) { if (istan(a, b, c)) s.insert(pii(a, b)); } for (auto [a, b, c] : vec) ans[a][b][c] = s.size(); } signed main() { ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); cin >> n >> m; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) cin >> ch[i][j]; for (int i = 1; i <= n; i++) solve1(i, 1, 3), solve1(i, m, 2); for (int j = 1; j <= m; j++) solve1(1, j, 1), solve1(n, j, 0); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) for (int k = 0; k < 4; k++) if (!vis[i][j][k]) { solve2(i, j, k); } int q; cin >> q; map mp; mp["above"] = 0; mp["below"] = 1; mp["left"] = 2; mp["right"] = 3; while (q--) { int x, y; string str; cin >> x >> y >> str; int tem = mp[str]; if (tem == 0) x--; else if (tem == 1) x++; else if (tem == 2) y--; else y++; cout << ans[x][y][tem] << "\n"; } return 0; }
|