-
Notifications
You must be signed in to change notification settings - Fork 34
/
1005-Rooks.cpp
97 lines (61 loc) · 954 Bytes
/
1005-Rooks.cpp
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
int a[100];
int n;
int k;
long long ans[40][40];
int ok(int x, int y)
{
for (int i = 0; i < x; i++) {
if(a[i] == y) {
return 0;
}
}
return 1;
}
long long explore(int i, int count)
{
long long x;
long long y;
long long sum = 0;
if(i == n) {
if(count == k) {
return 1;
}
else {
return 0;
}
}
if(ans[i][count] != -1) {
return ans[i][count];
}
for (int j = 0; j < n; j++) {
y = 0;
x = 0;
if(ok(i, j)) {
a[i] = j;
x = explore(i+1, count+1);
}
sum += x;
}
a[i] = -1;
y = explore(i+1, count);
sum += y;
ans[i][count] = sum;
return sum;
}
int main()
{
int t;
scanf("%d", &t);
for (int cs = 1; cs <= t; cs++) {
scanf("%d", &n);
scanf("%d", &k);
memset(a, -1, sizeof(a));
memset(ans, -1, sizeof(ans));
printf("Case %d: %lld\n",cs, explore(0, 0));
}
}