前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >LeetCode 水域大小(DFS)(BFS)

LeetCode 水域大小(DFS)(BFS)

作者头像
SakuraTears
发布2022-01-13 11:39:28
2530
发布2022-01-13 11:39:28
举报
文章被收录于专栏:从零开始的Code生活

DFS BFS例题

来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/pond-sizes-lcci

你有一个用于表示一片土地的整数矩阵land,该矩阵中每个点的值代表对应地点的海拔高度。若值为0则表示水域。由垂直、水平或对角连接的水域为池塘。池塘的大小是指相连接的水域的个数。编写一个方法来计算矩阵中所有池塘的大小,返回值需要从小到大排序。

示例:

输入: [ [0,2,1,0], [0,1,0,1], [1,1,0,1], [0,1,0,1] ] 输出: [1,2,4] 提示:

0 < len(land) <= 1000 0 < len(land[i]) <= 1000

此题DFS,BFS都可以用。 (代码格式为leetcode模板格式)

代码:

BFS
代码语言:javascript
复制
class Solution {
public:
    int n = 0;
    vector<vector<int>> dirs = {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}};//可以斜着所以是8个方向
    struct Node {int x; int y;};
    queue<Node> q;
    int BFS(int i, int j, vector<vector<int>>& land)
    {
        n = 1;
        Node start, node;
        start.x = i;
        start.y = j;
        land[i][j] = 1;
        q.push(start);
        while (!q.empty()) {
            start = q.front();
            q.pop();
            for (int i = 0; i < 8; i++)
            {
                node.x = start.x + dirs[i][0];
                node.y = start.y + dirs[i][1];
                if (node.x >= 0 && node.x < land.size() && node.y >= 0 && node.y < land[0].size() && land[node.x][node.y] == 0)//判断是否为鱼塘
                {
                    q.push(node);
                    land[node.x][node.y] = 1;//染色
                    n++;
                }
            }
        }
        return n;
    }

    vector<int> pondSizes(vector<vector<int>>& land) {
        vector<int> arr;
        for (int i = 0; i < land.size(); i++)
        {
            for (int j = 0; j < land[i].size(); j++)
            {
                if (land[i][j] == 0)
                {
                    arr.push_back(BFS(i, j, land));
                }
                else 
                {
                    continue;
                }
            }
        }
        sort(arr.begin(), arr.end());
        return arr;
    }
};
DFS
代码语言:javascript
复制
class Solution {
public:
    vector<vector<int>> dirs = {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}};
    struct Node {int x; int y;};
    
    int DFS(int i, int j, vector<vector<int>>& land)
    {
        if (i < 0 || i >= land.size() || j < 0 || j >= land[0].size()) return 0;
        if (land[i][j] != 0) return 0;//判断是否为鱼塘
        land[i][j] = 1;//染色
        Node next;
        int n = 1;
        for (int m = 0; m < 8; m++)
        {
            next.x = i + dirs[m][0];
            next.y = j + dirs[m][1];
            n += DFS(next.x, next.y, land);
        }
        return n;
    }

    vector<int> pondSizes(vector<vector<int>>& land) {
        vector<int> arr;
        for (int i = 0; i < land.size(); i++)
        {
            for (int j = 0; j < land[i].size(); j++)
            {
                if (land[i][j] == 0)
                {
                    arr.push_back(DFS(i, j, land));
                }
                else 
                {
                    continue;
                }
            }
        }
        sort(arr.begin(), arr.end());
        return arr;
    }
};
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020年09月09日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • BFS
  • DFS
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档