算法基础集训第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;
    }
};

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

相关推荐
yszaygr21387 小时前
Verilog参数化游程编码RLE模块
算法
望易7 小时前
刚设计的大模型架构-双域耦合认知框架
算法·架构
复杂网络11 小时前
多个 Claude Code 与多个 Codex 协同工作:设计与实现方案
算法
HjhIron1 天前
面试常客:字符串算法从入门到进阶
算法·面试
吴佳浩1 天前
DeepSeek DSpark:Confidence-Scheduled Speculative Decoding 技术解析
人工智能·算法·deepseek
触底反弹1 天前
🧠 搞懂 Token,才算真正入门大模型——从分词原理到 Embedding 语义实战
javascript·人工智能·算法
vivo互联网技术1 天前
ICLR 2026 | 基于后验采样的图像恢复方法LearnIR:人脸去阴影、去雾
人工智能·算法·aigc
浮生望1 天前
JS字符串与回文算法:从包装类到双指针的面试进阶之路
javascript·算法
黄敬峰1 天前
面试必刷:从JS底层包装类到双指针,彻底搞懂字符串与回文算法
算法
地平线开发者2 天前
J6B vio scenario sample
算法