Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit da94292

Browse filesBrowse files
committed
add cpp solution
1 parent 5e0b76c commit da94292
Copy full SHA for da94292

File tree

1 file changed

+48
-0
lines changed
Filter options

1 file changed

+48
-0
lines changed
+48Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
const int dx[4] = {-1, 0, 1, 0};
2+
const int dy[4] = {0, -1, 0, 1};
3+
4+
class Solution {
5+
public:
6+
int n, m;
7+
void bfs(int x0, int y0, vector<vector<char>>& board, vector<vector<int>>& vis){
8+
queue<pair<int, int>> q;
9+
vis[x0][y0] = 1;
10+
q.push(make_pair(x0, y0));
11+
while(!q.empty()){
12+
int x = q.front().first, y = q.front().second;
13+
q.pop();
14+
for (int i = 0; i < 4; i++){
15+
int xx = x + dx[i];
16+
int yy = y + dy[i];
17+
if (xx >= 0 and xx < n and yy >= 0 and yy < m and board[xx][yy] == 'O' and vis[xx][yy] == 0){
18+
vis[xx][yy] = 1;
19+
q.push(make_pair(xx,yy));
20+
}
21+
}
22+
23+
}
24+
}
25+
26+
void solve(vector<vector<char>>& board) {
27+
n = board.size();
28+
if (n == 0) return;
29+
m = board[0].size();
30+
vector<vector<int>> vis(n, vector<int>(m, 0));
31+
for (int i = 0; i < n; i++) {
32+
if (board[i][0] == 'O' and vis[i][0] == 0)
33+
bfs(i, 0, board, vis);
34+
if (board[i][m-1] == 'O' and vis[i][m-1] == 0)
35+
bfs(i, m-1, board, vis);
36+
}
37+
for (int i = 0; i < m; i++) {
38+
if (board[0][i] == 'O' and vis[0][i] == 0)
39+
bfs(0, i, board, vis);
40+
if (board[n-1][i] == 'O' and vis[n-1][i] == 0)
41+
bfs(n-1, i, board, vis);
42+
}
43+
for (int i = 0; i < n; i++)
44+
for (int j = 0; j < m; j++)
45+
if (board[i][j] == 'O' and vis[i][j] == 0) board[i][j] = 'X';
46+
47+
}
48+
};

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.