CodeVS 1295 N皇后问题 题解

#include 
using namespace std;

bool a[13+2], b[26+2], c[26+2];
int N, num;

void checker(int n){
	if(n>N){
		++num;
		return;
	}
	for(int i = 1; i<=N; ++i){
        if(!a[i] && !b[i+n] && !c[i-n+12]){
		    a[i] = b[i+n] = c[i-n+12] = true;
		    checker(n+1);
		    a[i] = b[i+n] = c[i-n+12] = false;
		}
	}
}

int main(){
    scanf("%d",&N);
	checker(1);
	printf("%d\n", num);
	return 0;
}

你可能感兴趣的:(深度优先搜索)