forked from ex01tus/leetcode-grind
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSurroundedRegions.java
More file actions
47 lines (40 loc) · 1.36 KB
/
SurroundedRegions.java
File metadata and controls
47 lines (40 loc) · 1.36 KB
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
package graph;
/**
* Description: https://leetcode.com/problems/surrounded-regions
* Difficulty: Medium
* Time complexity: O(m * n)
* Space complexity: O(m * n)
*/
public class SurroundedRegions {
private final int[][] directions = new int[][] {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
public void solve(char[][] board) {
for (int i = 0; i < board.length; i++) {
dfs(board, i, 0);
dfs(board, i, board[0].length - 1);
}
for (int i = 0; i < board[0].length; i++) {
dfs(board, 0, i);
dfs(board, board.length - 1, i);
}
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[0].length; j++) {
if (board[i][j] == 'F') { // all 'F' regions have access to the boarders
board[i][j] = 'O';
} else if (board[i][j] == 'O') { // the rest of 'O' are surrounded
board[i][j] = 'X';
}
}
}
}
private void dfs(char[][] board, int x, int y) {
if (board[x][y] != 'O') return;
board[x][y] = 'F';
for (int[] dir : directions) {
int x1 = x + dir[0];
int y1 = y + dir[1];
if (x1 >= 0 && x1 < board.length && y1 >= 0 && y1 < board[0].length) {
dfs(board, x1, y1);
}
}
}
}