leetcode-695. Max Area of Island

题目: https://leetcode.com/problems/max-area-of-island/description/

int maxAreaOfIsland(vector<vector<int>>& grid) {
        int max=0;
        if(grid.size()==0) return 0;
        for(int j=0;j<grid.size();j++)
        {
            for(int i=0;i<grid[0].size();i++)
            {
                if(grid[j][i]==1)
                {
                    max=std::max(DF(grid,j,i),max);
                }
            }
        }
        return max;
    }
    int DF(vector<vector<int>>& grid,int i,int j)
    {
        //递归一定要存在终止条件,否则会永远的递归下去
        if(i<0||i>=grid.size()||j<0||j>=grid[0].size()||grid[i][j]==0)
            return 0;
        int sum=1;
        grid[i][j]=0;
        sum+=DF(grid,i+1,j)+DF(grid,i-1,j)+DF(grid,i,j+1)+DF(grid,i,j-1);
        return sum;
    }

 

posted @ 2018-08-16 21:29  小雪SS  阅读(145)  评论(0编辑  收藏  举报