搜索与图论(一)

一、DFS与BFS

1.1深度优先搜索(DFS)

DFS不具有最短性

cpp 复制代码
//排列数字问题
#include<iostream>
using namespace std;

const int N = 10;
int n;
int path[N];
bool st[N];

void dfs(int u)
{
    if(u == n)
    {
        for(int i = 0;i < n;i++) printf("%d",path[i]);
        puts("");
        return;
    }
    for(int i =1;i <= n;i++)
    {
        if(!st[i])
        {
            path[u] = i;
            st[i] = true;
            dfs(u + 1);
            st[i] = false;
        }
    }
}
int main()
{
    cin>>n;
    dfs(0);
    return 0;
}

1.2宽度优先搜索(BFS)

一层一层搜索,可以搜到最短路。

cpp 复制代码
//走迷宫问题
#include<iostream>
#include<algorithm>
#include<queue>
#include<cstring>
using namespace std;

typedef pair<int,int> PII;

const int N = 110;

int n,m;
int g[N][N];
int d[N][N];
PII q[N * N];

int bfs()
{
    int hh = 0,tt = 0;
    q[0] = {0,0};

    //初始化为-1
    memset(d,-1,sizeof d);
    d[0][0] = 0;

    //定义头的向量
    int dx[4] = {-1,0,1,0},dy[4] = {0,1,0,-1};

    while(hh <= tt)
    {
        auto t = q[hh ++];

        for(int  i = 0; i< 4;i++)
        {
            int x = t.first + dx[i],y = t.second + dy[i];
            if(x >=0 && x < n && y >= 0 && y < m && g[x][y] ==0 && d[x][y] == -1)
            {
                d[x][y] = d[t.first][t.second] + 1;
                q[++ tt] = {x,y};
            }
        }
    }
    return d[n - 1][m - 1];
}

int main()
{
    cin>>n>>m;

    for(int  i = 0;i < n;i++)
        for(int j = 0;j < m;j++)
            cin>>g[i][j];

    cout<<bfs()<<endl;

    return 0;
}

二、树与图的遍历

2.1树与图的深度优先遍历

cpp 复制代码
#include<iostream>
using namespace std;

int n,m;
//h存的是n个链表的链表头
//e存的是所有的结点值
//ne存的是每个节点的next指针
int h[N],e[M],ne[M],idx;
bool st[N];

void dfs(int u)
{
    stu[u] = true; //标记一下,已经被搜过了
    
    for(int i = h[u];i != -1;i = ne[i])
    {
        int j = e[i];
        if(!st[j]) dfs(j);
    }
}

2.2树与图的广度优先遍历

cpp 复制代码
int n,m;
int h[N],e[N],ne[N],idx;
//d是距离,q是队列
int d[N],q[N];

//插入函数
void add(int a,int b)
{
    e[idx] = b,ne[idx],ne[idx] = h[a],h[a] = idx++;
}
int bfs()
{
    //定义队头队尾
    int hh = 0,tt = 0;
    q[0] = 1;
    
    memset(d,-1,sizeof d);
    
    d[1] = 0;
    
    while(hh <= tt)
    {
        int t = q[hh ++];
        for(int i = h[t];i != -1;i = ne[i])
        {
            int j = e[i];
            if(d[j] == -1)
            {
                d[j] = d[t] + 1;
                q[++ tt] = j;
            }
        }
    }
    return d[n];
}

三、拓扑排序

适用于有向图

cpp 复制代码
#include<cstring>
#include<iostream>
#include<algorithm>

using namespace std;

const int N = 100010;

int n,m;
int h[N],e[N],ne[N],idx;
//q为队列,d为存储的入度
int q[N],d[N];

void add(int a,int b)
{
    e[idx] = b,ne[idx] = h[a],h[a] = idx++;
}

bool topsort()
{
    int hh = 0,tt = -1;

    for(int i =1;i <= n;i++)
    {
        if(!d[i])
            q[++tt] = i;
    }
    while(hh <= tt)
    {
        int t = q[hh++];

        for(int i = h[t];i!=-1;i = ne[i])
        {
            int j = e[i];
            d[j]--;
            if(d[j] == 0) q[++tt] = j;
        }
    }
    return tt == n-1;
}
int main()
{
    cin>>n>>m;

    memset(h,-1,sizeof h);

    for(int  i = 0;i < m;i++)
    {
        int a,b;
        cin>>a>>b;
        add(a,b);
    }
    if(topsort())
    {
        for(int i =0;i < n;i++) printf("%d",q[i]);
        puts("");
    }
    else{
        puts("-1");
    }

    return 0;
}
相关推荐
计信金边罗1 天前
是否存在路径(FIFOBB算法)
算法·蓝桥杯·图论
闻缺陷则喜何志丹3 天前
【二分图 图论】P9384 [THUPC 2023 决赛] 着色|普及+
c++·算法·图论·二分图·洛谷
鸽子炖汤3 天前
LRC and VIP
c++·算法·图论
JK0x075 天前
代码随想录算法训练营 Day61 图论ⅩⅠ Floyd A※ 最短路径算法
算法·图论
qq_447429415 天前
数据结构与算法:图论——拓扑排序
linux·c语言·学习·图论
zc.ovo5 天前
图论刷题1
算法·深度优先·图论
珂朵莉MM5 天前
2022 RoboCom 世界机器人开发者大赛-本科组(省赛)解题报告 | 珂学家
人工智能·算法·职场和发展·深度优先·图论
蒙奇D索大6 天前
【数据结构】图论核心算法解析:深度优先搜索(DFS)的纵深遍历与生成树实战指南
数据结构·算法·深度优先·图论·图搜索算法
ShiinaMashirol6 天前
代码随想录打卡|Day50 图论(拓扑排序精讲 、dijkstra(朴素版)精讲 )
java·图论
JK0x078 天前
代码随想录算法训练营 Day60 图论Ⅹ Bellmen_ford 系列算法
android·算法·图论