首页 > 其他 > 详细

水域大小

时间:2021-09-07 15:53:11      阅读:20      评论:0      收藏:0      [点我收藏+]

技术分享图片

 

 

class Solution {
public:
    vector<int> res;
    vector<int> pondSizes(vector<vector<int>>& land) {
        vector<vector<bool>> visit(land.size(),vector<bool>(land[0].size(),false));
        for(int i=0;i<land.size();i++)
        {
            for(int j=0;j<land[i].size();j++)
            {
                if(!visit[i][j] && land[i][j]==0)
                {
                    int count=0;
                    DFS(land,visit,i,j,count);
                    res.push_back(count);
                }
            }
        }
        sort(res.begin(),res.end());
        return res;
    }
    void DFS(vector<vector<int>>&land,vector<vector<bool>>&visit,int i,int j,int&  count)
    {
        visit[i][j]=true;
        count+=1;
        if(j-1>=0 && land[i][j-1]==0 && visit[i][j-1]==false)
        {
            DFS(land,visit,i,j-1,count);
        }
        if(j+1<land[0].size() && land[i][j+1]==0 && visit[i][j+1]==false)
        {
            DFS(land,visit,i,j+1,count);
        }
        if(i-1>=0 && land[i-1][j]==0 && visit[i-1][j]==false)
        {
            DFS(land,visit,i-1,j,count);
        }
        if(i+1<land.size() && land[i+1][j]==0 && visit[i+1][j]==false)
        {
            DFS(land,visit,i+1,j,count);
        }
        if(j-1>=0 && i-1>=0 && land[i-1][j-1]==0 && visit[i-1][j-1]==false)
        {
            DFS(land,visit,i-1,j-1,count);
        }
        if(j+1<land[0].size() && i-1>=0 && land[i-1][j+1]==0 && visit[i-1][j+1]==false)
        {
            DFS(land,visit,i-1,j+1,count);
        }
        if(j-1>=0 && i+1<land.size() && land[i+1][j-1]==0 && visit[i+1][j-1]==false)
        {
            DFS(land,visit,i+1,j-1,count);
        }
        if(j+1<land[0].size() && i+1<land.size() && land[i+1][j+1]==0 && visit[i+1][j+1]==false)
        {
            DFS(land,visit,i+1,j+1,count);
        }
    }
};

 

水域大小

原文:https://www.cnblogs.com/libin123/p/15237689.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!