Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- int row, col;
- public:
- int closedIsland(vector<vector<int>>& grid) {
- row = grid.size();
- col = grid[0].size();
- for(int i=0; i<row; i++)
- for(int j=0; j<col; j++)
- if((i*j == 0 || i == row-1 || j == col-1) && grid[i][j] == 0)
- dfs(i, j, grid);
- int components = 0;
- for(int i=0; i<row; i++)
- for(int j=0; j<col; j++)
- if(grid[i][j] == 0)
- components++, dfs(i, j, grid);
- return components;
- }
- void dfs(int r, int c, vector<vector<int>> &grid){
- if(r<0 || r >= row || c<0 || c>=col || grid[r][c] == 1) return;
- grid[r][c] = 1;
- dfs(r+1, c, grid);
- dfs(r-1, c, grid);
- dfs(r, c+1, grid);
- dfs(r, c-1, grid);
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement