aclark17 0 Newbie Poster

Ive been having some trouble getting my Nqueens program to run for n>=9.

I use a function bool PlaceQueens(c) to recursivley find all solutions using a backtracking algorithim , it seems to return neither true or false.

For n=9, the function PlaceQueens exits without returning a value during call 4296.

Here is my code:

static int count=0;
bool ChessBoard::PlaceQueens(int c){

	int r=0;
	
	 if(processcol(r,c)){
		if(c==n-1){
			c--;
			solutions++;}
		c++;}
	else
	c--;
	 if(c<0){
		
		if(solutions==0)
			return false;
		else{
			cout<<"solutions: "<<solutions<<endl;
			return true;}
	}
	 else{

	count++;
	
	if(count>4293){
		cout<<"call: "<<count<<endl;
	
		cout<<*this<<endl;}
	return PlaceQueens(c);}
}

Here is whats happening on the board between calls 4293 and 4296:

//program output

Queen Placement

Enter size of board (positive integer): 9

Placing queens on a 9 x 9 chess board...
call: 4294

-----------------
| | | | | | | | | Q |
-----------------
| | | | Q | | | | | |
-----------------
| Q | | | | | | | | |
-----------------
| | | | | Q | | | | |
-----------------
| | | | | | | | Q | |
-----------------
| | Q | | | | | | | |
-----------------
| | | | | | | Q | | |
-----------------
| | | Q | | | | | | |
-----------------
| | | | | | Q | | | |
-----------------

call: 4295

-----------------
| | | | | | | | | |
-----------------
| | | | Q | | | | | |
-----------------
| Q | | | | | | | | |
-----------------
| | | | | Q | | | | |
-----------------
| | | | | | | | Q | |
-----------------
| | Q | | | | | | | |
-----------------
| | | | | | | Q | | |
-----------------
| | | Q | | | | | | |
-----------------
| | | | | | Q | | | |
-----------------

call: Press any key to continue . . .

//end output

It looks like its exiting just before outputing the value of count. At this point it has found 76 solutions. Total number of solutions for n=9 is 92. It processes n<9 without a problem.I have included the header main and function definitions a attachments to this post.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.