| 알고스팟 NQUEEN |
#include<cstdio>
bool col[30], dwCross[30], upCross[30];
int board[15][15], n, ans = 0;
void NQueen(int depth){
if (depth == n) {
ans++;
return;
}
for (int i = 0; i < n; i++){
if (!col[i] && !dwCross[n + depth - i] && !upCross[depth + i]){
col[i] = true; dwCross[n + depth - i] = true; upCross[depth + i] = true;
NQueen(depth + 1);
col[i] = false; dwCross[n + depth - i] = false; upCross[depth + i] = false;
}
}
}
int main(){
int t; scanf("%d", &t);
while (t--){
ans = 0;
scanf("%d", &n);
NQueen(0);
printf("%d\n", ans);
}
}
bool col[30], dwCross[30], upCross[30];
int board[15][15], n, ans = 0;
void NQueen(int depth){
if (depth == n) {
ans++;
return;
}
for (int i = 0; i < n; i++){
if (!col[i] && !dwCross[n + depth - i] && !upCross[depth + i]){
col[i] = true; dwCross[n + depth - i] = true; upCross[depth + i] = true;
NQueen(depth + 1);
col[i] = false; dwCross[n + depth - i] = false; upCross[depth + i] = false;
}
}
}
int main(){
int t; scanf("%d", &t);
while (t--){
ans = 0;
scanf("%d", &n);
NQueen(0);
printf("%d\n", ans);
}
}
댓글 없음:
댓글 쓰기