-
Notifications
You must be signed in to change notification settings - Fork 166
/
Chessboard and Queens.cpp
56 lines (50 loc) · 1.05 KB
/
Chessboard and Queens.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
#include <bits/stdc++.h>
using namespace std;
const int N = 8;
char c;
int ans;
bool b[N][N], input[N][N];
bool place(int x, int y){
bool yes = true;
for(int i = 0; i < N; i++)
if(b[x][i] || b[i][y])
yes = false;
for(int i = 0; x-i >= 0 && y-i >= 0; i++)
if(b[x-i][y-i])
yes = false;
for(int i = 0; x-i >= 0 && y+i < N; i++)
if(b[x-i][y+i])
yes = false;
return yes;
}
bool check(){
for(int i = 0; i < N; i++)
for(int j = 0; j < N; j++)
if(b[i][j] && input[i][j])
return false;
return true;
}
void dfs(int i){
if(i == N){
if(check())
ans++;
return;
}
for(int j = 0; j < N; j++){
if(place(i, j)){
b[i][j] = true;
dfs(i+1);
b[i][j] = false;
}
}
}
int main(){
for(int i = 0; i < N; i++){
for(int j = 0; j < N; j++){
scanf(" %c", &c);
input[i][j] = (c == '*');
}
}
dfs(0);
printf("%d\n", ans);
}