60pts超时求助
查看原帖
60pts超时求助
724593
Decepticon楼主2022/8/1 13:47

代码:

#include <bits/stdc++.h>
using namespace std;
int vis[20][20],n;
char a[20][20];
int b[20],ans;

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

void dfs(int k){
	if(k > n){
	   ans++;
	   return;
	}
	for(int i = 1; i <= n; i++){
		if(vis[k][i] == 0){
		    if(pd(k,i)){
		   	    b[k] = i;
		   	    dfs(k+1);
		    }
		}
	}
}

int main(){
	cin >> n;
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= n; j++){
			cin >> a[i][j];
			if(a[i][j] == '.') vis[i][j] = 1;
		}
	}
	dfs(1);
	cout << ans << endl;
	return 0;
}
2022/8/1 13:47
加载中...