-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSurroundedRegion.java
85 lines (76 loc) · 2.71 KB
/
SurroundedRegion.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
package main.java.topcodingquestion.treesandgraphs;
import java.util.LinkedList;
import java.util.Queue;
public class SurroundedRegion {
public static void main(String[] args) {
}
public void solve(char[][] board) {
if (board.length == 0) return;
Queue<Pair> queue = new LinkedList<>();
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[0].length; j++) {
if ((i == 0 || i == board.length - 1) || (j == 0 || j == board[0].length - 1) && board[i][j] == 'O') {
queue.add(new Pair(i, j));
}
}
}
while (!queue.isEmpty()) {
Pair current = queue.poll();
if (current.x >= 0 && current.x < board.length && current.y >= 0 && current.y < board[0].length
&& board[current.x][current.y] == '0') {
board[current.x][current.y] = 'D';
queue.add(new Pair(current.x - 1, current.y));
queue.add(new Pair(current.x + 1, current.y));
queue.add(new Pair(current.x, current.y - 1));
queue.add(new Pair(current.x, current.y + 1));
}
}
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[0].length; j++) {
if (board[i][j] == '0') {
board[i][j] = 'X';
}
if (board[i][j] == 'D') {
board[i][j] = '0';
}
}
}
}
public void solveII(char[][] board) {
if (board.length == 0) return;
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[0].length; j++) {
if ((i == 0 || i == board.length - 1) || (j == 0 || j == board[0].length - 1) && board[i][j] == 'O') {
dfs(board, i, j);
}
}
}
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[0].length; j++) {
if (board[i][j] == 'O') {
board[i][j] = 'X';
}
if (board[i][j] == 'D') {
board[i][j] = 'O';
}
}
}
}
private void dfs(char[][] board, int row, int col) {
if (row >= 0 && row < board.length && col >= 0 && col < board[0].length && board[row][col] == 'O') {
board[row][col] = 'D';
dfs(board, row + 1, col);
dfs(board, row - 1, col);
dfs(board, row, col + 1);
dfs(board, row, col - 1);
}
}
class Pair {
int x;
int y;
Pair(int x, int y) {
this.x = x;
this.y = y;
}
}
}