48分求助
查看原帖
48分求助
369767
ANGELA2002楼主2021/6/27 19:43
#include<bits/stdc++.h>
using namespace std;
int m[10]={0};
int s=0;
int n;
void print_answer(){
	if(s<=2){
		for(int i=1;i<=n;i++)cout<<m[i]<<" ";
		cout<<endl;
	}
	s++;
}
bool judge(int h,int x){
	bool flag=true;
	for(int i=1;flag&&i<h;i++){
		if(m[i]==x)flag=false;
		if(abs(m[i]-x)==abs(i-h))flag=false;
	}
	return flag;
}
void bfs(int h){
	if(h>n){
		print_answer();
		return;
	}
	else{
		for(int i=1;i<=n;i++){
			if(judge(h,i)){
				m[h]=i;
				bfs(h+1);
			}
		}
	}
}
int main(){
	cin>>n;
	bfs(1);
	cout<<s;
	return 0;
}
2021/6/27 19:43
加载中...