Given a 2D board containing
A region is captured by flipping all
For example,
'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 XAnalysis:
Rather than recording the 2D positions for any scanned 'O', a trick is to substitute any border 'O's with another character - here in the Code I use 'Y'. And scan the board again to change any rest 'O's to 'X's, and change 'Y's back to 'O's.
We start searching 'O' from the four borders. I tried DFS first, the OJ gives Runtime error on the 250x250 large board; In the Sol 2 below, I implement BFS instead, and passed all tests.
The time complexity is O(n^2), as in the worst case, we may need to scan the entire board.
Code:
1, DFS
class Solution {
public:
// dfs - Runtime error on large board 250x250
void dfs(vector<vector<char>> &board, int r, int c) {
if (r<0||r>board.size()-1||c<0||c>board[0].size()-1||board[r][c]!='O')
return;
board[r][c]='Y';
dfs(board, r-1, c);
dfs(board, r+1, c);
dfs(board, r, c-1);
dfs(board, r, c+1);
}
void solve(vector<vector<char>> &board) {
if (board.empty() || board.size()<3 || board[0].size()<3)
return;
int r=board.size();
int c=board[0].size();
// dfs from boundary to inside
for (int i=0; i<c; i++) {
if (board[0][i]=='O')
dfs(board, 0, i); // first row
if (board[c-1][i]=='O')
dfs(board, c-1, i); // last row
}
for (int i=0; i<board.size(); i++) {
if (board[i][0]=='O')
dfs(board, i, 0); // first col
if (board[i][c-1])
dfs(board, i, c-1); // last col
}
// scan entire matrix and set values
for (int i=0; i<board.size(); i++) {
for (int j=0; j<board[0].size(); j++) {
if (board[i][j]=='O')
board[i][j]='X';
else if (board[i][j]=='Y')
board[i][j]='O';
}
}
}
};
2, BFS
class Solution {
public:
void solve(vector<vector<char>> &board) {
if (board.empty() || board.size()<3 || board[0].size()<3)
return;
int r=board.size();
int c=board[0].size();
// queues to store row and col indices
queue<int> qr;
queue<int> qc;
// start from boundary
for (int i=0; i<c; i++) {
if (board[0][i]=='O') { qr.push(0); qc.push(i); }
if (board[r-1][i]=='O') { qr.push(r-1); qc.push(i); }
}
for (int i=0; i<r; i++) {
if (board[i][0]=='O') { qr.push(i); qc.push(0); }
if (board[i][c-1]=='O') { qr.push(i); qc.push(c-1); }
}
// BFS
while (!qr.empty()) {
int rt=qr.front(); qr.pop();
int ct=qc.front(); qc.pop();
board[rt][ct]='Y';
if (rt-1>=0 && board[rt-1][ct]=='O') { qr.push(rt-1); qc.push(ct); } // go left
if (rt+1<r && board[rt+1][ct]=='O') { qr.push(rt+1); qc.push(ct); } // go right
if (ct-1>=0 && board[rt][ct-1]=='O') { qr.push(rt); qc.push(ct-1); } // go up
if (ct+1<c && board[rt][ct+1]=='O') { qr.push(rt); qc.push(ct+1); } // go down
}
// scan entire matrix and set values
for (int i=0; i<board.size(); i++) {
for (int j=0; j<board[0].size(); j++) {
if (board[i][j]=='O') board[i][j]='X';
else if (board[i][j]=='Y') board[i][j]='O';
}
}
}
};
No comments:
Post a Comment