八皇后

#include <iostream>

using namespace std;
const int maxn=100;

int n,P[maxn],hashTable[maxn]={false};

void generateP(int index){
    if(index == n+1){
        count++;
        return;
    }
    for(int x=1;x<=n;x++){
        if(hashTable[x]==false){
            bool flag=true;
            for(int pre=1;pre<index;pre++){
                if(abs(index -pre)==abx(x-P[pre])){
                    flag=false;
                    break;
                }
            }
            if(flag){
                P[index]=x;
                hashTable[x]=true;
                generateP(index+1);
                hashTable[x]=false;
            }
        }
    }
}

n皇后解决方案

posted @ 2019-11-09 21:33  殇之弑梦  阅读(120)  评论(0编辑  收藏  举报