# 题目：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

# 思路：

DFS写在代码里面，但是会出现栈溢出。

BFS似乎不会，我不知道为啥，只是能够成功。

# 代码：

class Solution {
public:
/*
void solve(vector<vector<char>>& board) {
if(board.empty()||board[0].empty()) return;
int i=0,j=0;
int m=board.size(),n=board[0].size();

for(;j<n-1;j++){
walk(board,i,j);
}

for(;i<m-1;i++){        //right
walk(board,i,j);
}

for(;j>0;j--){          //down
walk(board,i,j);
}

for(;i>0;i--){//left
walk(board,i,j);
}

for(i=0;i<m;i++){
for(j=0;j<n;j++){
if(board[i][j]=='#') board[i][j]='O';
else    board[i][j]='X';
}
}
}

void walk(vector<vector<char> >&board,int i,int j){
//DFS
if(i<0||i>=board.size()||j<0||j>=board[0].size())   return;
if(board[i][j]=='O'){
board[i][j]='#';
walk(board,i-1,j);
walk(board,i+1,j);
walk(board,i,j-1);
walk(board,i,j+1);
}
}*/

void bfs(int i, int j, vector<vector<char>>& board) {
int n = board.size();
int m = board[0].size();
queue<pair<int, int>> q;
q.push(make_pair(i, j));
while (!q.empty()) {
auto top = q.front();
q.pop();
if (top.first >= 0 && top.first < n && top.second >= 0 && top.second < m && board[top.first][top.second] == 'O') {
//如果没有这句话，那么你接下来的make_pair就会出错，溢出.
board[top.first][top.second] = '#';
q.push(make_pair(top.first - 1, top.second));
q.push(make_pair(top.first + 1, top.second));
q.push(make_pair(top.first, top.second + 1));
q.push(make_pair(top.first, top.second - 1));
}
}
}

void solve(vector<vector<char>>& board) {
if(board.empty()||board[0].empty()) return;
int m=board.size(),n=board[0].size();

for(int i=0;i<m;i++){
//最左边与最右边
if(board[i][0]=='O')    walk( board,i,0);
if(board[i][n-1]=='O')  walk( board,i,n-1);
}

for(int i=1;i<n-1;i++){
//最上边与最下边
if(board[0][i]=='O')    walk( board,0,i);
if(board[m-1][i]=='O')  walk( board,m-1,i);
}

for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(board[i][j]=='#')    board[i][j]='O';
else    board[i][j]='X';
}
}
}

void walk(vector<vector<char>>& board,int i,int j){
int m=board.size(),n=board[0].size();
queue<pair<int,int>>q;
q.push(make_pair(i,j));
while(!q.empty()){
auto tem=q.front();
q.pop();
if(tem.first>=0&&tem.first<m&&tem.second>=0&&tem.second<=n&&board[tem.first][tem.second]=='O'){
board[tem.first][tem.second]='#';
q.push(make_pair(tem.first-1,tem.second));
q.push(make_pair(tem.first+1,tem.second));
q.push(make_pair(tem.first,tem.second-1));
q.push(make_pair(tem.first,tem.second+1));
}
}
}
};


#### (LeetCode 130)Surrounded Regions(并查集)

2016-04-20 22:07:34

#### LeetCode 130 Surrounded Regions（并查集）

2017-04-28 22:44:59

#### (LeetCode 130) Surrounded Regions(BFS)

2016-04-20 16:45:54

#### LeetCode-130. Surrounded Regions (JAVA)（环绕区域）

2017-04-17 12:16:17

#### leetcode_question_130 Surrounded Regions

2013-09-19 23:54:37

#### Leetcode Surrounded Regions 解题报告

2014-02-15 20:57:18

#### Surrounded Regions -- LeetCode

2014-04-04 01:12:07

#### leetcode 130. Surrounded Regions 典型的深度优先遍历DFS + 矩阵遍历

2017-09-16 13:07:17

#### LeetCode 130. Surrounded Regions (并查集)

2017-04-29 23:05:40