DFS
AcWing 842. 排列数字
#include <iostream>
using namespace std;
const int N = 10;
int n;
int path[N], 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 ++) //遍历n个数为path赋值
{
if (!st[i]) //当n中某个数未被使用
{
path[u] = i;
st[i] = true; //st中i这个数已经被使用
dfs(u + 1);
st[i] = false; //回溯恢复
}
}
}
int main()
{
cin >> n;
dfs(0);
return 0;
}
AcWing 843. n-皇后问题
标签:BFS,int,dfs,---,算法,AcWing
From: https://www.cnblogs.com/hjy94wo/p/16649745.html