Dominating n-queen puzzle

I have solved the generics n-queen problem, but I am looking to for an algorthim to solve the 5 queen dominations problem where you have to dominatate all squares in the board with 5 queens with a 8x8 board. I want to print all possible solutions (4860 solutions available in this problem) to this, but my code won't do it.

#include <iostream>

using namespace std;

int i;
bool q;
bool a[9];
bool b[17];
bool c[17];
int x[9];

void clear(){
    for(int i = 1;i<=8;i++)
        a[i] = true;
    for(int i = 2;i<=16;i++)
        b[i] = true;
    for(int i = -7;i<=7;i++)
        c[i] = true;
}

void printboard(){
    cout << "Berhasil!!" << endl;
    for(int j=1;j<=8;j++){
        for(i=1;i<=8;i++){
            if(j == x[i])
                cout<<"K ";
            else
                cout << "* ";
        }
        cout << "\n";
    }
}

bool isfull(){
    for(int i = 1;i<=8;i++)
        if(a[i] == false)
            return false;
    for(int i = 2;i<=16;i++)
        if(b[i] == false)
            return false;
    for(int i = -7;i<=7;i++)
        if(c[i] == false)
            return false;
}

void trys(int i,bool &q,int step){
    int j;
    j = 0;
    do{
        j+=1;
        q=false;
        x[i] = step;
        a[j] = false;
        b[i+j] = false;
        c[i-j] = false;
        if(step<5){
            trys(i+1,q,step+1);
            if(!q){
                a[j] = true;
                b[i+j] = true;
                c[i-j] = true;
            }
        }else{
            if(isfull){
                printboard();
                clear();
                q = true;
            }
        }
    }while(true);
}

int main(){

        clear();
//      for(int i = 1;i<=8;i++)
//          x[i] = true;
        trys(i,q,0);
//      if(q){
//          cout << endl;
//          printboard();   
//      }
}

this code I take from Niklaus Wirth and modified a little bit.

Upvotes: 0

Views: 80

Answers (0)

Related Questions