P1706 全排列问题

// Problem: P1706 全排列问题
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P1706
// Memory Limit: 125 MB
// Time Limit: 1000 ms
// User: Pannnn

#include <bits/stdc++.h>

using namespace std;

void dfs(vector<vector<int>> &res, vector<int> &tmp, vector<int> &isUse, int n, int pos) {
    if (pos >= n) {
        res.push_back(tmp);
        return;
    }
    for (int i = 1; i <= n; ++i) {
        if (isUse[i] == 0) {
            isUse[i] = 1;
            tmp[pos] = i;
            dfs(res, tmp, isUse, n, pos + 1);
            isUse[i] = 0;
        }
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    int n;
    cin >> n;
    vector<vector<int>> res;
    vector<int> tmp(n);
    vector<int> isUse(10);
    dfs(res, tmp, isUse, n, 0);
    
    for (int i = 0; i < res.size(); ++i) {
        for (int j = 0; j < res[i].size(); ++j) {
            printf("%5d", res[i][j]);
        }
        printf("\n");
    }
    return 0;
}
上一篇:django验证码模块使用


下一篇:(翻译)验证码(Captcha)