-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathLv4_N-Queen.cpp
53 lines (47 loc) · 968 Bytes
/
Lv4_N-Queen.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
#include <string>
#include <vector>
using namespace std;
vector<vector<bool>> arr(13,vector<bool>(13,false));
vector<bool> check_col(13, false);
vector<bool> check_dig(26, false);
vector<bool> check_dig2(26, false);
bool check(int row, int col,int n) {
// |
if (check_col[col]) {
return false;
}
// 왼쪽 위 대각선
if (check_dig[row + col]) {
return false;
}
// /
if (check_dig2[row - col + n]) {
return false;
}
return true;
}
int calc(int n, int row) {
if (row == n) {
// ans += 1;
return 1;
}
int cnt = 0;
for (int col = 0; col < n; col++) {
if (check(row, col, n)) {
check_dig[row + col] = true;
check_dig2[row - col + n] = true;
check_col[col] = true;
arr[row][col] = true;
cnt += calc(n,row + 1);
check_dig[row + col] = false;
check_dig2[row - col + n] = false;
check_col[col] = false;
arr[row][col] = false;
}
}
return cnt;
}
int solution(int n) {
int answer = calc(n,0);
return answer;
}