-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11085.cpp
More file actions
43 lines (41 loc) · 859 Bytes
/
11085.cpp
File metadata and controls
43 lines (41 loc) · 859 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
#include <bits/stdc++.h>
using namespace std;
#define mem(x, y) memset(x, y, sizeof(x));
int a[15];
int b[15];
int SUM;
bool check(int x, int y) {
int i;
for (i = 1; i < x; i++) {
if (a[i] == y) return false;
if (abs(a[i] - y) == abs(x - i)) return false;
}
return true;
}
void call(int k) {
int i;
if (k == 9) {
int sum = 0;
for (i = 1; i <= 8; i++)
if (a[i] != b[i]) sum++;
if (sum < SUM) SUM = sum;
return;
}
for (i = 1; i <= 8; i++) {
if (check(k, i)) {
a[k] = i;
call(k + 1);
}
}
return;
}
int main() {
int t = 1, i;
while (scanf("%d", &b[1]) == 1) {
for (i = 2; i <= 8; i++) scanf("%d", &b[i]);
SUM = 10;
call(1);
printf("Case %d: %d\n", t++, SUM);
}
return 0;
}