#8TLE了,求助
查看原帖
#8TLE了,求助
515990
Editzed楼主2021/8/21 15:00
#include<bits/stdc++.h>
using namespace std;
const int N=100;
int a[N];
int tot,n;
bool check(int x,int y){
	for(int i=1;i<=x;i++){
		if(a[i]==y) return false;
		if(a[i]+i==x+y) return false;
		if(i-a[i]==x-y) return false;
	}
	return true;
}
void dfs(int r){//第r行皇后放于何处 
	if(r==n+1){
		if(tot<3){
			for(int i=1;i<=n;i++)
		        cout<<a[i]<<" ";
		    cout<<endl; 
		}
		
		tot++;
		return;
	}
	for(int i=1;i<=n;i++){
		if(check(r,i)){
			a[r]=i;
			dfs(r+1);
			a[r]=0;
		}
	}
}
int main(){
	cin>>n;
	dfs(1);
	cout<<tot;
	return 0;
}
2021/8/21 15:00
加载中...