搜索与图论:深度优先搜索
- 题目描述
- 参考代码
题目描述
参考代码
#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 < nfor (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;
}