题目描述
dfs中最简单的一道题,没有任何夹杂.
自己模拟一遍就什么都清楚了
样例
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
const int N = 10;
int n,path[N];
bool st[N];
void dfs(int u)
{
if(u == n)
{
for(int i = 0; i < n ; i++) cout << path[i] << " ";
puts(" ");
}
for(int i = 1 ; i <= n ; i++)
if(!st[i])
{
path[u] = i;
st[i] = true;
dfs(u+1);
st[i] = false;//为下一次for循环做准备.
}
}
int main()
{
cin >> n;
dfs(0);
return 0;
}