Run ID | 作者 | 问题 | 语言 | 测评结果 | 分数 | 时间 | 内存 | 代码长度 | 提交时间 |
---|---|---|---|---|---|---|---|---|---|
138375 | 梁思宸 | N皇后问题 | C++ | 运行超时 | 80 | 1000 MS | 240 KB | 477 | 2024-03-16 09:11:07 |
#include <bits/stdc++.h> using namespace std; int ans, a[18], n; bool b (int i, int cur){ if (cur == 1) return true; for (int j = 1; j < cur; j++){ if (a[j] == i || abs(i - 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++){ if (b(i, cur)){ a[cur] = i; dfs(cur + 1); } } } int main (){ cin >> n; dfs(1); cout << ans; return 0; }