Surrounded Regions
Given a 2D board containing ‘X’ and ‘O’ (the letter O), capture all regions surrounded by ‘X’.
A region is captured by flipping all 'O’s into 'X’s in that surrounded region.
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
Explanation:
Surrounded regions shouldn’t be on the border, which means that any ‘O’ on the border of the board are not flipped to ‘X’. Any ‘O’ that is not on the border and it is not connected to an ‘O’ on the border will be flipped to ‘X’. Two cells are connected if they are adjacent cells connected horizontally or vertically.
解法1:BFS
找到边界上的O,然后使用一个queue进行宽搜,找出所有的连接边界O的点。
自己的标记方法:visited表示是否访问,connetO表示是否连接O;
小chick:找到之后将O变为$,然后将所有的O变成X,再将所有的 $变为O。
class Solution {
public:
void solve(vector<vector<char>>& board) {
if(board.empty() || board[0].empty())
return ;
int row = board.size(), col = board[0].size();
vector<vector<bool>> visited(row, vector<bool> (col, 0));
vector<vector<bool>> connectO(row, vector<bool> (col, 0));
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
if((i==0 || j==0 || i==row-1 || j==col-1) && board[i][j] == 'O' && visited[i][j] == 0){
queue<int> q;
q.push(i*col+j);
while(!q.empty()){
int t = q.front(), x = t / col, y = t % col; q.pop();
cout<<x<<" "<<y<<endl;
connectO[x][y] = 1;
visited[x][y] = 1;
if(x-1 >= 0 && visited[x-1][y]==0 && board[x-1][y]=='O'){
q.push(t-col);
visited[x-1][y] = 1;
}
if(y+1 <col && visited[x][y+1]==0 && board[x][y+1]=='O'){
q.push(t+1);
visited[x][y+1] = 1;
}
if(x+1 <row && visited[x+1][y]==0 && board[x+1][y]=='O'){
q.push(t+col);
visited[x+1][y] = 1;
}
if(y-1 >= 0 && visited[x][y-1]==0 && board[x][y-1]=='O'){
q.push(t-1);
visited[x][y-1] = 1;
}
}
}
}
}
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
if(connectO[i][j] == 0)
board[i][j] = 'X';
}
}
}
};
class Solution {
public:
void solve(vector<vector<char>>& board) {
if(board.empty() || board[0].empty())
return ;
int row = board.size(), col = board[0].size();
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
if(i!=0 && j!=0 && i!=row-1 && j!=col-1)
continue;
if (board[i][j] != 'O') continue;
board[i][j] = '$';
queue<int> q;
q.push(i*col+j);
while(!q.empty()){
int t = q.front(), x = t / col, y = t % col; q.pop();
if(x-1 >= 0 && board[x-1][y]=='O'){
q.push(t-col);
board[x-1][y] = '$';
}
if(y+1 <col && board[x][y+1]=='O'){
q.push(t+1);
board[x][y+1] = '$';
}
if(x+1 <row && board[x+1][y]=='O'){
q.push(t+col);
board[x+1][y] = '$';
}
if(y-1 >= 0 && board[x][y-1]=='O'){
q.push(t-1);
board[x][y-1] = '$';
}
}
}
}
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
if(board[i][j] == 'O') board[i][j] = 'X';
if(board[i][j] == '$') board[i][j] = 'O';
}
}
}
};
解法2:DFS
class Solution {
public:
void solve(vector<vector<char>>& board) {
if(board.empty() || board[0].empty())
return ;
int row = board.size(), col = board[0].size();
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
if(i!=0 && j!=0 && i!=row-1 && j!=col-1)
continue;
if (board[i][j] != 'O') continue;
solveDFS(board, i, j);
}
}
for (int i = 0; i < row; ++i) {
for (int j = 0; j < col; ++j) {
if (board[i][j] == 'O') board[i][j] = 'X';
if (board[i][j] == '$') board[i][j] = 'O';
}
}
}
void solveDFS(vector<vector<char>>& board, int i, int j){
board[i][j] = '$';
if (i > 0 && board[i - 1][j] == 'O')
solveDFS(board, i - 1, j);
if (j < board[i].size() - 1 && board[i][j + 1] == 'O')
solveDFS(board, i, j + 1);
if (i < board.size() - 1 && board[i + 1][j] == 'O')
solveDFS(board, i + 1, j);
if (j > 1 && board[i][j - 1] == 'O')
solveDFS(board, i, j - 1);
}
};
参考
https://www.cnblogs.com/grandyang/p/4555831.html