class Solution {
public:
int maxAreaOfIsland(vector<vector<int>>& grid)
{
int max_area=0,area=0;
for(int i=0;i<grid.size();i++)
{
for(int j=0;j<grid[i].size();j++)
{
if(grid[i][j]==1)
{
area=calAreaOfIsland(grid,i,j,0);
}
max_area=(max_area>area?max_area:area);
}
}
return max_area;
}
int calAreaOfIsland(vector<vector<int>>& grid, int x, int y, int area)
{
area+=1;
grid[x][y]=-1; //标记为已探索过岛屿
if(x>0&&grid[x-1][y]==1)area=calAreaOfIsland(grid,x-1,y,area);
if(x<grid.size()-1&&grid[x+1][y]==1)area=calAreaOfIsland(grid,x+1,y,area);
if(y>0&&grid[x][y-1]==1)area=calAreaOfIsland(grid,x,y-1,area);
if(y<grid[x].size()-1&&grid[x][y+1]==1)area=calAreaOfIsland(grid,x,y+1,area);
return area;
}
};
07-28
385
09-08
175
12-05
1667
11-16