-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSCTDL023.cpp
63 lines (59 loc) · 1.19 KB
/
SCTDL023.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
/**
* @file SCTDL023.cpp
* @author long ([email protected])
* @brief C++ program counting ways to place Queen in chessboard
* @version 0.1
* @date 2023-02-27
*
* @copyright Copyright (c) 2023
*
*/
#include<iostream>
#define MAX 100
#define TRUE 1
#define FALSE 0
using namespace std;
int X[MAX], toFront[MAX], toBack[MAX], notVisited[MAX];
int n, counter{0};
void Init (void ) {
counter = 0;
cin >> n;
for (int i=1; i<=n; i++)
{
notVisited[i] = TRUE;
}
for (int i=1; i<=(2*n-1); i++)
{
toFront[i] = TRUE;
toBack[i]=TRUE;
}
}
void Try(int i){
for (int j =1; j<=n; j++){
if (notVisited[j] && toFront[i-j+n] && toBack[i+j-1]){
X[i] = j;
notVisited[j]=FALSE;
toFront[i-j+n]=FALSE;
toBack[i+j-1]=FALSE;
if (i==n) {
counter++;
} else {
Try(i+1);
}
notVisited[j]=TRUE;
toFront[i-j+n]=TRUE;
toBack[i+j-1]=TRUE;
}
}
}
int main(){
int t;
cin >> t;
while(t--) {
Init();
Try(1);
cout << counter << endl;
counter = 0;
}
return 0;
}