-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathP_130.java
More file actions
46 lines (41 loc) · 1.37 KB
/
Copy pathP_130.java
File metadata and controls
46 lines (41 loc) · 1.37 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
package leetcode.medium;
public final class P_130 {
private static final int[][] DIRS = { { 0, 1 }, { 0, -1 }, { 1, 0 }, { -1, 0 } };
static int n;
static int m;
static char[][] g;
public void solve(char[][] board) {
n = board.length;
m = board[0].length;
g = board;
fillBorders(board, 'O', '*');
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (board[i][j] == 'O') {
board[i][j] = 'X';
}
}
}
fillBorders(board, '*', 'O');
}
private static void fillBorders(char[][] board, char prev, char next) {
for (int i = 0; i < n; i++) {
if (board[i][0] == prev) { dfs(i, 0, prev, next); }
if (board[i][m - 1] == prev) { dfs(i, m - 1, prev, next); }
}
for (int j = 0; j < m; j++) {
if (board[0][j] == prev) { dfs(0, j, prev, next); }
if (board[n - 1][j] == prev) { dfs(n - 1, j, prev, next); }
}
}
private static void dfs(int x, int y, char prev, char next) {
g[x][y] = next;
for (int[] dir : DIRS) {
final int nx = x + dir[0];
final int ny = y + dir[1];
if (0 <= nx && nx < n && 0 <= ny && ny < m && g[nx][ny] == prev) {
dfs(nx, ny, prev, next);
}
}
}
}