Boj9663_n Queen
백준 9663 N-Queen
백트래킹
#include<iostream>
using namespace std;
int n;
int cnt;
int arr[14][14];
bool can(int q[],int row){
for (int i = 0; i < row; i++) {
if (q[i] == q[row]||row-i==abs(q[row]-q[i]))
return false;
}
return true;
}
bool queens(int q[],int row) {
if (row >= n) { cnt++; return true; }
for (int i = 1; i <= n; i++) {
q[row] = i;
if (can(q, row)) {
if (queens(q, row + 1)) ;
}
q[row] = 0;
}
return false;
}
int main(){
cin >> n;
int q[255];
queens(&q[0],0);
cout << cnt;
}