-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
feecdf2
commit ca2c2e5
Showing
1 changed file
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
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 | ||
*/ | ||
class Solution { | ||
public: | ||
void check(vector<vector<char>>& board,int i,int j){ | ||
int m=board.size(); | ||
int n=board[0].size(); | ||
if(i<0 || j<0 || i>=m || j>=n) return; | ||
if(board[i][j]=='O') | ||
board[i][j]='*'; | ||
else return; | ||
check(board,i+1,j); | ||
check(board,i-1,j); | ||
check(board,i,j+1); | ||
check(board,i,j-1); | ||
} | ||
void solve(vector<vector<char>>& board) { | ||
int m=board.size(); | ||
if(m==0) return; | ||
int n=board[0].size(); | ||
for(int i=0;i<m;i++){ | ||
if(board[i][0]=='O') | ||
check(board,i,0); | ||
if(board[i][n-1]=='O') | ||
check(board,i,n-1); | ||
} | ||
for(int i=0;i<n;i++){ | ||
if(board[0][i]=='O') | ||
check(board,0,i); | ||
if(board[m-1][i]=='O') | ||
check(board,m-1,i); | ||
} | ||
for(int i=0;i<m;i++){ | ||
for(int j=0;j<n;j++){ | ||
if(board[i][j]=='O') | ||
board[i][j]='X'; | ||
else if(board[i][j]=='*') | ||
board[i][j]='O'; | ||
} | ||
} | ||
} | ||
}; |