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

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

相关推荐
FPGA_无线通信12 分钟前
OFDM 精频偏补偿
算法·fpga开发
程序员-King.15 分钟前
day109—同向双指针(字符串)—每个字符最多出现两次的最长子字符串(LeetCode-3090)
算法·leetcode·双指针
青山的青衫15 分钟前
【单调栈和单调队列】LeetCode hot100+面试高频
算法·leetcode·面试
俊俊谢23 分钟前
【浮点运算性能优化】浮点转定点算法库的多平台通用移植方案与性能评估优化
算法·性能优化·c·浮点转定点·多平台移植
电饭叔24 分钟前
Luhn算法与信用卡识别完善《python语言程序设计》2018版--第8章14题利用字符串输入作为一个信用卡号之三
android·python·算法
bbq粉刷匠29 分钟前
力扣-电话号码组合
java·算法
狗头实习生38 分钟前
电话号码字母组合
java·算法·leetcode
C雨后彩虹44 分钟前
矩阵扩散问题
java·数据结构·算法·华为·面试
独自破碎E44 分钟前
力场重叠问题
java·开发语言·算法
free-elcmacom1 小时前
机器学习入门<5>支持向量机形象教学:寻找最安全的“三八线”,人人都能懂的算法核心
人工智能·python·算法·机器学习·支持向量机