用深搜做的
查看原帖
用深搜做的
1233392
RY111楼主2024/8/4 15:34

不知道为啥和想象的输出不同,模拟了一下2,感觉应该输出是对的,结果还是不对

#include<bits/stdc++.h>
using namespace std;
const int maxn=15;
bool st[maxn];
int cnt=0;
int n;
vector<int> a[1000];


void dfs(int x)//枚举到第几行
{

	if(x==n)
	{
		for(auto it:a[cnt])
		{
			cout<<it<<" ";
		}

//		cout<<endl;
		cnt++;
		return ;
	}

	for(int i=1; i<=n; i++)//放到哪一列
	{
		if(st[i]==0)
		{
			st[i]=1;
			a[cnt].push_back(i);
			dfs(x+1);
			st[i]=0;

		}

	}
}
int main()
{

	cin>>n;
	dfs(1);
	cout<<cnt;
}
2024/8/4 15:34
加载中...