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


• 本文已收录于以下专栏：

## Leetcote 130. Surrounded Regions

130. Surrounded Regions 题目要求： 在一个二维平面上有至少被'0'或者'X'填满，要求当‘O’被'X包围时：除非'O'就在边界或者'O'临近的'O'也在边界，否则将被‘P’代替...

## Surrounded Regions

leetcode 经典搜索题目 Surrounded Regions ，深度优先搜索和广度优先搜索分别实现Surrounded Regions 。

## Surrounded Regions

class Solution { public: void solve(vector> &board) { if(board.size()<1) return; ...

## surrounded regions

surrounded regions 递归的方法，最周围的 private: int lenx; int leny; public: void solve(vector> &board...

## Surrounded Regions

1 题目描述 Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is...

## Surrounded Regions

Q: Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A regio...

## Surrounded Regions

Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captu...

举报原因： 您举报文章：深度学习：神经网络中的前向传播和反向传播算法推导 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)