###### [leetcode] 130.Surrounded Regions

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 row = board.size();
if(row == 0)return;
int col = board[0].size();
if(col == 0)return;
queue<pair<int,int>> OIndex;
for(auto i = 0; i < col; i++) {
if(board[0][i] == 'O') {
OIndex.push(make_pair(0, i));
board[0][i] = '+';
}
if(board[row - 1][i] == 'O') {
OIndex.push(make_pair(row-1, i));
board[row - 1][i] = '+';
}
}

for(auto i = 0; i < row; i++) {
if(board[i][0] == 'O') {
OIndex.push(make_pair(i, 0));
board[i][0] = '+';
}
if(board[i][col - 1] == 'O') {
OIndex.push(make_pair(i, col - 1));
board[i][col - 1] = '+';
}
}

while(!OIndex.empty()) {
pair<int,int> index = OIndex.front();
OIndex.pop();
if(index.first != 0 && board[index.first - 1][index.second] == 'O') {
board[index.first - 1][index.second] = '+';
OIndex.push(make_pair(index.first - 1, index.second));
}
if(index.first != row - 1 && board[index.first + 1][index.second] == 'O') {
board[index.first + 1][index.second] = '+';
OIndex.push(make_pair(index.first + 1, index.second));
}
if(index.second != 0 && board[index.first][index.second - 1] == 'O') {
board[index.first][index.second - 1] = '+';
OIndex.push(make_pair(index.first, index.second - 1));
}
if(index.second != col - 1 && board[index.first][index.second + 1] == 'O') {
board[index.first][index.second + 1] = '+';
OIndex.push(make_pair(index.first, index.second + 1));
}
}
for(auto i = 0; i < row; i++)
for(auto j = 0; j < col; j++){
if(board[i][j] == 'O')board[i][j] = 'X';
else if(board[i][j] == '+')board[i][j] = 'O';
}
}
};

#### [LeetCode]130.Surrounded Regions

2015-02-06 22:28:37

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

2016-04-20 22:07:34

#### Surrounded Regions -- LeetCode

2014-04-04 01:12:07

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

2016-04-20 16:45:54

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

2017-04-17 12:16:17

#### Leetcode Surrounded Regions 解题报告

2014-02-15 20:57:18

#### leetcode surround regions

2015-03-22 17:11:05

#### LeetCode（130） Surrounded Regions

2016-08-15 22:04:38

#### LeetCode | Surrounded Regions（包围的区域）

2014-08-20 01:06:00

#### 利用BFS和DFS解决 LeetCode 130: Surrounded Regions

2017-07-30 12:23:47

## 不良信息举报

[leetcode] 130.Surrounded Regions