#include <bits/stdc++.h>
using namespace std;
//14888 ㄱㄱ
int n,m;
int cnt=0;

//열 col
bool isin1[100];
//y=x 대각선 r+c
bool isin2[100];
//y=-x 대각선 r-c + n-1
bool isin3[100];
void n_queen(int row){
    if(row==n) {
        cnt++;
        return;
    }
    
        for(int col=0; col<n; col++) {
            if(!isin1[col] && !isin2[row+col] && !isin3[row-col+n-1]) {
                isin1[col]=1;
                isin2[row+col]=1;
                isin3[row-col+n-1]=1;
                n_queen(row+1);
                //중요!!!!
                isin1[col]=0;
                isin2[row+col]=0;
                isin3[row-col+n-1]=0;
            }
        }
}


int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> n;
    n_queen(0);
    cout << cnt;
    return 0;
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: