AcWing 842. 排列数字

题目传送门

#include <bits/stdc++.h>

using namespace std;
const int N = 10;
int n;
vector<int> path; //记录走的路径
bool st[N];

void dfs(int u) {
    //如果到达了终点
    if (u == n + 1) {
        for (int i = 0; i < n; i++)
            printf("%d ", path[i]);
        printf("
");
        return;
    }
    for (int i = 1; i <= n; i++)
        if (!st[i]) {
            path.push_back(i);
            st[i] = true;
            dfs(u + 1);
            st[i] = false;
            path.pop_back();
        }
}

int main() {
    //读入优化
    ios::sync_with_stdio(false);
    cin >> n;
    //开始
    dfs(1);
    return 0;
}
原文地址:https://www.cnblogs.com/littlehb/p/15305236.html