#include // https://www.acwing.com/solution/content/34401/ using namespace std; const int N = 20; int a[N]; bool st[N]; int n = 3; void dfs(int step) { if (step == n + 1) { for (int i = 1; i <= n; i++)printf("%d ", a[i]); printf("\n"); return; } for (int i = 1; i <= n; i++) { if (!st[i]) { //记录本位置放了哪个数字 a[step] = i; //标识此数字已使用过了 st[i] = true; //走到下一个位置 dfs(step + 1); //反向标识此数字没有使用过 st[i] = false; } } } int main() { dfs(1); return 0; }