-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
0130-surrounded-regions.dart
51 lines (43 loc) · 1.19 KB
/
0130-surrounded-regions.dart
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
class Solution {
late List<List<String>> board;
late int rows;
late int columns;
bool isInBound(int row, int column) {
return 0 <= row && row < rows && 0 <= column && column < columns;
}
void dfs(int row, int column) {
if (!isInBound(row, column) || board[row][column] != 'O') {
return;
}
board[row][column] = 'T';
dfs(row + 1, column);
dfs(row, column + 1);
dfs(row - 1, column);
dfs(row, column - 1);
}
void solve(List<List<String>> board) {
this.board = board;
rows = board.length;
columns = board[0].length;
// Traverse the first and last columns
for (int column = 0; column < columns; column++) {
dfs(0, column);
dfs(rows - 1, column);
}
// Traverse the first and last rows
for (int row = 0; row < rows; row++) {
dfs(row, 0);
dfs(row, columns - 1);
}
// Replace all 'O' with 'X' and 'T' back to 'O'
for (int row = 0; row < rows; row++) {
for (int column = 0; column < columns; column++) {
if (board[row][column] == 'O') {
board[row][column] = 'X';
} else if (board[row][column] == 'T') {
board[row][column] = 'O';
}
}
}
}
}