Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 130_Surrounded_Regions.cpp #147

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions C++/130_Surrounded_Regions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
//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

Tag: Breadth-first Search, Union Find

Author: Xinyu Liu
*/
#include <vector>
using namespace std;

class Solution {
public:
void solve(vector<vector<char>>& board) {
int i,j;
int row = board.size();
if(!row)
return;
int col = board[0].size();

for(i = 0; i < row; i++){
nearcheck(board, i, 0, row,col);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

space before col

if(col > 1)
nearcheck(board, i, col - 1, row, col);
}
for(j = 1; j + 1 < col; j++){
nearcheck(board, 0, j, row, col);
if(row > 1)
nearcheck(board, row - 1, j, row, col);
}
for(i = 0; i < row; i++)
for(j = 0; j < col; j++)
if(board[i][j] == 'O')
board[i][j] = 'X';
for(i = 0; i < row; i++)
for(j = 0; j < col; j++)
if(board[i][j] == '1')
board[i][j] = 'O';
}
void nearcheck(vector<vector<char> >&vec, int i, int j, int row, int col){
if(vec[i][j] == 'O'){
vec[i][j] = '1';
if(i > 1)
nearcheck(vec, i - 1, j, row, col);
if(j > 1)
nearcheck(vec, i, j - 1, row, col);
if(i < row - 1)
nearcheck(vec, i + 1, j, row, col);
if(j < col - 1)
nearcheck(vec, i, j + 1, row, col);
}
}
};

void main(){
char c0[] = {'X','X','X','X'};
vector<char> v0 (begin(c0), end(c0));
char c1[] = {'X','O','O','X'};
vector<char> v1 (begin(c1), end(c1));
char c2[] = {'X','X','O','X'};
vector<char> v2 (begin(c2), end(c2));
char c3[] = {'X','X','X','X'};
vector<char> v3 (begin(c3), end(c3));
vector<vector<char>> v;
v.push_back(v0);
v.push_back(v1);
v.push_back(v2);
v.push_back(v3);
Solution sol;
sol.solve(v);
}