|
84 | 84 | <li><code>grid[i][j]</code> 是 <code>'/'</code>、<code>'\'</code>、或 <code>' '</code>。</li>
|
85 | 85 | </ol>
|
86 | 86 |
|
87 |
| - |
88 | 87 | ## 解法
|
89 | 88 |
|
90 | 89 | <!-- 这里可写通用的实现逻辑 -->
|
91 | 90 |
|
| 91 | +并查集。 |
| 92 | + |
| 93 | +并查集模板: |
| 94 | + |
| 95 | +模板 1——朴素并查集: |
| 96 | + |
| 97 | +```python |
| 98 | +# 初始化,p存储每个点的父节点 |
| 99 | +p = list(range(n)) |
| 100 | + |
| 101 | +# 返回x的祖宗节点 |
| 102 | +def find(x): |
| 103 | + if p[x] != x: |
| 104 | + # 路径压缩 |
| 105 | + p[x] = find(p[x]) |
| 106 | + return p[x] |
| 107 | + |
| 108 | +# 合并a和b所在的两个集合 |
| 109 | +p[find(a)] = find(b) |
| 110 | +``` |
| 111 | + |
| 112 | +模板 2——维护 size 的并查集: |
| 113 | + |
| 114 | +```python |
| 115 | +# 初始化,p存储每个点的父节点,size只有当节点是祖宗节点时才有意义,表示祖宗节点所在集合中,点的数量 |
| 116 | +p = list(range(n)) |
| 117 | +size = [1] * n |
| 118 | + |
| 119 | +# 返回x的祖宗节点 |
| 120 | +def find(x): |
| 121 | + if p[x] != x: |
| 122 | + # 路径压缩 |
| 123 | + p[x] = find(p[x]) |
| 124 | + return p[x] |
| 125 | + |
| 126 | +# 合并a和b所在的两个集合 |
| 127 | +if find(a) != find(b): |
| 128 | + size[find(b)] += size[find(a)] |
| 129 | + p[find(a)] = find(b) |
| 130 | +``` |
| 131 | + |
| 132 | +模板 3——维护到祖宗节点距离的并查集: |
| 133 | + |
| 134 | +```python |
| 135 | +# 初始化,p存储每个点的父节点,d[x]存储x到p[x]的距离 |
| 136 | +p = list(range(n)) |
| 137 | +d = [0] * n |
| 138 | + |
| 139 | +# 返回x的祖宗节点 |
| 140 | +def find(x): |
| 141 | + if p[x] != x: |
| 142 | + t = find(p[x]) |
| 143 | + d[x] += d[p[x]] |
| 144 | + p[x] = t |
| 145 | + return p[x] |
| 146 | + |
| 147 | +# 合并a和b所在的两个集合 |
| 148 | +p[find(a)] = find(b) |
| 149 | +d[find(a)] = distance |
| 150 | +``` |
| 151 | + |
| 152 | +对于本题,可以把每个方块看成四个三角形,从上开始顺时针编号 0,1,2,3,`'/'`代表 0、3,1、2 连通,`'\\'` 代表 0、1,2、3 连通,`' '` 代表 0、1、2、3 都联通,然后再和方块周围的三角形联通,最后返回总的连通分量就得到结果了。 |
| 153 | + |
92 | 154 | <!-- tabs:start -->
|
93 | 155 |
|
94 | 156 | ### **Python3**
|
95 | 157 |
|
96 | 158 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
97 | 159 |
|
98 | 160 | ```python
|
99 |
| - |
| 161 | +class Solution: |
| 162 | + def regionsBySlashes(self, grid: List[str]) -> int: |
| 163 | + n = len(grid) |
| 164 | + p = list(range(n * n * 4)) |
| 165 | + |
| 166 | + def find(x): |
| 167 | + if p[x] != x: |
| 168 | + p[x] = find(p[x]) |
| 169 | + return p[x] |
| 170 | + |
| 171 | + for i in range(n): |
| 172 | + for j in range(n): |
| 173 | + idx = i * n + j |
| 174 | + if i < n - 1: |
| 175 | + p[find(idx * 4 + 2)] = find((idx + n) * 4) |
| 176 | + if j < n - 1: |
| 177 | + p[find(idx * 4 + 1)] = find((idx + 1) * 4 + 3) |
| 178 | + |
| 179 | + if grid[i][j] == '/': |
| 180 | + p[find(idx * 4)] = find(idx * 4 + 3) |
| 181 | + p[find(idx * 4 + 1)] = find(idx * 4 + 2) |
| 182 | + elif grid[i][j] == '\\': |
| 183 | + p[find(idx * 4)] = find(idx * 4 + 1) |
| 184 | + p[find(idx * 4 + 2)] = find(idx * 4 + 3) |
| 185 | + else: |
| 186 | + p[find(idx * 4)] = find(idx * 4 + 1) |
| 187 | + p[find(idx * 4 + 1)] = find(idx * 4 + 2) |
| 188 | + p[find(idx * 4 + 2)] = find(idx * 4 + 3) |
| 189 | + s = set() |
| 190 | + for i in range(len(p)): |
| 191 | + s.add(find(i)) |
| 192 | + return len(s) |
100 | 193 | ```
|
101 | 194 |
|
102 | 195 | ### **Java**
|
103 | 196 |
|
104 | 197 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
105 | 198 |
|
106 | 199 | ```java
|
| 200 | +class Solution { |
| 201 | + private int[] p; |
| 202 | + |
| 203 | + public int regionsBySlashes(String[] grid) { |
| 204 | + int n = grid.length; |
| 205 | + p = new int[n * n * 4]; |
| 206 | + for (int i = 0; i < p.length; ++i) { |
| 207 | + p[i] = i; |
| 208 | + } |
| 209 | + for (int i = 0; i < n; ++i) { |
| 210 | + char[] row = grid[i].toCharArray(); |
| 211 | + for (int j = 0; j < n; ++j) { |
| 212 | + int idx = i * n + j; |
| 213 | + if (i < n - 1) { |
| 214 | + p[find(idx * 4 + 2)] = find((idx + n) * 4); |
| 215 | + } |
| 216 | + if (j < n - 1) { |
| 217 | + p[find(idx * 4 + 1)] = find((idx + 1) * 4 + 3); |
| 218 | + } |
| 219 | + |
| 220 | + if (row[j] == '/') { |
| 221 | + p[find(idx * 4)] = find(idx * 4 + 3); |
| 222 | + p[find(idx * 4 + 1)] = find(idx * 4 + 2); |
| 223 | + } else if (row[j] == '\\') { |
| 224 | + p[find(idx * 4)] = find(idx * 4 + 1); |
| 225 | + p[find(idx * 4 + 2)] = find(idx * 4 + 3); |
| 226 | + } else { |
| 227 | + p[find(idx * 4)] = find(idx * 4 + 1); |
| 228 | + p[find(idx * 4 + 1)] = find(idx * 4 + 2); |
| 229 | + p[find(idx * 4 + 2)] = find(idx * 4 + 3); |
| 230 | + } |
| 231 | + } |
| 232 | + } |
| 233 | + Set<Integer> s = new HashSet<>(); |
| 234 | + for (int i = 0; i < p.length; ++i) { |
| 235 | + s.add(find(i)); |
| 236 | + } |
| 237 | + return s.size(); |
| 238 | + } |
| 239 | + |
| 240 | + private int find(int x) { |
| 241 | + if (p[x] != x) { |
| 242 | + p[x] = find(p[x]); |
| 243 | + } |
| 244 | + return p[x]; |
| 245 | + } |
| 246 | +} |
| 247 | +``` |
| 248 | + |
| 249 | +### **C++** |
| 250 | + |
| 251 | +```cpp |
| 252 | +class Solution { |
| 253 | +public: |
| 254 | + vector<int> p; |
| 255 | + |
| 256 | + int regionsBySlashes(vector<string>& grid) { |
| 257 | + int n = grid.size(); |
| 258 | + for (int i = 0; i < n * n * 4; ++i) p.push_back(i); |
| 259 | + for (int i = 0; i < n; ++i) { |
| 260 | + string row = grid[i]; |
| 261 | + for (int j = 0; j < n; ++j) { |
| 262 | + int idx = i * n + j; |
| 263 | + if (i < n - 1) p[find(idx * 4 + 2)] = find((idx + n) * 4); |
| 264 | + if (j < n - 1) p[find(idx * 4 + 1)] = find((idx + 1) * 4 + 3); |
| 265 | + if (row[j] == '/') |
| 266 | + { |
| 267 | + p[find(idx * 4)] = find(idx * 4 + 3); |
| 268 | + p[find(idx * 4 + 1)] = find(idx * 4 + 2); |
| 269 | + } |
| 270 | + else if (row[j] == '\\') |
| 271 | + { |
| 272 | + p[find(idx * 4)] = find(idx * 4 + 1); |
| 273 | + p[find(idx * 4 + 2)] = find(idx * 4 + 3); |
| 274 | + } |
| 275 | + else |
| 276 | + { |
| 277 | + p[find(idx * 4)] = find(idx * 4 + 1); |
| 278 | + p[find(idx * 4 + 1)] = find(idx * 4 + 2); |
| 279 | + p[find(idx * 4 + 2)] = find(idx * 4 + 3); |
| 280 | + } |
| 281 | + } |
| 282 | + } |
| 283 | + unordered_set<int> s; |
| 284 | + for (int i = 0; i < p.size(); ++i) |
| 285 | + s.insert(find(i)); |
| 286 | + return s.size(); |
| 287 | + } |
| 288 | + |
| 289 | + int find(int x) { |
| 290 | + if (p[x] != x) p[x] = find(p[x]); |
| 291 | + return p[x]; |
| 292 | + } |
| 293 | +}; |
| 294 | +``` |
107 | 295 |
|
| 296 | +### **Go** |
| 297 | + |
| 298 | +```go |
| 299 | +var p []int |
| 300 | + |
| 301 | +func regionsBySlashes(grid []string) int { |
| 302 | + n := len(grid) |
| 303 | + p = make([]int, n*n*4) |
| 304 | + for i := 0; i < len(p); i++ { |
| 305 | + p[i] = i |
| 306 | + } |
| 307 | + for i := 0; i < n; i++ { |
| 308 | + row := grid[i] |
| 309 | + for j := 0; j < n; j++ { |
| 310 | + idx := i*n + j |
| 311 | + if i < n-1 { |
| 312 | + p[find(idx*4+2)] = find((idx + n) * 4) |
| 313 | + } |
| 314 | + if j < n-1 { |
| 315 | + p[find(idx*4+1)] = find((idx+1)*4 + 3) |
| 316 | + } |
| 317 | + if row[j] == '/' { |
| 318 | + p[find(idx*4)] = find(idx*4 + 3) |
| 319 | + p[find(idx*4+1)] = find(idx*4 + 2) |
| 320 | + } else if row[j] == '\\' { |
| 321 | + p[find(idx*4)] = find(idx*4 + 1) |
| 322 | + p[find(idx*4+2)] = find(idx*4 + 3) |
| 323 | + } else { |
| 324 | + p[find(idx*4)] = find(idx*4 + 1) |
| 325 | + p[find(idx*4+1)] = find(idx*4 + 2) |
| 326 | + p[find(idx*4+2)] = find(idx*4 + 3) |
| 327 | + } |
| 328 | + } |
| 329 | + } |
| 330 | + s := make(map[int]bool) |
| 331 | + for i := 0; i < len(p); i++ { |
| 332 | + s[find(i)] = true |
| 333 | + } |
| 334 | + return len(s) |
| 335 | +} |
| 336 | + |
| 337 | +func find(x int) int { |
| 338 | + if p[x] != x { |
| 339 | + p[x] = find(p[x]) |
| 340 | + } |
| 341 | + return p[x] |
| 342 | +} |
108 | 343 | ```
|
109 | 344 |
|
110 | 345 | ### **...**
|
|
0 commit comments