Untitled
raw download clone
C
views 30
,
size 534 b
#include<stdio.h>
int col[100],cnt[100],cnt2[100]; 
int ans=0,n;
int check(int x,int y){
	if(col[y]==0&&cnt[x-y+20]==0&&cnt2[x+y]==0)return 1; 
}
int queen(int row_now){
	int i;
	if (row_now==n){
		ans++;
		}
	else {
		for(i=0;i<n;i++){
			if(check(row_now,i)==1){
				col[i]=1;
				cnt[row_now-i+20]=1;
				cnt2[row_now+i]=1;
				queen(row_now+1);		
				col[i]=0;
				cnt[row_now-i+20]=0;
				cnt2[row_now+i]=0;
		}}
	}
}
int main(){
	int n;
	scanf("%d",&n);
	queen(0);
	printf("%d\n",ans);
	return 0;
}
close fullscreen
Login or Register to edit or fork this paste. It's free.