|
53 | 53 | <li><code>cells</code> 中的所有格子坐标都是 <strong>唯一</strong> 的。</li>
|
54 | 54 | </ul>
|
55 | 55 |
|
56 |
| - |
57 | 56 | ## 解法
|
58 | 57 |
|
59 | 58 | <!-- 这里可写通用的实现逻辑 -->
|
60 | 59 |
|
| 60 | +逆序并查集。 |
| 61 | + |
| 62 | +并查集模板: |
| 63 | + |
| 64 | +模板 1——朴素并查集: |
| 65 | + |
| 66 | +```python |
| 67 | +# 初始化,p存储每个点的父节点 |
| 68 | +p = list(range(n)) |
| 69 | + |
| 70 | +# 返回x的祖宗节点 |
| 71 | +def find(x): |
| 72 | + if p[x] != x: |
| 73 | + # 路径压缩 |
| 74 | + p[x] = find(p[x]) |
| 75 | + return p[x] |
| 76 | + |
| 77 | +# 合并a和b所在的两个集合 |
| 78 | +p[find(a)] = find(b) |
| 79 | +``` |
| 80 | + |
| 81 | +模板 2——维护 size 的并查集: |
| 82 | + |
| 83 | +```python |
| 84 | +# 初始化,p存储每个点的父节点,size只有当节点是祖宗节点时才有意义,表示祖宗节点所在集合中,点的数量 |
| 85 | +p = list(range(n)) |
| 86 | +size = [1] * n |
| 87 | + |
| 88 | +# 返回x的祖宗节点 |
| 89 | +def find(x): |
| 90 | + if p[x] != x: |
| 91 | + # 路径压缩 |
| 92 | + p[x] = find(p[x]) |
| 93 | + return p[x] |
| 94 | + |
| 95 | +# 合并a和b所在的两个集合 |
| 96 | +if find(a) != find(b): |
| 97 | + size[find(b)] += size[find(a)] |
| 98 | + p[find(a)] = find(b) |
| 99 | +``` |
| 100 | + |
| 101 | +模板 3——维护到祖宗节点距离的并查集: |
| 102 | + |
| 103 | +```python |
| 104 | +# 初始化,p存储每个点的父节点,d[x]存储x到p[x]的距离 |
| 105 | +p = list(range(n)) |
| 106 | +d = [0] * n |
| 107 | + |
| 108 | +# 返回x的祖宗节点 |
| 109 | +def find(x): |
| 110 | + if p[x] != x: |
| 111 | + t = find(p[x]) |
| 112 | + d[x] += d[p[x]] |
| 113 | + p[x] = t |
| 114 | + return p[x] |
| 115 | + |
| 116 | +# 合并a和b所在的两个集合 |
| 117 | +p[find(a)] = find(b) |
| 118 | +d[find(a)] = distance |
| 119 | +``` |
| 120 | + |
61 | 121 | <!-- tabs:start -->
|
62 | 122 |
|
63 | 123 | ### **Python3**
|
64 | 124 |
|
65 | 125 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
66 | 126 |
|
67 | 127 | ```python
|
| 128 | +class Solution: |
| 129 | + def latestDayToCross(self, row: int, col: int, cells: List[List[int]]) -> int: |
| 130 | + n = row * col |
| 131 | + p = list(range(n + 2)) |
| 132 | + grid = [[False] * col for _ in range(row)] |
| 133 | + top, bottom = n, n + 1 |
| 134 | + |
| 135 | + def find(x): |
| 136 | + if p[x] != x: |
| 137 | + p[x] = find(p[x]) |
| 138 | + return p[x] |
68 | 139 |
|
| 140 | + def check(i, j): |
| 141 | + return 0 <= i < row and 0 <= j < col and grid[i][j] |
| 142 | + |
| 143 | + for k in range(len(cells) - 1, -1, -1): |
| 144 | + i, j = cells[k][0] - 1, cells[k][1] - 1 |
| 145 | + grid[i][j] = True |
| 146 | + for x, y in [[0, 1], [0, -1], [1, 0], [-1, 0]]: |
| 147 | + if check(i + x, j + y): |
| 148 | + p[find(i * col + j)] = find((i + x) * col + j + y) |
| 149 | + if i == 0: |
| 150 | + p[find(i * col + j)] = find(top) |
| 151 | + if i == row - 1: |
| 152 | + p[find(i * col + j)] = find(bottom) |
| 153 | + if find(top) == find(bottom): |
| 154 | + return k |
| 155 | + return 0 |
69 | 156 | ```
|
70 | 157 |
|
71 | 158 | ### **Java**
|
72 | 159 |
|
73 | 160 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
74 | 161 |
|
75 | 162 | ```java
|
| 163 | +class Solution { |
| 164 | + private int[] p; |
| 165 | + private int row; |
| 166 | + private int col; |
| 167 | + private boolean[][] grid; |
| 168 | + private int[][] dirs = new int[][]{{0, -1}, {0, 1}, {1, 0}, {-1, 0}}; |
| 169 | + |
| 170 | + public int latestDayToCross(int row, int col, int[][] cells) { |
| 171 | + int n = row * col; |
| 172 | + this.row = row; |
| 173 | + this.col = col; |
| 174 | + p = new int[n + 2]; |
| 175 | + for (int i = 0; i < p.length; ++i) { |
| 176 | + p[i] = i; |
| 177 | + } |
| 178 | + grid = new boolean[row][col]; |
| 179 | + int top = n, bottom = n + 1; |
| 180 | + for (int k = cells.length - 1; k >= 0; --k) { |
| 181 | + int i = cells[k][0] - 1, j = cells[k][1] - 1; |
| 182 | + grid[i][j] = true; |
| 183 | + for (int[] e : dirs) { |
| 184 | + if (check(i + e[0], j + e[1])) { |
| 185 | + p[find(i * col + j)] = find((i + e[0]) * col + j + e[1]); |
| 186 | + } |
| 187 | + } |
| 188 | + if (i == 0) { |
| 189 | + p[find(i * col + j)] = find(top); |
| 190 | + } |
| 191 | + if (i == row - 1) { |
| 192 | + p[find(i * col + j)] = find(bottom); |
| 193 | + } |
| 194 | + if (find(top) == find(bottom)) { |
| 195 | + return k; |
| 196 | + } |
| 197 | + } |
| 198 | + return 0; |
| 199 | + } |
| 200 | + |
| 201 | + private int find(int x) { |
| 202 | + if (p[x] != x) { |
| 203 | + p[x] = find(p[x]); |
| 204 | + } |
| 205 | + return p[x]; |
| 206 | + } |
| 207 | + |
| 208 | + private boolean check(int i, int j) { |
| 209 | + return i >= 0 && i < row && j >= 0 && j < col && grid[i][j]; |
| 210 | + } |
| 211 | +} |
| 212 | +``` |
| 213 | + |
| 214 | +### **C++** |
| 215 | + |
| 216 | +```cpp |
| 217 | +class Solution { |
| 218 | +public: |
| 219 | + vector<int> p; |
| 220 | + int dirs[4][2] = {{0, -1}, {0, 1}, {1, 0}, {-1, 0}}; |
| 221 | + int row, col; |
| 222 | + |
| 223 | + int latestDayToCross(int row, int col, vector<vector<int>>& cells) { |
| 224 | + int n = row * col; |
| 225 | + this->row = row; |
| 226 | + this->col = col; |
| 227 | + p.resize(n + 2); |
| 228 | + for (int i = 0; i < p.size(); ++i) p[i] = i; |
| 229 | + vector<vector<bool>> grid(row, vector<bool>(col, false)); |
| 230 | + int top = n, bottom = n + 1; |
| 231 | + for (int k = cells.size() - 1; k >= 0; --k) |
| 232 | + { |
| 233 | + int i = cells[k][0] - 1, j = cells[k][1] - 1; |
| 234 | + grid[i][j] = true; |
| 235 | + for (auto e : dirs) |
| 236 | + { |
| 237 | + if (check(i + e[0], j + e[1], grid)) |
| 238 | + { |
| 239 | + p[find(i * col + j)] = find((i + e[0]) * col + j + e[1]); |
| 240 | + } |
| 241 | + } |
| 242 | + if (i == 0) p[find(i * col + j)] = find(top); |
| 243 | + if (i == row - 1) p[find(i * col + j)] = find(bottom); |
| 244 | + if (find(top) == find(bottom)) return k; |
| 245 | + } |
| 246 | + return 0; |
| 247 | + } |
| 248 | + |
| 249 | + bool check(int i, int j, vector<vector<bool>>& grid) { |
| 250 | + return i >= 0 && i < row && j >= 0 && j < col && grid[i][j]; |
| 251 | + } |
| 252 | + |
| 253 | + int find(int x) { |
| 254 | + if (p[x] != x) p[x] = find(p[x]); |
| 255 | + return p[x]; |
| 256 | + } |
| 257 | +}; |
| 258 | +``` |
| 259 | +
|
| 260 | +### **Go** |
| 261 | +
|
| 262 | +```go |
| 263 | +var p []int |
| 264 | +
|
| 265 | +func latestDayToCross(row int, col int, cells [][]int) int { |
| 266 | + n := row * col |
| 267 | + p = make([]int, n+2) |
| 268 | + for i := 0; i < len(p); i++ { |
| 269 | + p[i] = i |
| 270 | + } |
| 271 | + grid := make([][]bool, row) |
| 272 | + for i := 0; i < row; i++ { |
| 273 | + grid[i] = make([]bool, col) |
| 274 | + } |
| 275 | + top, bottom := n, n+1 |
| 276 | + dirs := [4][2]int{{0, -1}, {0, 1}, {1, 0}, {-1, 0}} |
| 277 | + for k := len(cells) - 1; k >= 0; k-- { |
| 278 | + i, j := cells[k][0]-1, cells[k][1]-1 |
| 279 | + grid[i][j] = true |
| 280 | + for _, e := range dirs { |
| 281 | + if check(i+e[0], j+e[1], grid) { |
| 282 | + p[find(i*col+j)] = find((i+e[0])*col + j + e[1]) |
| 283 | + } |
| 284 | + } |
| 285 | + if i == 0 { |
| 286 | + p[find(i*col+j)] = find(top) |
| 287 | + } |
| 288 | + if i == row-1 { |
| 289 | + p[find(i*col+j)] = find(bottom) |
| 290 | + } |
| 291 | + if find(top) == find(bottom) { |
| 292 | + return k |
| 293 | + } |
| 294 | + } |
| 295 | + return 0 |
| 296 | +} |
| 297 | +
|
| 298 | +func check(i, j int, grid [][]bool) bool { |
| 299 | + return i >= 0 && i < len(grid) && j >= 0 && j < len(grid[0]) && grid[i][j] |
| 300 | +} |
76 | 301 |
|
| 302 | +func find(x int) int { |
| 303 | + if p[x] != x { |
| 304 | + p[x] = find(p[x]) |
| 305 | + } |
| 306 | + return p[x] |
| 307 | +} |
77 | 308 | ```
|
78 | 309 |
|
79 | 310 | ### **...**
|
|
0 commit comments