class Solution {
public:
int dfs(vector<vector<int>>& grid,int i,int j)
{
if(i<0||i==grid.size()||j<0||j==grid[i].size())
{
return 1;
}
if(grid[i][j]==0)
{
return 1;
}
if(grid[i][j]==-1)
{
return 0;
}
if(grid[i][j]==1)
{
grid[i][j]=-1;
return dfs(grid, i-1, j)+dfs(grid, i+1, j)+dfs(grid, i, j-1)+dfs(grid, i, j+1);
}
return 0;
}
int islandPerimeter(vector<vector<int>>& grid) {
for(int i=0;i<grid.size();i++)
{
for(int j=0;j<grid[i].size();j++)
{
if(grid[i][j]==1)
{
return dfs(grid, i, j);
}
}
}
return 0;
}
};
leetcode Island Perimeter
最新推荐文章于 2021-03-04 06:20:03 发布