# Nqueens n>=9 program terminates

Discussion in 'C' started by namehere, Jul 2, 2008.

1. ### namehereBanned

Joined:
Jul 2, 2008
Messages:
2
0
Trophy Points:
0
//works great for n<9 cant figure out why it doesnt work for n>=9//

Code:
```#include <iostream>
#include <math.h>
using namespace std;

class board
{
public:

board(int n)
{
this->size=n;
list=new int[size];
solutions=0;
for(int i=0;i<size;i++)
list[i]=-1;

}
~board()
{
delete list;
}

int solve(int col);
bool process(int col,int left);
void printboard();
private:
int* list;
int size;
int solutions;
};

int board::solve(int col)
{

if(col>=0){
if(process(col,col-1)){
if(col==size-1)
solutions++;
else
col++;}
else
col--;
return solve(col);}
return solutions;

}
bool board::process(int col,int left)
{

list[col]++;

for(list[col]=list[col];list[col]<size;list[col]++){
if(list[col]!=list[left]&&(list[col]-col)!=(list[left]-left)&&(list[col]+col)!=(list[left]+left)){
for(left=left-1;left>=0;left--){
if(list[col]==list[left]||(list[col]-col)==(list[left]-left)||(list[col]+col)==(list[left]+left)){
left=col-1;
break;}}
if(left<0)
break;
}}

if(list[col]>=size){
list[col]=-1;
return false;}
return true;

}
void board::printboard()
{

int a,b,c;

for(a=0;a<size;a++)
{
for(b=0;b<size;b++)
cout<<"-----";
cout<<endl;
for(c=0;c<size;c++){
cout<<"|  ";
if(list[c]==a)
cout<<"Q ";
else
cout<<"  ";}

cout<<'|'<<endl;}
for(a=0;a<size;a++)
cout<<"-----";

}

void main()
{
int n;
cout<<"Enter board size: ";
cin>>n;
board b(n);
cout<<b.solve(0)<<endl;
cout<<endl;

}```

Last edited by a moderator: Jul 2, 2008