-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy path50.cpp
More file actions
65 lines (50 loc) · 1.45 KB
/
Copy path50.cpp
File metadata and controls
65 lines (50 loc) · 1.45 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
#include <vector>
#include <algorithm>
using namespace std;
//❶ 현재 행에 이미 다른 퀸이 있는지 확인하는 함수
bool isSameRow(int placedRow, int currentRow) {
return placedRow == currentRow;
}
//❷ 대각선에 다른 퀸이 있는지 확인하는 함수
bool isDiagonalAttack(int placedCol, int placedRow, int currentCol, int currentRow) {
return abs(placedCol - currentCol) == abs(placedRow - currentRow);
}
//❸ 퀸을 안전하게 배치할 수 있는지 확인하는 함수
bool isSafePosition(const vector<int> &queen, int col, int row) {
for (int i = 0; i < col; ++i) {
if (isSameRow(queen[i], row) || isDiagonalAttack(i, queen[i], col, row)) {
return false;
}
}
return true;
}
//❹ 퀸을 배치하는 함수
long long placeQueens(vector<int> &queen, int col) {
int n = queen.size();
if (col == n) {
return 1;
}
long long count = 0;
for (int row = 0; row < n; ++row) {
//❺ 퀸을 놓을수 있는 위치인 경우 퀸을 놓음
if (isSafePosition(queen, col, row)) {
queen[col] = row;
count += placeQueens(queen, col + 1);
queen[col] = -1;
}
}
return count;
}
long long solution(int n) {
vector<int> queen(n, -1);
//❻ 퀸을 놓을수 있는 경우의 수를 반환
return placeQueens(queen, 0);
}
//아래 코드는 테스트 코드 입니다.
#include <iostream>
using namespace std;
int main()
{
cout << solution(4) << endl; // 출력값 : 2
return 0;
}