-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathN-Queen.cpp
More file actions
76 lines (58 loc) · 1.74 KB
/
N-Queen.cpp
File metadata and controls
76 lines (58 loc) · 1.74 KB
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
#include <iostream>
#include <vector>
using namespace std;
int GetPermutation(int x, int y,
vector<bool>& visited,
vector<bool>& diagonalPositive,
vector<bool>& diagonalNegative)
{
int boardSize = visited.size();
if (boardSize - 1 == x)
{
return 1;
}
int numPermutation = 0;
for (int i = 0; i < boardSize; ++i)
{
int nx = x + 1;
int ny = i;
int positiveID = ny - nx + boardSize - 1;
int negativeID = nx + ny;
bool isVisited = visited[i];
bool isDiagonalPositive = diagonalPositive[positiveID];
bool isDiagonalNegative = diagonalNegative[negativeID];
if ( !(isVisited || isDiagonalNegative || isDiagonalPositive) )
{
visited[i] = true;
diagonalPositive[positiveID] = true;
diagonalNegative[negativeID] = true;
numPermutation += GetPermutation(nx, ny, visited, diagonalPositive, diagonalNegative);
visited[i] = false;
diagonalPositive[positiveID] = false;
diagonalNegative[negativeID] = false;
}
}
return numPermutation;
}
int main()
{
freopen("input.txt", "r", stdin);
int n;
cin >> n;
vector<bool> visited(n, false);
// ex) n = 6;
// "\", -5 ~ 5 y - x
vector<bool> DiagonalPositive(n + n, false);
// "/" 0 ~ 10, x + y
vector<bool> DiagonalNegative(n + n, false);
cout << GetPermutation(-1, 0, visited, DiagonalPositive, DiagonalNegative) << endl;
// for(int i = 0; i<6; ++i)
// {
// for (int j = 0; j < 6; ++j)
// {
// cout << " " << i << "," << j << " ";
// }
// cout << endl;
// }
return 0;
}