-
Notifications
You must be signed in to change notification settings - Fork 0
/
11085.cpp
65 lines (58 loc) · 814 Bytes
/
11085.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
#include<cstdio>
#include<iostream>
#include<vector>
#include<set>
using namespace std;
vector<int>given(8),pos(8);
set<vector<int>>s;
bool place_allowed(int r,int c)
{
for(int i=0;i<c;i++)
{
if((r==pos[i]) || (abs(r - pos[i])==abs(c-i))) return false;
}
return true;
}
void solve(int c)
{
if(c==8){s.insert(pos);return;}
for(int r=0;r<8;r++)
{
if(place_allowed(r,c))
{
pos[c] = r;
solve(c+1);
}
}
return;
}
int mini()
{
int ans=-1,val;
for(auto x:s)
{
val=0;
for(int i=0;i<8;i++)
{
val+=(x[i]==given[i] ?0:1);
}
if(ans==-1 || val<ans) ans=val;
}
return ans;
}
int main()
{
int i,count=1,ct=1;
solve(0);
while(cin>>given[0])
{
given[0]--;
for(i = 1;i < 8;i++)
{
cin>>given[i];
given[i]--;
}
printf("Case %d: %d\n", ct++, mini());
}
return 0;
}