Hello all!
I´m doing the n-queens prolbem in an recursive manner with a twist. The twist i the possibility to chose to place 2-8 queens in a 8x8 board.I have problem with storing the rows properly and print them at the right time inside the recursion,please feel free to give me some tips to proceed.

include <stdio.h>
define TRUE 1
define FALSE 0

int *board;

int main()
{
board=(int*)calloc(8+1,sizeof(int));
board++;
//here goes the userinput no of queens doesn´t matter in this code
int col;
int queens=2;//example from userinput
for(col=1;col<=8;col++)
     placeQueens(1,col,queens);
}
void placeQueens(int row,int col,int queens)
{
       int i;
        for(i=1;i<row;i++)
       {
        if((board[i] == col) || ((row+col)==(i+board[i]))||((row-col)==(i-board[i])))
        {
            check= FALSE;
        }
        else
         check=TRUE;
     }
      if(check==TRUE)
       {
         board[row]=col;
        
        if(row==8)
        {
            
            for(i=1;i<=queens;i++)
            printf("(%d,%d)",i,board[i]);
                    
           printf("\n");
        }            
        else
        {
            for(i=1;i<=8;i++)
            placeQueens(row+1,i);
        }
    }
        
}

this works fine for the first to rows which prints (1,1)(2,3) to (1,8)(2,6) but thats it.The aim is to print the first row first col (1,1) to (8,7) and the (1,2) to (8,8) ie find all solutions with 2 queens on table..

Recommended Answers

All 3 Replies

Why use dynamic allocattion to define an 8x8 array? Just define board as int board[8][8] .

Create a function to display the board and just call it.

Thanks, I will do it with this approach..

if u have correct source of this problem give it me plz

commented: Do it yourself you sponger / cheater / leech -3
commented: / Dumbass. -1
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.