-
Notifications
You must be signed in to change notification settings - Fork 2
/
130.surrounded-regions.java
86 lines (75 loc) · 2.36 KB
/
130.surrounded-regions.java
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
/*
* @lc app=leetcode id=130 lang=java
*
* [130] Surrounded Regions
*/
// @lc code=start
class Solution {
class UnionFind {
private int[] parent;
public UnionFind(char[][] board) {
int m = board.length;
int n = board[0].length;
parent = new int[m * n + 1];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (board[i][j] == 'O') {
parent[i * n + j] = i * n + j;
}
}
}
}
public int find(int p) {
while (p != parent[p]) {
parent[p] = parent[parent[p]];
p = parent[p];
}
return p;
}
public void union(int p, int q) {
int rootP = find(p);
int rootQ = find(q);
if (rootP != rootQ) {
parent[rootP] = rootQ;
}
}
}
public void solve(char[][] board) {
if (board == null || board.length == 0) {
return;
}
int[][] directions = new int[][]{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
UnionFind uf = new UnionFind(board);
int m = board.length;
int n = board[0].length;
int dummyParent = m * n;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (board[i][j] == 'O') {
//check whether it is at border
if (i == 0 || j == 0 || i == m - 1 || j == n - 1) {
uf.union(dummyParent, i * n + j);
continue;
}
for (int[] dir : directions) {
int newX = i + dir[0];
int newY = j + dir[1];
if (newX >= 0 && newY >= 0 && newX < m && newY < n && board[newX][newY] == 'O') {
uf.union(i * n + j, newX * n + newY);
}
}
}
}
}
//final step, fill in the X
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (board[i][j] == 'O' && uf.find(i * n + j) != uf.find(dummyParent)) {
board[i][j] = 'X';
}
}
}
return;
}
}
// @lc code=end