提交时间:2023-12-01 13:40:53
运行 ID: 113147
#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<<endl; return 0; }