-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject10-1n-Queens.cpp
More file actions
87 lines (66 loc) · 1.54 KB
/
project10-1n-Queens.cpp
File metadata and controls
87 lines (66 loc) · 1.54 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
77
78
79
80
81
82
83
84
85
86
87
// 중복되는 경로중 최소값이면 부호 바꾸고 맥스값을 1000000로 큰값으로 지정
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
int n;
vector<int> col;
int cnt;
vector<int> tmp;
string maxString = "";
long long maxNum = -1;
bool promising(int i);
void queens(int i);
int main(void)
{
cin >> n; // n*n행렬
if (n<4)
{
EXIT_FAILURE;
}
col.resize(n+1);
queens(0);
cnt = (int)tmp.size()/n;
cout << cnt << endl; // 가능한 보드의 배치 개수를 출력
cout << maxNum; // 경로 중 문자열로 취급했을때 최대값을 출력
// tmp를 n만큼 쪼개고 그것의 크기를 비교해서 젤 큰것을 출력
}
bool promising(int i)
{
int k= 1;
bool flag = true;
while (k<i && flag)
{
if (col[i] == col[k] || abs(col[i] - col[k]) == i - k)
flag = false;
k++;
}
return flag;
}
void queens(int i)
{
int j;
if (promising(i))
{
if (i== n)
{
for (int k=1;k<=n;k++)
{
maxString = maxString + to_string(col[k]);
tmp.push_back(col[k]);
}
if (stoll(maxString) > maxNum) // 부호 바꾸기
{
maxNum = stoll(maxString);
}
maxString.clear();
}
else
for (j=1;j<=n;j++)
{
col[i+1] = j;
queens(i+1);
}
}
}