-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolve.cpp
More file actions
36 lines (35 loc) · 1.16 KB
/
solve.cpp
File metadata and controls
36 lines (35 loc) · 1.16 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
class Solution {
public:
void solve(vector<vector<char>>& board) {
int m = board.size(), n = board[0].size();
if (m <= 2 || n <= 2) return;
queue<pair<int, int> > q;
// borders
for (int i = 0; i < n; i++) {
if (board[0][i] == 'O') q.push({0, i});
if (board[m - 1][i] == 'O') q.push({m - 1, i});
}
for (int i = 1; i < m - 1; i++) {
if (board[i][0] == 'O') q.push({i, 0});
if (board[i][n - 1] == 'O') q.push({i, n - 1});
}
int dir[5] = {1, 0, -1, 0, 1};
while (!q.empty()) {
auto [r, c] = q.front();
q.pop();
board[r][c] = 'a';
for (int i = 0; i < 4; i++) {
int x = r + dir[i], y = c + dir[i + 1];
if (x >= 0 && x < m && y >= 0 && y < n && board[x][y] == 'O') {
q.push({x, y});
}
}
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (board[i][j] == 'O') board[i][j] = 'X';
else if (board[i][j] == 'a') board[i][j] = 'O';
}
}
}
};