提交时间:2024-04-10 13:38:49
运行 ID: 142847
#include<bits/stdc++.h> using namespace std; int n,res[25][25],o; bool col[25],line1[50],line2[50]; void dfs(int x){ if(x>n){ if(o<=2){ for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)if(res[i][j]==1)cout<<j<<" "; cout<<endl; } o++; return; } for(int y=1;y<=n;y++) if(col[y]==0&&line1[x-y+n]==0&&line2[x+y]==0){ col[y]=line1[x-y+n]=line2[x+y]=1; res[x][y]=1; dfs(x+1); res[x][y]=0; col[y]=line1[x-y+n]=line2[x+y]=0; } } int main(){ cin>>n; dfs(1); cout<<o; return 0; }