-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
41 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
//210327 | ||
//백준 9663번 N-Queen (골드 5) | ||
//https://www.acmicpc.net/problem/9663 | ||
|
||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
int board[15]; | ||
int n, count = 0; | ||
|
||
int checkCondition(int row) { | ||
for (int i = 0; i < row; i++) { // 상위 행들에 대해 반복 | ||
// 현재 행의 열 == 상위 행의 열 (같은 열에 있는 경우) 혹은 현재 행 - 상위 행 == 현재 열 - 상위 열(같은 대각선상에 있는 경우) 이면 | ||
if (board[row] == board[i] || row - i == abs(board[row] - board[i])) { | ||
return 0; // row 행에 col번째 열은 불가능. 다시 돌아간다. | ||
} | ||
} | ||
return 1; // for문을 무사히 통과했다면 이 행은 통과 | ||
} | ||
|
||
void nQueen(int row) { | ||
if (row == n) { // 모든 행을 다 통과했다면 | ||
count++; // 횟수 하나 세기 | ||
return; | ||
} | ||
|
||
for (int col = 0; col < n; col++) { | ||
board[row] = col; // row 행에 col번째 열을 넣어보고 | ||
if (checkCondition(row)) { // 이 행에서 가능한지 검사 | ||
nQueen(row + 1); // 가능하면 다음 행으로 넘어감 | ||
} | ||
} | ||
} | ||
|
||
int main() { | ||
scanf("%d", &n); | ||
nQueen(0); | ||
printf("%d", count); | ||
return 0; | ||
} |