DFS写挂求助
查看原帖
DFS写挂求助
809165
The_Wandering_Earth楼主2023/2/15 13:16

rt,只会输出0,全部WA了

#include<bits/stdc++.h>
using namespace std;
int n,a[105],b[105],c[105],d[105],ans;
void print(){
	ans++;
	if(ans<=3){
		for(int j=1;j<=n;j++)cout<<a[j]<<" ";
		cout<<endl;
	}
}
void dfs(int i){
	if(i>n){
		print();
		return;
	}
	else{
		for(int j=1;j<=n;j++){
			if((!b[j])&&(!c[i+j])&&(d[i-j+n])){
				a[i]=j,b[j]=1,c[i+j]=1,d[i-j+n]=1;
				dfs(i+1);
				b[j]=0,c[i+j]=0,d[i-j+n]=0;
			}
		}
	}
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	cin>>n;
	dfs(1);
	cout<<ans;
	return 0;
}
2023/2/15 13:16
加载中...