搜索与图论:深度优先搜索

搜索与图论:深度优先搜索

题目描述

参考代码

cpp 复制代码
#include <iostream>

using namespace std;

const int N = 10;

int n;
int path[N];
bool st[N];

void dfs(int u)
{
    // u == n 搜索到最后一层
    if (u == n)
    {
        for (int i = 0; i < n; i++) printf("%d ", path[i]);
        puts("");
        return;
    }
    // u < n
    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 天前
跟着TRAE SOLO学习两大搜索
前端·算法
ghie90901 天前
图像去雾算法详解与MATLAB实现
开发语言·算法·matlab
云泽8081 天前
从三路快排到内省排序:探索工业级排序算法的演进
算法·排序算法
weixin_468466851 天前
遗传算法求解TSP旅行商问题python代码实战
python·算法·算法优化·遗传算法·旅行商问题·智能优化·np问题
FMRbpm1 天前
链表5--------删除
数据结构·c++·算法·链表·新手入门
程序员buddha1 天前
C语言操作符详解
java·c语言·算法
John_Rey1 天前
API 设计哲学:构建健壮、易用且符合惯用语的 Rust 库
网络·算法·rust
愿没error的x1 天前
动态规划、贪心算法与分治算法:深入解析与比较
算法·贪心算法·动态规划
NONE-C1 天前
动手学强化学习 第6章 Dyna-Q 算法
算法
惊讶的猫1 天前
面向无监督行人重识别的摄像头偏差消除学习
人工智能·算法·机器学习