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

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

相关推荐
旖-旎32 分钟前
深搜练习(组合)(5)
c++·算法·深度优先·力扣
@小码农1 小时前
2026年3月Scratch图形化编程等级考试一级真题试卷
开发语言·数据结构·c++·算法
Wect2 小时前
LeetCode 5. 最长回文子串:DP + 中心扩展
前端·算法·typescript
糖果店的幽灵2 小时前
决策树详解与sklearn实战
算法·决策树·sklearn
Lewiis2 小时前
趣谈排序算法
算法·排序算法
ComputerInBook2 小时前
数字图像处理(4版)——第 8 章——图像压缩与水印(上)(Rafael C.Gonzalez&Richard E. Woods)
人工智能·算法·计算机视觉·图像压缩·图像水印
刀法如飞2 小时前
Python列表去重:从新手三连到高阶特技,20种解法全收录
python·算法·编程语言
minji...2 小时前
算法题 动态规划
算法·动态规划
水蓝烟雨3 小时前
3337. 字符串转换后的长度 II
算法·leetcode
MegaDataFlowers3 小时前
SiliconCompiler workflow
算法