为啥我这个跑不出来
查看原帖
为啥我这个跑不出来
591458
qinziao楼主2024/8/30 19:45
#include<bits/stdc++.h>
using namespace std;
int n;
bool lie[15],xieh[30],xie1[15],xie2[15];
stack<int>q;
void getout(){
	while(q.size()){
		printf("%d ",q.top());
		q.pop();
	}
}
bool dfs(int x){
	if(x==n+1) return 1;
	
	for(int j=1;j<=n;j++){
		
		if(!lie[j]&&!xieh[x+j]){
				
			lie[j]=1;
			xieh[x+j]=1;
				
				if(x>j)
					if(!xie1[x-j]){
						
							xie1[x-j]=1;
							
						if(dfs(x+1)){
							q.push(j);
							if(x==1){
								getout();
							}
							return 1;
						}
						
							xie1[x-j]=0;
							
					}
				
				else
					if(!xie2[j-x]){
						
							xie2[j-x]=1;
							
						if(dfs(x+1)){
							q.push(j);
							if(x==1){
								getout();
							}
							return 1;
						}
						
							xie2[j-x]=0;
							
					}
				
			lie[j]=0;
			xieh[x+j]=0;
			
		}
	}
	
	return 0;
}
int main(){
	cin>>n;
	dfs(1);
	return 0;
}
2024/8/30 19:45
加载中...