全WA求助
查看原帖
全WA求助
724593
Decepticon楼主2022/7/14 17:32

不知道错哪了???

#include <bits/stdc++.h>
using namespace std;
int n,ans;
int b[15];
bool book[15] = {false};

bool pd(int x,int y){
	for(int i = 1; i < x; i++){
		if(y == i) return false;
		if(i-b[i] == x-y) return false;
		if(i+b[i] == x+y) return false;
	}
	return true;
}

void dfs(int k){
	if(k > n){
	    ans++;
	    for(int i = 1; i <= n; i++)
	       cout << b[i] << " ";
	    cout << endl;
	    return;
	}
	for(int i = 1; i <= n; i++){
		if(pd(k,i) && !book[i]){
		   b[k] = i;
		   book[i] = true;
		   dfs(k+1);
		   book[i] = false;
		}
	}
}

int main(){
	cin >> n;
	dfs(1);
	cout << ans << endl;
	return 0;
}
2022/7/14 17:32
加载中...