程序员面试金典 16.19

Pond Sizes:统计一片矩形区域中连通的池塘数目。

只需要遍历每一个点,深搜统计连通的池塘大小,最后排序返回即可。

class Solution {
public:
    vector<int> pondSizes(vector<vector<int>>& land) {
        vector<int> sizes;
        Visited.assign(land.size(), vector<bool>(land[0].size(), false));
        for(size_t r = 0; r < land.size(); r++)
        {
            for(size_t c = 0; c < land[r].size(); c++)
            {
                if(land[r][c] == 0 && !Visited[r][c]){
                    int size = ComputeSize(land, static_cast<int>(r), static_cast<int>(c));
                    sizes.push_back(size);
                }
            }
        }
        sort(sizes.begin(), sizes.end());
        return sizes;
    }
private:
    vector<vector<bool>> Visited;
    int ComputeSize(const vector<vector<int>> &land, const int r, const int c)
    {
        if(r < 0 || r >= land.size() || c < 0 || c >= land[r].size()) return 0;
        if(land[r][c] != 0 || Visited[r][c]) return 0;
        int size = 1;
        Visited[r][c] = true;
        for(int dr = -1; dr <= 1; dr++)
        {
            for(int dc = -1; dc <= 1; dc++)
            {
                size += ComputeSize(land, r + dr, c + dc);
            }
        }
        return size;
    }
};

你可能感兴趣的:(《程序员面试金典》)