-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution20.java
155 lines (148 loc) · 3.74 KB
/
Solution20.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import java.util.Queue;
import java.util.LinkedList;
/*
* Surrounded Regions
* A region is captured by flipping all 'O's into 'X's in that surrounded region
*/
public class Solution20 {
/* wrong method
public void solve(char[][] board) {
if (board == null || board.length < 3 || board[0].length < 3)
return ;
for (int i = 1; i < board.length-1; i++) {
for (int j = 1; j < board[i].length-1; j++) {
if (board[i][j] == 'o') {
int row = i, col = j;
int cnt = 0;
while (row >= 0) {
if (board[row][j] == 'x') {
cnt++;
break;
}
row--;
}
if (cnt != 1) break;
row = i;
while (row < board.length) {
if (board[row][j] == 'x') {
cnt++;
break;
}
row++;
}
if (cnt != 2) break;
while (col >= 0) {
if (board[i][col] == 'x') {
cnt++;
break;
}
col--;
}
if (cnt != 3) break;
col = j;
while (col < board[i].length) {
if (board[i][col] == 'x') {
cnt++;
break;
}
col++;
}
if (cnt == 4)
board[i][j] = 'x';
}
}
}
}*/
/* last test case runtime error
public void solve(char[][] board) {
if (board.length == 0 || (board.length < 3 && board[0].length < 3))
return ;
boolean[][] isChange = new boolean[board.length][board[0].length];
for (int i = 0; i < board[0].length; i++) {
change(board, isChange, 0, i);
change(board, isChange, board.length-1, i);
}
for (int i = 0; i < board.length; i++) {
change(board, isChange, i, 0);
change(board, isChange, i, board[i].length-1);
}
for (int i = 0; i < board.length; i++)
for (int j = 0; j < board[i].length; j++) {
if (board[i][j] == 'o')
board[i][j] = 'x';
else if (board[i][j] == '#')
board[i][j] = 'o';
}
}
public void change(char[][] board, boolean[][]isChange, int row, int col) {
if (row < 0 || row >= board.length ||col < 0 || col >= board[row].length || isChange[row][col])
return ;
isChange[row][col] = true;
if (board[row][col] == 'o') {
board[row][col] = '#';
change(board, isChange, row-1, col);
change(board, isChange, row+1, col);
change(board, isChange, row, col-1);
change(board, isChange, row, col+1);
}
}*/
// copy from https://github.com/mengli/leetcode/blob/master/Surrounded%20Regions.java, thanks for mengli
public class Point {
public int x;
public int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}
public void solve(char[][] board) {
int height = board.length;
int width = 0;
if (height > 0) {
width = board[0].length;
}
Queue<Point> q = new LinkedList<Point>();
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
if ((i == 0 || i == width - 1 || j == 0 || j == height - 1)
&& board[i][j] == 'O') {
q.add(new Point(i, j));
}
}
}
while (q.peek() != null) {
Point p = q.remove();
board[p.x][p.y] = '-';
if (p.x > 0 && board[p.x - 1][p.y] == 'O') {
q.add(new Point(p.x - 1, p.y));
}
if (p.x < width - 1 && board[p.x + 1][p.y] == 'O') {
q.add(new Point(p.x + 1, p.y));
}
if (p.y > 0 && board[p.x][p.y - 1] == 'O') {
q.add(new Point(p.x, p.y - 1));
}
if (p.y < height - 1 && board[p.x][p.y + 1] == 'O') {
q.add(new Point(p.x, p.y + 1));
}
}
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
if (board[i][j] != 'X')
board[i][j] = board[i][j] == 'O' ? 'X' : 'O';
}
}
}
public static void main(String... args) {
Solution20 s20 = new Solution20();
char[][] board = { "xxx".toCharArray(), "xox".toCharArray(),
"xxx".toCharArray() };
s20.solve(board);
for (char[] b : board) {
for (char bo : b) {
System.out.print(bo + " ");
}
System.out.println();
}
}
}