VIS 0 1 2分别竖线和两个对角线,参见对角线(x,y)的和 差关系表
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> using namespace std; const int N=15; int n,vis[5][N<<1],c[N],ans=0; void dfs(int cur){ if(cur==n+1){ ans++; if(ans>3) return; for(int i=1;i<=n;i++) printf("%d ",c[i]); printf("\n"); return; } for(int i=1;i<=n;i++){ if(!vis[0][i]&&!vis[1][i+cur]&&!vis[2][cur-i+n]){ vis[0][i]=vis[1][i+cur]=vis[2][cur-i+n]=1; c[cur]=i; dfs(cur+1); vis[0][i]=vis[1][i+cur]=vis[2][cur-i+n]=0; } } } int main(){ cin>>n; dfs(1);printf("%d",ans); return 0; }
时间: 2024-11-07 06:25:20