图论第一天|797.所有可能的路径 200. 岛屿数量

目录

Leetcode797.所有可能的路径

文章链接:代码随想录

题目链接:797.所有可能的路径

思路:深搜入门,注意邻接表和邻接矩阵的形式

cpp 复制代码
class Solution {
public:
    vector<vector<int>> result;
    vector<int> path;
    
    void dfs(vector<vector<int>>& graph, int x){
        if (x == graph.size() - 1){
            result.push_back(path);
            return ;
        }

        for (int i = 0; i < graph[x].size(); i++){
            path.push_back(graph[x][i]);
            dfs(graph, graph[x][i]);
            path.pop_back();
        }
    }
    
    vector<vector<int>> allPathsSourceTarget(vector<vector<int>>& graph) {
        path.push_back(0);
        dfs(graph, 0);
        return result;
    }
};

Leetcode200. 岛屿数量

文章链接:代码随想录

题目链接:200. 岛屿数量

思路:深搜法

cpp 复制代码
class Solution {
public:
    int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};
    void dfs(vector<vector<char>>& grid, vector<vector<bool>>& visited, int x, int y){
        for (int i = 0; i < 4; i++){
            int nex = x + dir[i][0];
            int ney = y + dir[i][1];
            if (nex < 0 || nex >= grid.size() || ney < 0 || ney >= grid[0].size()) continue ;
            if (!visited[nex][ney] && grid[nex][ney] == '1'){
                visited[nex][ney] = true;
                dfs(grid, visited, nex, ney);
            }
        }
    }
    
    
    int numIslands(vector<vector<char>>& grid) {
        int result = 0;
        vector<vector<bool>> visited(grid.size(), vector<bool>(grid[0].size(), 0));
        for (int i = 0; i < grid.size(); i++){
            for (int j = 0; j < grid[0].size(); j++){
                if (!visited[i][j] && grid[i][j] == '1'){
                    result++;
                    visited[i][j] = true;
                    dfs(grid, visited, i, j);
                }
            }
        }
        return result;
    }
};

广搜法,用队列存储,遍序搜寻,替代深搜回溯

cpp 复制代码
class Solution {
public:
    int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};
    void bfs(vector<vector<char>>& grid, vector<vector<bool>>& visited, int x, int y){
        queue<pair<int, int>> que;
        que.push({x, y});
        visited[x][y] = true;

        while(!que.empty()){
            pair<int, int> cur = que.front();
            que.pop();
            for (int i = 0; i < 4; i++){
            int nex = cur.first + dir[i][0];
            int ney = cur.second + dir[i][1];
            if (nex < 0 || nex >= grid.size() || ney < 0 || ney >= grid[0].size()) continue ;
            if (!visited[nex][ney] && grid[nex][ney] == '1'){
                que.push({nex, ney});
                visited[nex][ney] = true;
            }
        }
        }
        
        
    }
    
    
    int numIslands(vector<vector<char>>& grid) {
        int result = 0;
        vector<vector<bool>> visited(grid.size(), vector<bool>(grid[0].size(), 0));
        for (int i = 0; i < grid.size(); i++){
            for (int j = 0; j < grid[0].size(); j++){
                if (!visited[i][j] && grid[i][j] == '1'){
                    result++;
                    
                    bfs(grid, visited, i, j);
                }
            }
        }
        return result;
    }
};

图论第一天打卡,加油!!!

相关推荐
CoovallyAIHub几秒前
Le-DETR:省80%预训练数据,这个实时检测Transformer刷新SOTA|Georgia Tech & 北交大
深度学习·算法·计算机视觉
CoovallyAIHub15 分钟前
强化学习凭什么比监督学习更聪明?RL的“聪明”并非来自算法,而是因为它学会了“挑食”
深度学习·算法·计算机视觉
CoovallyAIHub27 分钟前
YOLO-IOD深度解析:打破实时增量目标检测的三重知识冲突
深度学习·算法·计算机视觉
NAGNIP11 小时前
轻松搞懂全连接神经网络结构!
人工智能·算法·面试
NAGNIP12 小时前
一文搞懂激活函数!
算法·面试
董董灿是个攻城狮12 小时前
AI 视觉连载7:传统 CV 之高斯滤波实战
算法
爱理财的程序媛18 小时前
openclaw 盯盘实践
算法
MobotStone21 小时前
Google发布Nano Banana 2:更快更便宜,图片生成能力全面升级
算法
颜酱1 天前
队列练习系列:从基础到进阶的完整实现
javascript·后端·算法