Given a 2D board containing 'X'
and 'O'
, capture all regions surrounded by 'X'
.
A region is captured by flipping all 'O'
s into 'X'
s in that surrounded region.
For example,
X X X X X O O X X X O X X O X X
After running your function, the board should be:
X X X X X X X X X X X X X O X X
class Solution {
public:
void solve(vector<vector<char>> &board) {
int m = board.size();
if(m <= 2) {
return;
}
int n = board[0].size();
stack<pair<int, int> > st;
for(int i = 0; i < m; ++i) {
for(int j = 0; j < n; ++j) {
if(board[i][j] == 'O' &&(i == 0 || i == m - 1 || j == 0 || j == n - 1)) {
board[i][j] = 'N';
st.push(make_pair(i, j));
}
}
}
const int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
while(!st.empty()) {
pair<int, int> cur = st.top();
st.pop();
for(int i = 0; i < 4; ++i) {
int r = cur.first + dir[i][0];
int c = cur.second + dir[i][1];
if(r >= 0 && r < m && c >= 0 && c < n && board[r][c] == 'O') {
board[r][c] = 'N';
st.push(make_pair(r, c));
}
}
}
for(int i = 0; i < m; ++i) {
for(int j = 0; j < n; ++j) {
if(board[i][j] == 'N') {
board[i][j] = 'O';
} else if(board[i][j] == 'O') {
board[i][j] = 'X';
}
}
}
}
};