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
| class Solution { public: int m, n; static constexpr int DIRs[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
unordered_map<int, pair<int, int>> roots;
vector<int> hitBricks(vector<vector<int>>& grid, vector<vector<int>>& hits) { m = grid.size(); n = grid[0].size(); set<pair<int, int>> h; for (int i = 0; i < hits.size(); i++) { int x = hits[i][0], y = hits[i][1]; h.insert({x, y}); } for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if (h.contains({i, j})) { if (grid[i][j]) { grid[i][j] = -1; } } if (grid[i][j] == 1) { roots[encode(i, j)] = {encode(i, j), 1}; } } } vector<vector<bool>> visited(m, vector<bool>(n, false)); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if (grid[i][j] == 1 && !visited[i][j]) { queue<pair<int, int>> q; q.emplace(i, j); visited[i][j] = true; while (!q.empty()) { auto [x, y] = q.front(); q.pop(); for (int k = 0; k < 4; k++) { int dx = x + DIRs[k][0], dy = y + DIRs[k][1]; if (dx >= 0 && dx < m && dy >= 0 && dy < n && grid[dx][dy] == 1 && !visited[dx][dy]) { visited[dx][dy] = 1; q.emplace(dx, dy); roots[encode(dx, dy)] = {encode(i, j), -1}; roots[encode(i, j)].second++; } } } } } } vector<int> res; for (int i = hits.size() - 1; i >= 0; i--) { int x = hits[i][0], y = hits[i][1]; if (grid[x][y] == 0) { res.push_back(0); continue; } set<pair<int, int>> neighbors; for (int k = 0; k < 4; k++) { int dx = x + DIRs[k][0], dy = y + DIRs[k][1]; if (dx >= 0 && dx < m && dy >= 0 && dy < n && grid[dx][dy] == 1) { auto root = findRoot(encode(dx, dy)); neighbors.insert(root); } } int cur = 0; if (neighbors.empty()) { roots[encode(x, y)] = {encode(x, y), 1}; } else if (neighbors.size() == 1) { auto it = neighbors.begin(); if (it->first < n) { roots[encode(x, y)] = {it->first, -1}; roots[it->first].second++; } else { int enc = it->first, num = it->second; roots[enc] = {encode(x, y), -1}; roots[encode(x, y)] = {encode(x, y), 1 + num}; if (x == 0) { cur = num; } }
} else { vector<pair<int, int>> v(neighbors.begin(), neighbors.end()); int potential_root = -1; for (auto root: v) { if (root.first < n) { potential_root = root.first; break; } } if (potential_root != -1) { roots[encode(x, y)] = {potential_root, -1}; roots[potential_root].second++; for (auto [root, num] : v) { if (root != potential_root) { roots[root] = {potential_root, -1}; roots[potential_root].second += num; if (root >= n) { cur += num; } } } } else { potential_root = v[0].first; roots[encode(x, y)] = {potential_root, -1}; roots[potential_root].second++; for (auto [root, num] : v) { if (root != potential_root) { roots[root] = {potential_root, -1}; roots[potential_root].second += num; } } } } res.push_back(cur); grid[x][y] = 1; } ranges::reverse(res); return res; }
int encode(int i, int j) { return i * n + j; }
pair<int, int> findRoot(int code) { if (roots[code].first != code) { roots[code] = findRoot(roots[code].first); } return roots[code]; } };
|