Algorithm/problem
백준 9663번 : N-Queen
DingCoDing
2022. 12. 13. 23:36
반응형
https://www.acmicpc.net/problem/9663
#include <bits/stdc++.h>
using namespace std;
int N;
//i+j, i-j
bool arr[16], diagonal1[32], diagonal2[55];
int ans = 0;
void backTrack(int L) {
if (L == N) {
ans++;
return;
}
for (int i = 0; i < N; i++) {
if (!arr[i] && !diagonal1[L + i] && !diagonal2[L - i + N]) {
arr[i] = diagonal1[L + i] = diagonal2[L - i + N] = 1;
backTrack(L + 1);
arr[i] = diagonal1[L + i] = diagonal2[L - i + N] = 0;
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> N;
backTrack(0);
cout << ans << '\n';
}
반응형