图论第一天|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;
    }
};

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

相关推荐
那个村的李富贵2 小时前
CANN加速下的AIGC“即时翻译”:AI语音克隆与实时变声实战
人工智能·算法·aigc·cann
power 雀儿2 小时前
Scaled Dot-Product Attention 分数计算 C++
算法
琹箐2 小时前
最大堆和最小堆 实现思路
java·开发语言·算法
renhongxia13 小时前
如何基于知识图谱进行故障原因、事故原因推理,需要用到哪些算法
人工智能·深度学习·算法·机器学习·自然语言处理·transformer·知识图谱
坚持就完事了3 小时前
数据结构之树(Java实现)
java·算法
算法备案代理3 小时前
大模型备案与算法备案,企业该如何选择?
人工智能·算法·大模型·算法备案
赛姐在努力.3 小时前
【拓扑排序】-- 算法原理讲解,及实现拓扑排序,附赠热门例题
java·算法·图论
野犬寒鸦4 小时前
从零起步学习并发编程 || 第六章:ReentrantLock与synchronized 的辨析及运用
java·服务器·数据库·后端·学习·算法
霖霖总总4 小时前
[小技巧66]当自增主键耗尽:MySQL 主键溢出问题深度解析与雪花算法替代方案
mysql·算法
rainbow68894 小时前
深入解析C++STL:map与set底层奥秘
java·数据结构·算法