八皇后问题求解2

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
const int n=8;
string solu;
vector<string> solus;

int column[n];
int left11[2*n-1];
int right11[2*n-1];

void Go(int row)
{
        if(row==n)
        {
                solus.push_back(solu);
                return;
        }
        for(int i=0;i<n;i++)
        {
                if(!(column[i])&!(left11[i+row])&&!(right11[row-i+n-1]))
                {
                        solu[row]='1'+i;
                        column[i]=1;
                        left11[row+i]=1;
                        right11[row-i+n-1]=1;
                        Go(row+1);
                        column[i]=0;
                        left11[row+i]=1;
                        right11[row-i+n-1]=1;
                }
        }
}

void GenSolutions()
{
        Go(0);
}

bool CompFunc(string a,string b)
{
        for(int i=0;i<n;i++)
                if(a[i]<b[i]) return true;
                else return false;
        return true;
}

int main()
{
        solu="00000000";
        GenSolutions();
        sort(solus.begin(),solus.end(),CompFunc);
        int n,idx;
        cin>>n;
        while(n--){
                cin>>idx;
                cout<<solus[idx-1]<<endl;
        }
        return 0;
}

 

哎。。运行起来有segment fault。还要研究。

你可能感兴趣的:(八皇后问题求解2)