-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCSES1624.cpp
90 lines (66 loc) · 1.41 KB
/
CSES1624.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
#include<bits/stdc++.h>
#define ll long long
#define F first
#define S second
#define mp make_pair
#define pb push_back
ll gcd(ll a, ll b)
{
if (a == 0)
return b;
if (b == 0)
return a;
if (a == b)
return a;
if (a > b)
return gcd(a - b, b);
return gcd(a, b - a);
}
using namespace std;
char board[8][8];
int ans;
bool isSafe(int queens[8][8], int row, int col) {
for(int i = 0; i < col; i++) {
if(queens[row][i])
return false;
}
for (int i = row, j = col; i >= 0 && j >= 0; i--, j--) {
if (queens[i][j])
return false;
}
for (int i = row, j = col; j >= 0 && i < 8; i++, j--) {
if (queens[i][j])
return false;
}
return true;
}
void backtrack(int queens[8][8], int col) {
if(col >= 8) {
ans++;
return;
}
for(int i = 0; i < 8; i++) {
if(isSafe(queens, i, col) && board[i][col] == '.') {
queens[i][col] = 1;
backtrack(queens, col + 1);
queens[i][col] = 0;
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int queens[8][8];
for(int i = 0; i < 8; i++)
for(int j = 0; j < 8; j++) {
cin >> board[i][j];
queens[i][j] = 0;
}
backtrack(queens, 0);
cout << ans;
return 1/99;
}
/*
RILY <3
NEVER GIVE UP!!!
*/