Notice
Recent Posts
Recent Comments
250x250
Creative Code
[9663번]N-Queen 본문
728x90
https://www.acmicpc.net/problem/9663
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
int N; // N*N보드판
int answer = 0;
int col[15];
void queen(int x) {
if (N == x) { // 모든 행에 배치가 가능한 경우 경우의수 +1
answer++;
}
else {
for (int i = 0; i < N; i++) {
col[x] = i;
bool check = true; // 퀸의 배치가 가능한지 체크
for (int j = 0; j < x; j++) {
if (col[x] == col[j] || abs(col[x] - col[j]) == x - j) {//같은 열에 있거나 대각선상에 있을때는 제외
check = false;
break;
}
}
if (check == true) {
queen(x + 1); // 배치가 가능할 시 다음 행으로 넘어감
}
}
}
}
int main() {
cin.tie(0);
cout.tie(0);
ios_base::sync_with_stdio(false);
cin >> N;
queen(0); // 첫번째 행 부터 배치 시작
cout << answer << '\n';
}
728x90
'백준 문제풀이' 카테고리의 다른 글
[14500번]테트로미노 (0) | 2023.09.24 |
---|---|
[9935번]문자열 폭발 (0) | 2023.09.24 |
[9019번]DSLR (0) | 2023.09.24 |
[7662번]이중 우선순위 큐 (0) | 2023.09.24 |
[1806번]부분합 (0) | 2023.09.24 |