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

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

相关推荐
海洲探索-Hydrovo3 小时前
TTP Aether X 天通透传模块丨国产自主可控大数据双向通讯定位模组
网络·人工智能·科技·算法·信息与通信
2401_841495646 小时前
【计算机视觉】基于复杂环境下的车牌识别
人工智能·python·算法·计算机视觉·去噪·车牌识别·字符识别
Jonkin-Ma6 小时前
每日算法(1)之单链表
算法
晚风残6 小时前
【C++ Primer】第六章:函数
开发语言·c++·算法·c++ primer
杨云强7 小时前
离散积分,相同表达式数组和公式
算法
地平线开发者7 小时前
征程 6 | BPU trace 简介与实操
算法·自动驾驶
满天星83035777 小时前
【C++】AVL树的模拟实现
开发语言·c++·算法·stl
Lris-KK7 小时前
力扣Hot100--94.二叉树的中序遍历、144.二叉树的前序遍历、145.二叉树的后序遍历
python·算法·leetcode
麦麦鸡腿堡8 小时前
Java的动态绑定机制(重要)
java·开发语言·算法
zy_destiny8 小时前
【工业场景】用YOLOv8实现抽烟识别
人工智能·python·算法·yolo·机器学习·计算机视觉·目标跟踪