We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent bb041bd commit 59b5d35Copy full SHA for 59b5d35
Solutions/[19]_1.cpp
@@ -0,0 +1,39 @@
1
+#include <bits/stdc++.h>
2
+
3
+using namespace std;
4
5
+int n, result;
6
+int row[15];
7
8
+// x번째 행에 놓은 Queen에 대하여 검증
9
+bool check(int x) {
10
+ // 이전 행에서 놓았던 모든 Queen들을 확인
11
+ for (int i = 0; i < x; i++) {
12
+ // 위쪽 혹은 대각선을 확인
13
+ if (row[x] == row[i]) return false;
14
+ if (abs(row[x] - row[i]) == x - i) return false;
15
+ }
16
+ return true;
17
+}
18
19
+// x번째 행에 대하여 처리
20
+void dfs(int x) {
21
+ if (x == n) result += 1;
22
+ else {
23
+ // x번째 행의 각 열에 Queen을 둔다고 가정
24
+ for (int i = 0; i < n; i++) {
25
+ row[x] = i;
26
+ // 해당 위치에 Queen을 두어도 괜찮은 경우
27
+ if (check(x)) {
28
+ // 다음 행으로 넘어가기
29
+ dfs(x + 1);
30
31
32
33
34
35
+int main() {
36
+ cin >> n;
37
+ dfs(0);
38
+ cout << result;
39
0 commit comments