Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- int rows, cols;
- public:
- void solve(vector<vector<char>>& board) {
- rows = board.size();
- cols = board[0].size();
- for(int i=0; i<rows; i++)
- for(int j=0; j<cols; j++)
- if(i*j == 0 || i == rows-1 || j == cols-1)
- dfs(i, j, board, '1');
- for(int i=0; i<rows; i++)
- for(int j=0; j<cols; j++)
- if(board[i][j] == 'O')
- board[i][j] = 'X';
- else if(board[i][j] == '1')
- board[i][j] = 'O';
- }
- void dfs(int x, int y, vector<vector<char>>& board, char c){
- if(x < 0 || x >= rows || y < 0 || y >= cols || board[x][y] != 'O') return;
- board[x][y] = c;
- dfs(x+1, y, board, c);
- dfs(x, y+1, board, c);
- dfs(x-1, y, board, c);
- dfs(x, y-1, board, c);
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement