-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path130.cpp
45 lines (45 loc) · 1.29 KB
/
130.cpp
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
class Solution {
public:
void dfs(int i, int j, vector<vector<char>> &board, int rows, int columns) {
vector<pair<int, int>> directions = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
board[i][j] = 'B';
for (pair<int, int> direction : directions) {
int newI = i + direction.first;
int newJ = j + direction.second;
bool isValid = newI >= 0 && newJ >= 0 && newI < rows && newJ < columns;
if (isValid && board[newI][newJ] == 'O') {
dfs(newI, newJ, board, rows, columns);
}
}
}
void solve(vector<vector<char>> &board) {
int rows = board.size();
int columns = board[0].size();
for (int j = 0; j < columns; j++) {
if (board[0][j] == 'O') {
dfs(0, j, board, rows, columns);
}
if (board[rows - 1][j] == 'O') {
dfs(rows - 1, j, board, rows, columns);
}
}
for (int i = 0; i < rows; i++) {
if (board[i][0] == 'O') {
dfs(i, 0, board, rows, columns);
}
if (board[i][columns - 1] == 'O') {
dfs(i, columns - 1, board, rows, columns);
}
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
if (board[i][j] == 'O') {
board[i][j] = 'X';
}
if (board[i][j] == 'B') {
board[i][j] = 'O';
}
}
}
}
};