提交时间:2023-10-28 09:42:26

运行 ID: 107765

#include<bits/stdc++.h> using namespace std; int a[15], n, cnt; bool b[15], c[15], d[15]; void dfs(int i){ if (i > n){ cnt ++; if (cnt <= 3){ for (int k = 1; k <= n; k++){ cout << a[k] << " "; } cout << endl; } } else { for (int k = 1; k <= n; k++){ if (!b[k] && !c[i + k] && !d[i - k + n]){ b[k] = true; c[i + k] = true; d[i - k + n] = true; a[i] = k; dfs(i + 1); b[k] = false; c[i + k] = false; d[i - k + n] = false; } } } } int main(){ cin >> n; dfs(1); cout << cnt; }