提交时间:2024-03-16 09:33:19
运行 ID: 138381
#include <bits/stdc++.h> using namespace std; int ans, a[18], n; bool b (int cur){ if (cur == 1) return true; for (int j = 1; j < cur; j++){ if (a[j] == a[cur] || abs(a[cur] - a[j]) == abs(cur - j)){ return false; } } return true; } void dfs (int cur){ if (cur > n){ ans++; return; } for (int i = 1; i <= n; i++){ a[cur] = i; if (b(cur)){ dfs(cur + 1); a[cur] = 0; } } } int main (){ cin >> n; dfs(1); cout << ans; return 0; }