xycsc23 @ 2024-09-06 09:13:38
#include<bits/stdc++.h>
using namespace std;
bool l[100], z[100], y[100];
int n, s=0;
void dfs(int j, string a){
if(j==n){
if(s<3)
cout<<a<<'\n';
s++;
return;
}
for(int i=0;i<n;i++){
if(!l[i] && !z[i+j] && !y[i-j+n]){
l[i]=true;
z[i+j]=true;
y[i-j+n]=true;
dfs(j+1, a+char(i+'1')+' ');
l[i]=!true;
z[i+j]=!true;
y[i-j+n]=!true;
}
}
return;
}
int main(){
cin>>n;
dfs(0, "");
cout<<s;
return 0;
}
评测记录
by lizexuanaibiancheng @ 2024-09-06 09:39:39
当第
by lizexuanaibiancheng @ 2024-09-06 09:41:08
当你输入
by hehe_666 @ 2024-09-06 09:55:07
@13548940890xyc 你可以看我的提交
by hehe_666 @ 2024-09-06 09:58:06
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
int a[55], n, k, cnt;
bool hang[55], lie[55], d1[55], d2[55];
void dfs(int step)
{
if(step > n)
{
if(cnt <= 2)
{
for(int i = 1; i <= n; i++)
{
cout << a[i] << " ";
}
cout << endl;
}
cnt++;
}
for(int i = 1; i <= n; i++)
{
int x = step, y = i;
if(hang[x] || lie[y] || d1[x - y + n] || d2[x + y]) continue;
hang[x] = lie[y] = d1[x - y + n] = d2[x + y] = true;
a[step] = i;
dfs(step + 1);
hang[x] = lie[y] = d1[x - y + n] = d2[x + y] = false;
}
}
int main()
{
cin >> n;
k = 3;
dfs(1);
cout << cnt;
return 0;
}
@13548940890xyc