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