wozhenbanghahei @ 2023-04-29 11:44:35
样例过了,结果全部WA,麻烦大佬看看我这段代码。
#include<bits/stdc++.h>
using namespace std;
int n,ans;
int a[105],book[105],c[105],d[105];
void dfs(int step)
{
if(step>n)
{
ans++;
if(ans<=3)
{
for(int i=1;i<n;i++) cout<<a[i];
cout<<a[n]<<endl;
}
return;
}
for(int i=1;i<=n;i++)
{
if(!book[i] && !c[i+step] && !d[i-step+n])
{
book[i]=1;
c[i+step]=1;
d[i-step+n]=1;
a[step]=i;
dfs(step+1);
book[i]=0;
c[i+step]=0;
d[i-step+n]=0;
}
}
return;
}
int main()
{
cin>>n;
dfs(1);
cout<<ans;
return 0;
}
by XEZ323 @ 2023-05-19 17:16:06
忘记打出空格了,看一下样例。
#include<bits/stdc++.h>
using namespace std;
int n,ans;
int a[105],book[105],c[105],d[105];
void dfs(int step)
{
if(step>n)
{
ans++;
if(ans<=3)
{
for(int i=1;i<n;i++) cout<<a[i]<<" ";
cout<<a[n]<<endl;
}
return;
}
for(int i=1;i<=n;i++)
{
if(!book[i] && !c[i+step] && !d[i-step+n])
{
book[i]=1;
c[i+step]=1;
d[i-step+n]=1;
a[step]=i;
dfs(step+1);
book[i]=0;
c[i+step]=0;
d[i-step+n]=0;
}
}
return;
}
int main()
{
cin>>n;
dfs(1);
cout<<ans;
return 0;
}
这样应该就可以了。实测AC