6
6
7
7
<!-- 这里写题目描述 -->
8
8
9
- <p >在由 1 x 1 方格组成的 N x N 网格  ; <code >grid</code > 中,每个 1 x 1  ; 方块由 <code >/ </code >、<code >\ </code > 或空格构成。这些字符会将方块划分为一些共边的区域。</p >
9
+ <p >在由 < code > 1 x 1</ code > 方格组成的 < code >n & nbsp ; x n</ code > & nbsp ; 网格  ; <code >grid</code > 中,每个 < code > 1 x 1</ code >   ; 方块由 <code >'/' </code >、<code >'\' </code > 或空格构成。这些字符会将方块划分为一些共边的区域。</p >
10
10
11
- <p >(请注意,反斜杠字符是转义的,因此 <code >\ </code > 用 < code > & quot ; \\ & quot ; </ code > & nbsp ; 表示。) 。</p >
11
+ <p >给定网格 & nbsp ; <code >grid </code >& nbsp ; 表示为一个字符串数组,返回 < em >区域的数量</ em > 。</p >
12
12
13
- <p >返回区域的数目 。</p >
13
+ <p >请注意,反斜杠字符是转义的,因此 & nbsp ; < code >'\'</ code > 用 < code >'\\'</ code > & nbsp ; 表示 。</p >
14
14
15
15
<p >  ; </p >
16
16
19
19
20
20
<p ><strong >示例 1:</strong ></p >
21
21
22
- <pre ><strong >输入:
23
- </strong >[
24
-   ; " ; /" ; ,
25
-   ; " ; / " ;
26
- ]
27
- <strong >输出:</strong >2
28
- <strong >解释:</strong >2x2 网格如下:
29
- <img alt =" " src =" https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0900-0999/0959.Regions%20Cut%20By%20Slashes/images/1.png " ></pre >
22
+ <p ><img src =" https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0900-0999/0959.Regions%20Cut%20By%20Slashes/images/1.png " style =" height : 200px ; width : 200px ;" /></p >
23
+
24
+ <pre >
25
+ <strong >输入:</strong >grid = [" /","/ "]
26
+ <strong >输出:</strong >2</pre >
30
27
31
28
<p ><strong >示例 2:</strong ></p >
32
29
33
- <pre ><strong >输入:
34
- </strong >[
35
-   ; " ; /" ; ,
36
-   ; " ; " ;
37
- ]
30
+ <p ><img src =" https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0900-0999/0959.Regions%20Cut%20By%20Slashes/images/2.png " style =" height : 198px ; width : 200px ;" /></p >
31
+
32
+ <pre >
33
+ <strong >输入:</strong >grid = [" /"," "]
38
34
<strong >输出:</strong >1
39
- <strong >解释:</strong >2x2 网格如下:
40
- <img alt =" " src =" https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0900-0999/0959.Regions%20Cut%20By%20Slashes/images/2.png " ></pre >
35
+ </pre >
41
36
42
37
<p ><strong >示例 3:</strong ></p >
43
38
44
- <pre ><strong >输入:
45
- </strong >[
46
-   ; " ; \\/" ; ,
47
-   ; " ; /\\" ;
48
- ]
49
- <strong >输出:</strong >4
50
- <strong >解释:</strong >(回想一下,因为 \ 字符是转义的,所以 " ; \\/" ; 表示 \/,而 " ; /\\" ; 表示 /\。)
51
- 2x2 网格如下:
52
- <img alt =" " src =" https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0900-0999/0959.Regions%20Cut%20By%20Slashes/images/3.png " ></pre >
53
-
54
- <p ><strong >示例 4:</strong ></p >
55
-
56
- <pre ><strong >输入:
57
- </strong >[
58
-   ; " ; /\\" ; ,
59
-   ; " ; \\/" ;
60
- ]
39
+ <p ><img src =" https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0900-0999/0959.Regions%20Cut%20By%20Slashes/images/4.png " style =" height : 200px ; width : 200px ;" /></p >
40
+
41
+ <pre >
42
+ <strong >输入:</strong >grid = ["/\\","\\/"]
61
43
<strong >输出:</strong >5
62
- <strong >解释:</strong >(回想一下,因为 \ 字符是转义的,所以 " ; /\\" ; 表示 /\,而 " ; \\/" ; 表示 \/。)
63
- 2x2 网格如下:
64
- <img alt =" " src =" https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0900-0999/0959.Regions%20Cut%20By%20Slashes/images/4.png " ></pre >
65
-
66
- <p ><strong >示例 5:</strong ></p >
67
-
68
- <pre ><strong >输入:
69
- </strong >[
70
-   ; " ; //" ; ,
71
-   ; " ; / " ;
72
- ]
73
- <strong >输出:</strong >3
74
- <strong >解释:</strong >2x2 网格如下:
75
- <img alt =" " src =" https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0900-0999/0959.Regions%20Cut%20By%20Slashes/images/5.png " >
44
+ <strong >解释:</strong >回想一下,因为 \ 字符是转义的,所以 "/\\" 表示 /\,而 "\\/" 表示 \/。
76
45
</pre >
77
46
78
47
<p >  ; </p >
79
48
80
49
<p ><strong >提示:</strong ></p >
81
50
82
- <ol >
83
- <li><code>1 <= grid.length == grid[0].length <= 30</code></li>
84
- <li><code>grid[i][j]</code> 是 <code>'/'</code>、<code>'\'</code>、或 <code>' '</code>。</li>
85
- </ol >
51
+ <ul >
52
+ <li><code>n == grid.length == grid[i].length</code></li>
53
+ <li><code>1 <= n <= 30</code></li>
54
+ <li><code>grid[i][j]</code> 是 <code>'/'</code>、<code>'\'</code>、或 <code>' '</code></li>
55
+ </ul >
56
+
57
+
86
58
87
59
## 解法
88
60
89
61
<!-- 这里可写通用的实现逻辑 -->
90
62
91
- 并查集。
63
+ 并查集。对于本题,可以把每个方块看成四个三角形,从上开始顺时针编号 0,1,2,3, ` '/' ` 代表 0 和 3,1 和 2 连通, ` '\\' ` 代表 0 和 1,2 和 3 连通, ` ' ' ` 代表 0、1、2、3 都联通,然后再和方块周围的三角形联通,最后返回总的连通分量就得到结果了。
92
64
93
- 并查集模板:
65
+ 以下是并查集的几个常用模板。
94
66
95
67
模板 1——朴素并查集:
96
68
@@ -149,8 +121,6 @@ p[find(a)] = find(b)
149
121
d[find(a)] = distance
150
122
```
151
123
152
- 对于本题,可以把每个方块看成四个三角形,从上开始顺时针编号 0,1,2,3,` '/' ` 代表 0、3,1、2 连通,` '\\' ` 代表 0、1,2、3 连通,` ' ' ` 代表 0、1、2、3 都联通,然后再和方块周围的三角形联通,最后返回总的连通分量就得到结果了。
153
-
154
124
<!-- tabs:start -->
155
125
156
126
### ** Python3**
@@ -252,38 +222,48 @@ class Solution {
252
222
class Solution {
253
223
public:
254
224
vector<int > p;
225
+ int size;
255
226
256
227
int regionsBySlashes(vector<string>& grid) {
257
228
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] == '/')
229
+ size = n * n * 4;
230
+ p.resize(size);
231
+ for (int i = 0; i < size; ++i) p[i] = i;
232
+ for (int i = 0; i < n; ++i)
233
+ {
234
+ for (int j = 0; j < n; ++j)
235
+ {
236
+ int k = i * n + j;
237
+ if (i < n - 1) merge(4 * k + 2, (k + n) * 4);
238
+ if (j < n - 1) merge(4 * k + 1, (k + 1) * 4 + 3);
239
+ char v = grid[i][j];
240
+ if (v == '/')
266
241
{
267
- p[find(idx * 4)] = find(idx * 4 + 3);
268
- p[find(idx * 4 + 1)] = find(idx * 4 + 2);
242
+ merge(4 * k, 4 * k + 3);
243
+ merge(4 * k + 1, 4 * k + 2);
269
244
}
270
- else if (row[j] == ' \\ ' )
245
+ else if (v == ' \\ ' )
271
246
{
272
- p[find(idx * 4)] = find(idx * 4 + 1);
273
- p[find(idx * 4 + 2)] = find(idx * 4 + 3);
247
+ merge (4 * k, 4 * k + 1);
248
+ merge(4 * k + 2, 4 * k + 3);
274
249
}
275
250
else
276
251
{
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);
252
+ merge(4 * k, 4 * k + 1);
253
+ merge(4 * k + 1, 4 * k + 2);
254
+ merge(4 * k + 2, 4 * k + 3);
280
255
}
281
256
}
282
257
}
283
- unordered_set<int> s;
284
- for (int i = 0; i < p.size(); ++i)
285
- s.insert(find(i));
286
- return s.size();
258
+ return size;
259
+ }
260
+
261
+ void merge(int a, int b) {
262
+ int pa = find(a);
263
+ int pb = find(b);
264
+ if (pa == pb) return;
265
+ p[pa] = pb;
266
+ --size;
287
267
}
288
268
289
269
int find(int x) {
@@ -296,49 +276,51 @@ public:
296
276
### ** Go**
297
277
298
278
``` go
299
- var p []int
300
-
301
279
func regionsBySlashes (grid []string ) int {
302
280
n := len (grid)
303
- p = make ([]int , n*n*4 )
304
- for i := 0 ; i < len (p); i++ {
281
+ size := n * n * 4
282
+ p := make ([]int , size)
283
+ for i := range p {
305
284
p[i] = i
306
285
}
307
- for i := 0 ; i < n; i++ {
308
- row := grid[i]
309
- for j := 0 ; j < n; j++ {
310
- idx := i*n + j
286
+ var find func (x int ) int
287
+ find = func (x int ) int {
288
+ if p[x] != x {
289
+ p[x] = find (p[x])
290
+ }
291
+ return p[x]
292
+ }
293
+ union := func (a, b int ) {
294
+ pa , pb := find (a), find (b)
295
+ if pa == pb {
296
+ return
297
+ }
298
+ p[pa] = pb
299
+ size--
300
+ }
301
+ for i , row := range grid {
302
+ for j , v := range row {
303
+ k := i*n + j
311
304
if i < n-1 {
312
- p[ find (idx* 4 + 2 )] = find ((idx + n) * 4 )
305
+ union ( 4 *k+ 2 , (k+n)* 4 )
313
306
}
314
307
if j < n-1 {
315
- p[ find (idx* 4 + 1 )] = find ((idx +1 )*4 + 3 )
308
+ union ( 4 *k+ 1 , (k +1 )*4 + 3 )
316
309
}
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 )
310
+ if v == ' /' {
311
+ union ( 4 *k, 4 *k+ 3 )
312
+ union ( 4 *k+ 1 , 4 *k+ 2 )
313
+ } else if v == ' \\ ' {
314
+ union ( 4 *k, 4 *k+ 1 )
315
+ union ( 4 *k+ 2 , 4 *k+ 3 )
323
316
} 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 )
317
+ union ( 4 *k, 4 *k+ 1 )
318
+ union ( 4 *k+ 1 , 4 *k+ 2 )
319
+ union ( 4 *k+ 2 , 4 *k+ 3 )
327
320
}
328
321
}
329
322
}
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]
323
+ return size
342
324
}
343
325
```
344
326
0 commit comments