算法基础集训第19天:广度优先搜索

1971. 寻找图中是否存在路径 - 力扣(LeetCode)

BFS

cpp 复制代码
class Solution
{
public:
    bool validPath(int n, vector<vector<int>> &edges, int source, int destination)
    {
        vector<vector<int>> adj(n);
        for (int i = 0; i < edges.size(); i++)
        {
            adj[edges[i][0]].push_back(edges[i][1]);
            adj[edges[i][1]].push_back(edges[i][0]);
        }
        queue<int> q;
        vector<bool> vis(n, false);
        q.push(source);
        vis[source] = true;
        while (!q.empty())
        {
            int t = q.front();
            q.pop();
            if (t == destination)
            {
                return true;
            }
            for (int i = 0; i < adj[t].size(); i++)
            {
                if (!vis[adj[t][i]])
                {
                    vis[adj[t][i]] = true;
                    q.push(adj[t][i]);
                }
            }
        }
        return false;
    }
};

DFS

cpp 复制代码
class Solution
{
    vector<vector<int>> adj;
    vector<bool> vis;
    bool dfs(int source, int destination)
    {
        if (source == destination)
        {
            return true;
        }
        for (int i = 0; i < adj[source].size(); i++)
        {
            if (!vis[adj[source][i]])
            {
                vis[adj[source][i]] = true;
                if (dfs(adj[source][i], destination))
                {
                    return true;
                }
            }
        }
        return false;
    }

public:
    bool validPath(int n, vector<vector<int>> &edges, int source, int destination)
    {
        adj.resize(n);
        for (int i = 0; i < edges.size(); i++)
        {
            adj[edges[i][0]].push_back(edges[i][1]);
            adj[edges[i][1]].push_back(edges[i][0]);
        }
        vis.resize(n, false);
        vis[source] = true;
        return dfs(source, destination);
    }
};

841. 钥匙和房间 - 力扣(LeetCode)

BFS

cpp 复制代码
class Solution
{
public:
    bool canVisitAllRooms(vector<vector<int>> &rooms)
    {
        int n = rooms.size();
        vector<bool> vis(n, false);
        queue<int> q;
        q.push(0);
        vis[0] = true;
        while (!q.empty())
        {
            int t = q.front();
            q.pop();
            for (int i = 0; i < rooms[t].size(); i++)
            {
                if (!vis[rooms[t][i]])
                {
                    vis[rooms[t][i]] = true;
                    q.push(rooms[t][i]);
                }
            }
        }
        for (int i = 0; i < n; i++)
        {
            if (vis[i] == false)
            {
                return false;
            }
        }
        return true;
    }
};

DFS

cpp 复制代码
class Solution
{
    vector<bool> vis;
    int num = 1;
    void dfs(int x, vector<vector<int>> &rooms)
    {
        for (int i = 0; i < rooms[x].size(); i++)
        {
            if (!vis[rooms[x][i]])
            {
                num++;
                vis[rooms[x][i]] = true;
                dfs(rooms[x][i], rooms);
            }
        }
    }

public:
    bool canVisitAllRooms(vector<vector<int>> &rooms)
    {
        int n = rooms.size();
        vis.resize(n, false);
        vis[0] = true;
        dfs(0, rooms);
        return n == num;
    }
};

2368. 受限条件下可到达节点的数目 - 力扣(LeetCode)

BFS

cpp 复制代码
class Solution
{
public:
    int reachableNodes(int n, vector<vector<int>> &edges, vector<int> &restricted)
    {
        vector<vector<int>> adj(n);
        for (int i = 0; i < edges.size(); i++)
        {
            adj[edges[i][0]].push_back(edges[i][1]);
            adj[edges[i][1]].push_back(edges[i][0]);
        }
        int res = 1;
        vector<bool> vis(n, false);
        for (int i = 0; i < restricted.size(); i++)
        {
            vis[restricted[i]] = true;
        }
        queue<int> q;
        q.push(0);
        vis[0] = true;
        while (!q.empty())
        {
            int t = q.front();
            q.pop();
            for (int i = 0; i < adj[t].size(); i++)
            {
                if (!vis[adj[t][i]])
                {
                    res++;
                    vis[adj[t][i]] = true;
                    q.push(adj[t][i]);
                }
            }
        }
        return res;
    }
};

知识星球 | 深度连接铁杆粉丝,运营高品质社群,知识变现的工具

相关推荐
倒头就睡的小比特2 天前
算法竞赛C++常用的STL
c++·算法
小羊没烦恼!2 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
猎头南楼2 天前
知识社区推荐系统实践:新用户冷启动与长短期兴趣建模的挑战 资深推荐算法工程师
人工智能·深度学习·算法·机器学习
旖旎夜光3 天前
力控面试题 01.01: 判定字符是否唯一(位运算) —— 题解
c++·学习·算法·leetcode·力控
wzdark3 天前
大规模并行计算中的负载均衡算法研究4
算法
Because_of_Her13 天前
并查集-听课笔记
笔记·算法·并查集
码流子3 天前
高速公路安全监测实践:碰撞监测预警+物联网底座,从感知到处置的闭环
大数据·人工智能·物联网·算法·架构
another heaven3 天前
【算法/C++ MD5算法能否逆解码?原理、C++实现与同类哈希算法对比】
c++·算法·哈希算法
wzdark3 天前
从算法设计模式看编程思维的抽象能力4
算法
2601_962218613 天前
万象生鲜系统称重自动多退少补算法解决生鲜非标品痛点
大数据·数据库·人工智能·python·算法