代码求调
查看原帖
代码求调
571428
ZoeZhang楼主2024/7/28 09:34

P1219 [USACO1.5] 八皇后 Checker Challenge

rt

#include<iostream>
using namespace std;

bool book[30];
bool b_1[30];//x+y
bool b_2[30];//x-y
int n;
long long ans=0;
int now[15];
int an_string[4][15];

void dfs(int row){
	for(int i=1;i<=n;i++){
		if(book[i]==true||b_1[row+i]==true||b_2[i-row]==true)continue;
		if(row==n){
			ans++;
			now[row]=i;
			if(ans<=3){
				for(int j=1;j<=n;j++)
					an_string[ans][j]=now[j];
			}
			return;
		}
		book[i]=true;b_1[row+i]=true;b_2[i-row]=true;
		now[row]=i;
		dfs(row+1);
		book[i]=false;b_1[row+i]=false;b_2[i-row]=false;
	}
	return;
}
int main(){
	cin>>n;
	for(int i=1;i<=29;i++)
	{
		book[i]=false;
		b_1[i]=false;
		b_2[i]=false;
	}
		
	for(int i=1;i<=n;i++){
		book[i]=true;
		b_1[i+1]=true;
		b_2[i-1]=true;
		now[1]=i;
		dfs(2);
		book[i]=false;
		b_1[i+1]=false;
		b_2[i-1]=false;
	}
	for(int i=1;i<=3;i++){
		for(int j=1;j<=n;j++)
			cout<<an_string[i][j]<<" ";
		cout<<endl;
	}
	cout<<ans;
	return 0;
}
2024/7/28 09:34
加载中...