提交时间:2023-10-28 09:56:46

运行 ID: 107788

#include <bits/stdc++.h> using namespace std; int n; int sum,a[105]; bool b[105],c[105],d[105]; void print() { sum++; if(sum <= 3) { for(int k = 1;k <= n;k++) { cout << a[k]; if(k != n) cout << ' '; } cout << '\n'; } } void dfs(int i) { if(i > n) { print(); return ; } else { for(int j = 1;j <= n;j++) { if((b[j] == 0) && (c[i + j] == 0) && (d[i - j + n] == 0)) { b[j] = 0; c[i + j]; d[i - j + n] = 1; a[i] = j; dfs(i + 1); b[j] = 0; c[i + j] = 0; d[i - j + n] = 0; } } } } int main() { ios::sync_with_stdio(0); cin >> n; dfs(1); cout << sum; }