-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbacktrack-queen-problem.cpp
More file actions
64 lines (57 loc) · 948 Bytes
/
Copy pathbacktrack-queen-problem.cpp
File metadata and controls
64 lines (57 loc) · 948 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
const int N = 10;
int _count = 0;
int n;
int mat[N];
int diag[N];
int diag_inv[N];
void find_ways_queen(int k)
{
if (k == n)
{
_count++;
return;
}
else
{
for (int i = 0; i < n; i++)
{
if (mat[i] || diag[i + k] || diag_inv[i - k + n - 1])
{
continue;
}
mat[i] = diag[i + k] = diag_inv[i - k + n - 1] = 1;
find_ways_queen(k + 1);
mat[i] = diag[i + k] = diag_inv[i - k + n - 1] = 0;
}
}
}
void solve()
{
cin >> n;
find_ways_queen(0);
cout << _count << endl;
}
int main()
{
freopen("input.txt", "r", stdin);
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int no_of_test_cases = 0;
// cin >> no_of_test_cases;
if (!no_of_test_cases)
no_of_test_cases = 1;
while (no_of_test_cases--)
{
solve();
}
return 0;
}