Untitled

                Never    
C++
       
#include <iostream>
#include <string>
#include <cstring>

using namespace std;

vector<vector<int> > grid; 
int n;
int ans=0;
bool col[100];
bool RU[100];
bool LU[100];
bool row[100];

bool illegal(int idx){
    int r=idx/n;
    int c=idx%n;
    int RUU=r+c;
    int LUU=r-c;
    return col[c] || RU[RUU] || LU[LUU] || row[r];
}

void update(int idx,bool val){
    int r=idx/n;
    int c=idx%n;
    int RUU=r+c;
    int LUU=r-c;
    col[c]=val;
    RU[RUU]=val;
    LU[LUU]=val;
    row[r]=val;
}

void dfs(int y){
    /*
    
    if(idx==n*n){
        ans+=1;
        return;
    }

    if(!illegal(idx)){
        update(idx,true);
        dfs(idx+1);
        update(idx,false);
    }

    dfs(idx+1);
    return;

    */

    
    if(y==n){
        ans+=1;
        return;
    }
    
    for(int x=0;x<n;x++){
        if(illegal(n*y+x)){
            continue;
        }
        update(n*y+x,true);
        dfs(y+1);
        update(n*y+x,false);
    }
    return;
}

int main(){
    memset(col,0,sizeof(col));
    memset(RU,0,sizeof(RU));
    memset(LU,0,sizeof(LU));
    cin>>n;
    dfs(0);
    cout<<ans;


}

Raw Text