Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- int row, col;
- public:
- int numIslands(vector<vector<char>>& grid) {
- row = grid.size();
- col = grid[0].size();
- int components = 0;
- for(int i=0; i<row; i++)
- for(int j=0; j<col; j++)
- if(grid[i][j] == '1')
- components++, dfs(i, j, grid);
- return components;
- }
- void dfs(int r, int c, vector<vector<char>> &grid){
- if(r<0 || r >= row || c<0 || c>=col || grid[r][c] == '0') return;
- grid[r][c] = '0';
- 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