cpulxb @ 2024-01-24 22:30:43
by zengziqvan @ 2024-01-24 23:00:15
请放上您的代码 @cpulxb
by zengziqvan @ 2024-01-24 23:01:35
在输入栏上面有一栏插入代码
by cpulxb @ 2024-01-24 23:03:27
//刚刚又看了看,不知道我下面的理解对不都对
#include<bits/stdc++.h>
using namespace std;
int n;
int vis1[100];
int vis2[100];
int vis3[100];
int chess[13];
int sum;//多少种方案
void dfs(int s)//s代表第s行的位置
{
if(s>n)//s>n代表已经填完第s行了,不能写s==n,因为此时的第s行还没有
//通过for循环确定其位置
{
sum++;
if (sum<= 3)
{
for (int i =1; i <=n; i++)
cout << chess[i]<<" ";
cout << endl;
}
return;
}
for (int j =1; j<=n; j++)
{
if (vis1[j] == 0 && vis2[j + s] == 0 && vis3[s - j +n] == 0)
{
chess[s] = j;
vis1[j] = 1; vis2[j + s] = 1; vis3[s - j +n] = 1;
dfs(s + 1);
vis1[j] = 0; vis2[j + s] = 0; vis3[s - j +n] = 0;
}
}
}
int main()
{
cin >> n;
dfs(1);
cout << sum;
return 0;
}
by KongY11 @ 2024-01-25 17:42:51
是的 s==n的时候n还没放