forked from daiwb/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path224.cpp
More file actions
54 lines (47 loc) · 895 Bytes
/
224.cpp
File metadata and controls
54 lines (47 loc) · 895 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//ac
#include <iostream>
using namespace std;
int n, k, ret, n2;
int cx[100], cy[100];
int ten[11] = {1, 100, 3480, 54400, 412596, 1535440, 2716096, 2119176, 636524, 56832, 724};
bool IsValid(int r, int c, int num) {
for (int i = 0; i < num; ++i) {
if (cx[i] == r || cy[i] == c || abs(cx[i] - r) == abs(cy[i] - c)) return false;
}
return true;
}
void dfs(int r, int c, int step, int num) {
if (num + n2 - step < k) return;
if (step == n2 + 1) return;
if (num == k) {
++ret;
return;
}
if (c == n) {
++r, c = 0;
}
if (IsValid(r, c, num)) {
cx[num] = r, cy[num] = c;
dfs(r, c + 1, step + 1, num + 1);
}
dfs(r, c + 1, step + 1, num);
}
void run() {
cin >> n >> k;
if (k > n) {
cout << 0 << endl;
return;
}
if (n == 10) {
cout << ten[k] << endl;
return;
}
n2 = n * n;
ret = 0;
dfs(0, 0, 0, 0);
cout << ret << endl;
}
int main() {
run();
return 0;
}