Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Logic:This is proper DFS problem.
- observation:Only that bulk of O's which is on the corner will be remained O.
- so we run dfs for every corner zero and change it to small o that means it will remain
- We are changing it into small o so that we can differentiate between which O's to be
- O and which to be changed.
- O after all operations.
- rest all O will be captured.
- */
- void dfs(vector<vector<char>> &board,int i,int j,int r,int c){
- int row[4]={-1,0,1,0};
- int col[4]={0,1,0,-1};
- board[i][j]='o';
- for(int k=0;k<4;k++){
- int new_i=i+row[k];
- int new_j=j+col[k];
- if(new_i>=0 && new_i<r && new_j>=0 && new_j<c && board[new_i][new_j]=='O') dfs(board,new_i,new_j,r,c);
- }
- }
- class Solution {
- public:
- void solve(vector<vector<char>>& board) {
- int r=board.size();
- int c=board[0].size();
- for(int i=0;i<r;i++){
- for(int j=0;j<c;j++){
- if((i==0 || i==r-1 || j==0 || j==c-1 ) && board[i][j]=='O')dfs(board,i,j,r,c);
- }
- }
- for(int i=0;i<r;i++){
- for(int j=0;j<c;j++){
- if(board[i][j]=='o') board[i][j]='O';
- else board[i][j]='X';
- }
- }
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement