在由 1 x 1 方格组成的 N x N 网格 grid 中,每个 1 x 1 方块由 /、\ 或空格构成。这些字符会将方块划分为一些共边的区域。
(请注意,反斜杠字符是转义的,因此 \ 用 “\” 表示。)。
返回区域的数目。
示例 1:
输入:
[
" /",
"/ "
]
输出:2
解释:2x2 网格如下:
示例 2:
输入:
[
" /",
" "
]
输出:1
解释:2x2 网格如下:
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/regions-cut-by-slashes
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
ps:又是cv的一天,emmmm
class Solution {
public int regionsBySlashes(String[] grid) {
int n = grid.length;
int[] f = new int[n * n * 4];
for (int i = 0; i < n * n * 4; i++) {
f[i] = i;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
int idx = i * n + j;
if (i < n - 1) {
int bottom = idx + n;
merge(f, idx * 4 + 2, bottom * 4);
}
if (j < n - 1) {
int right = idx + 1;
merge(f, idx * 4 + 1, right * 4 + 3);
}
if (grid[i].charAt(j) == '/') {
merge(f, idx * 4, idx * 4 + 3);
merge(f, idx * 4 + 1, idx * 4 + 2);
} else if (grid[i].charAt(j) == '\\') {
merge(f, idx * 4, idx * 4 + 1);
merge(f, idx * 4 + 2, idx * 4 + 3);
} else {
merge(f, idx * 4, idx * 4 + 1);
merge(f, idx * 4 + 1, idx * 4 + 2);
merge(f, idx * 4 + 2, idx * 4 + 3);
}
}
}
Set<Integer> fathers = new HashSet<Integer>();
for (int i = 0; i < n * n * 4; i++) {
int fa = find(f, i);
fathers.add(fa);
}
return fathers.size();
}
public int find(int[] f, int x) {
if (f[x] == x) {
return x;
}
int fa = find(f, f[x]);
f[x] = fa;
return fa;
}
public void merge(int[] f, int x, int y) {
int fx = find(f, x);
int fy = find(f, y);
f[fx] = fy;
}
}