做题地址:https://www.luogu.com.cn/problem/solution/P1219
这一题是一道dfs问题
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>using namespace std;const int N = 16;int n;
bool col[N * N], dg[N * N], udg[N * N];
int g[N * N], res;void dfs(int u){
if (u == n) {
res ++;if (res <= 3){
for (int i = 0; i < n; i ++ ) cout << g[i] + 1<< ' ';puts("");return;}}for (int i = 0; i < n; i ++ ){
if (!col[i] && !dg[u + i] && !udg[n - u + i]){
g[u] = i;col[i] = dg[u + i] = udg[i - u + n] = true;dfs(u + 1);col[i] = dg[u + i] = udg[i - u + n] = false;}}
}
int main(){
cin >> n;dfs(0);cout << res << endl;return 0;
}