提交时间:2023-10-05 07:59:07
运行 ID: 105017
#include<stdio.h> #include<math.h> int n,ans=0; int f[1000]; int check(int x,int y){ int i,j; for(i=1;i<x;i++){ if(f[i]==y || abs(x-i)==abs(y-f[i])){ return 0; } } return 1; } int dfs(int i){ int j; if(i==n+1){ ans++; return 0; } for(j=1;j<=n;j++){ if(check(i,j)==1){ f[i]=j; dfs(i+1); } } } int main(){ int i,j; scanf("%d",&n); dfs(1); printf("%d",ans); }