多了个空行,暴零了!!求调!!
查看原帖
多了个空行,暴零了!!求调!!
1350521
SimonLan楼主2024/10/12 22:02
#include<bits/stdc++.h>
using namespace std;
bool flag=false;
int ans[1000],N,cnt=0,p=0;
bool check(int row,int col){
	for(int i=1;i<row;i++){
		if(ans[i]==col||abs(col-ans[i])==abs(row-i)){
			return false;
		}
	}	
	return true;
}
void dfs(int dep){
	if(dep>N){
		for(int i=1;i<=N;i++){
		    if(p==3) break;
			cout<<ans[i]<<" ";
		}
        cnt++;
        p++; 
		flag=true;
		cout<<endl;
		return;
	}
	for(int i=1;i<=N;i++){
		if(check(dep,i)==true){
			ans[dep]=i;
			dfs(dep+1);
		}
	}
}
int main(){
	cin>>N;
	dfs(1);
    cout<<cnt;
	if(flag==false){
		cout<<"no solute!";
	}
}

正确输出

2 4 6 1 3 5
3 6 2 5 1 4
4 1 5 2 6 3
4

我的输出:

2 4 6 1 3 5
3 6 2 5 1 4
4 1 5 2 6 3

4
2024/10/12 22:02
加载中...