代码随想录day50图论1

文章目录

深度优先搜索理论基础

cpp 复制代码
void dfs(参数) {
    if (终止条件) {
        存放结果;
        return;
    }

    for (选择:本节点所连接的其他节点) {
        处理节点;
        dfs(图,选择的节点); // 递归
        回溯,撤销处理结果
    }
}

98. 所有可达路径

题目链接
文章讲解

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
bool flag;
vector<int> path;
void dfs(vector<vector<int>>& grid,int x,int n)
{
    if(x==n)
    {
        flag=true;
        cout<<"1 ";
        for(int i=0;i<path.size();i++)
        {
            if(i!=path.size()-1)
            cout<<path[i]<<" ";
            else cout<<path[i]<<endl;
        }
    }
    for(int i=1;i<=n;i++)
        {
            if(grid[x][i]==1)
            {
                path.push_back(i);
                dfs(grid,i,n);
                path.pop_back();
            }
        }
     
}
 
int main(){
    int n,m;
    cin>>n>>m;
    vector<vector<int>> grid(n+1,vector<int>(n+1,0));
    for(int i=0;i<m;i++)
    {
        int x,y;
        cin>>x>>y;
        grid[x][y]=1;
    }
    dfs(grid,1,n);
    if(!flag) cout<<-1;
}

797.所有可能的路径

题目链接

新创建一个二维数组

cpp 复制代码
class Solution {
public:
    vector<vector<int>> ans;
    vector<int> path;
    void dfs(vector<vector<int>>& grid,int x,int n)
    {
        if(x==n-1)
        {
            ans.push_back(path);
            return ;
        }
        for(int i=0;i<n;i++)
        {
            if(grid[x][i]==1)
            {
                path.push_back(i);
                dfs(grid,i,n);
                path.pop_back();
            }
        }
    }
    vector<vector<int>> allPathsSourceTarget(vector<vector<int>>& graph) {
        int n=graph.size();
        vector<vector<int>> grid(n,vector<int>(n,0));
        for(int i=0;i<n;i++)
        {
            int x=graph[i].size();
            for(int j=0;j<x;j++)
            {
                int y=graph[i][j];
                grid[i][y]=1;
            }
        }
        path.push_back(0);
        dfs(grid,0,n);
        return ans;
    }
};

不用新建

cpp 复制代码
class Solution {
public:
    vector<vector<int>> ans;
    vector<int> path;
    void dfs(vector<vector<int>>& graph,int x,int n)
    {
        if(x==n)
        {
            ans.push_back(path);
            return ;
        }
        for(int i=0;i<graph[x].size();i++)
        {
                path.push_back(graph[x][i]);
                dfs(graph,graph[x][i],n);
                path.pop_back();
        }
    }
    vector<vector<int>> allPathsSourceTarget(vector<vector<int>>& graph) {
        int n=graph.size();
        path.push_back(0);
        dfs(graph,0,n-1);
        return ans;
    }
};

BFS理论基础

cpp 复制代码
int dir[4][2] = {0, 1, 1, 0, -1, 0, 0, -1}; // 表示四个方向
// grid 是地图,也就是一个二维数组
// visited标记访问过的节点,不要重复访问
// x,y 表示开始搜索节点的下标
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(); // 从队列取元素
        int curx = cur.first;
        int cury = cur.second; // 当前节点坐标
        for (int i = 0; i < 4; i++) { // 开始想当前节点的四个方向左右上下去遍历
            int nextx = curx + dir[i][0];
            int nexty = cury + dir[i][1]; // 获取周边四个方向的坐标
            if (nextx < 0 || nextx >= grid.size() || nexty < 0 || nexty >= grid[0].size()) continue;  // 坐标越界了,直接跳过
            if (!visited[nextx][nexty]) { // 如果节点没被访问过
                que.push({nextx, nexty});  // 队列添加该节点为下一轮要遍历的节点
                visited[nextx][nexty] = true; // 只要加入队列立刻标记,避免重复访问
            }
        }
    }

}
相关推荐
漂流瓶jz12 天前
UVA-1613 K度图的着色 题解答案代码 算法竞赛入门经典第二版
算法·深度优先·图论·贪心·aoapc·算法竞赛入门经典·uva
YLCHUP13 天前
【思维&图论建模】题解:债权(debt)_图论_建模_思路转化_思维题目_C++算法竞赛
c++·笔记·算法·数学建模·贪心算法·深度优先·图论
charliejohn15 天前
计算机考研 408 数据结构 图论-关键路径
数据结构·图论
Tairitsu_H24 天前
[C++高阶数据结构·图论下] 最短路径:为什么“挑最小的”一定正确?
数据结构·c++·图论
Tairitsu_H24 天前
[C++高阶数据结构·图论上] 最小生成树:为什么“选边”和“选点”是两条路?
数据结构·c++·图论·最小生成树
Mortalbreeze1 个月前
0 - 1 BFS 算法详解:从基本原理到经典题目建模
数据结构·c++·算法·图论·宽度优先
一去不复返的通信er1 个月前
力扣-图论-岛屿数量
算法·leetcode·图论
春弦_1 个月前
自用算法整理二
数据结构·算法·图论
lvwangshu1 个月前
P8981 「DROI」Round 1 距离 题解
动态规划·图论·题解·dp·树形dp·技巧·换根dp·性质题·思维好题
海海不掉头发1 个月前
图论与应用_课堂笔记
笔记·图论