Untitled
raw download clone
TEXT
views 22
,
size 752 b
#include<stdio.h>

int n,ans;
int col[50],cnt[50],cntt[50];
int queens(int row);
int valid(int x,int y);

int main(void)
{
    scanf("%d",&n);
    queens(0);
    printf("%d",ans);
    return 0;
}

int valid(int x,int y)
{
    for(int i=0;i<x;i++){
        if(col[y]==1||cnt[i-y+15]==1||cntt[i+y]==1) return 0;
    }
    return 1;
}
int queens(int row)
{
    if(row==n)
        ans++;
    else{
        for(int j=0;j<n;j++){
            if(valid(row,j)){
                col[j]=1;
                cnt[row-j+15]=1;
                cntt[row+j]=1;
                queens(row+1);
                col[j]=0;
                cnt[row-j+15]=0;
                cntt[row+j]=0;
            }
        }
    }
    return ans;
}
close fullscreen
Login or Register to edit or fork this paste. It's free.