Advertisement
nikunjsoni

130

May 1st, 2021
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.90 KB | None | 0 0
  1. class Solution {
  2. int rows, cols;
  3. public:
  4.     void solve(vector<vector<char>>& board) {
  5.         rows = board.size();
  6.         cols = board[0].size();
  7.        
  8.         for(int i=0; i<rows; i++)
  9.             for(int j=0; j<cols; j++)
  10.                 if(i*j == 0 || i == rows-1 || j == cols-1)
  11.                     dfs(i, j, board, '1');
  12.        
  13.         for(int i=0; i<rows; i++)
  14.             for(int j=0; j<cols; j++)
  15.                 if(board[i][j] == 'O')
  16.                     board[i][j] = 'X';
  17.                 else if(board[i][j] == '1')
  18.                     board[i][j] = 'O';
  19.     }
  20.    
  21.     void dfs(int x, int y, vector<vector<char>>& board, char c){
  22.         if(x < 0 || x >= rows || y < 0 || y >= cols || board[x][y] != 'O') return;
  23.         board[x][y] = c;
  24.         dfs(x+1, y, board, c);
  25.         dfs(x, y+1, board, c);
  26.         dfs(x-1, y, board, c);
  27.         dfs(x, y-1, board, c);
  28.     }
  29. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement