## Sample C Program To Implement 8 Queens Problem Using Arrays, Pointers & Functions.

ALGORITHM:

STEP 1: Define the functions that are to be used.
STEP 2: Assign a constant value of 8 to QUEENNO.
STEP 3: Call the function placequeen(0, x).
STEP 4: Print the message “End”.
STEP 5: Stop the program.

FUNCTION VOID PLACEQUEEN(int k, int *x)

STEP 1: Declare local variables
STEP 2: Set a for loop for i.
for(i = 0; i < 8; i++)
STEP 3: Check for result of function canbeplaced(k, I, x).
STEP 4: If it is 1 then assign the value of i to x[k].
x[k] = i;
STEP 5: Check if k is equal to 7 if show call the function showboard(x).

STEP 6: Read the value of ch from the user.
STEP 7: If the value is equal to n or N then exit.
STEP 8: Check whether k is less than 7 if so then call the function placequeen(k + 1, x).

FUNCTION INT CANBEPLACED(int k, int i, int *x)

STEP 1: Declare the local variables.
STEP 2: Set a for loop for j.
for(j = 0; j < k; j++)
STEP 3: Check for the following condition
if( (abs(j - k) == abs(x[j] - i) || (x[j] == i) ) )
STEP 4: If its true return 0 else return 1.

FUNCTION VOID SHOWBOARD(int *x)

STEP 1: Declare the local variables.
STEP 2: Set all the display style in printf function.
STEP 3: Set a for loop for i to print 1,2,..8 vertically and horizontally.
for(i = 0; i < 8; i++)
STEP 4: Set another for loop for j and check whether j is equal to x[i].
for(j = 0; j < 8; j++)
STEP 5: If so then print Q else print -.

SAMPLE PROGRAM:

#include<stdio.h>
#include<conio.h>
#define QUEENNO 8

void placequeen(int, int*);
int canbeplaced(int, int, int*);
void showboard(int*);

void main()
{

int x[QUEENNO], i;
clrscr();

printf("The 8 queens problem");
placequeen(0, x);

printf("End");
getch();

}

void placequeen(int k, int *x)
{

int i, j;
char ch;

for(i = 0; i < 8; i++)
{
if(canbeplaced(k, i, x))
{
x[k] = i;
if(k == 7)
{
showboard(x);
printf(" Want to see more?[n->stop, any-> continue]: ");
scanf(" %c ", &ch);
if(ch == 'n' || ch == 'N')
exit(0);
}
if(k < 7)
placequeen(k + 1, x);
}
}

}

int canbeplaced(int k, int i, int *x)
{

int j;
for(j = 0; j < k; j++)
{
if((abs(j - k) == abs(x[j] - i)) || (x[j] == i)))
return 0;
}

return 1;

}

void showboard(int *x)
{

int i, j;
printf(" \n----------------------------------------------\n ");
printf(" ");
for(i = 0; i < 8; i++)
{
printf(" %d ",(i + 1));
printf(" ");
}
for(i = 0; I < 8; i++)
{
printf(" \n \n %d ",(i + 1));
for(j = 0; j < 8; j++)
{
if(j == x[i])
printf(" Q ");
else
printf(" - ");
printf(" ");
}
printf(" ");
}
printf(" \n---------------------------------------------- ");
}

OUTPUT:

The 8 Queens’ Problem
------------------
1 2 3 4 5 6 7 8
1 Q - - - - - - -
2 - - Q - - - - -
3 - - - - Q - - -
4 - - - - - - Q -
5 – Q - - - - - -
6 - - - Q - - - -
7 - - - - - Q - -
8 - - - - - - - Q
------------------ Want to see more?[n->stop, any-> continue]: N

NOTE:

If you liked the post/article or if you think this post/article helped you in any way, then please show your appreciation by filling up at least one form from the options given below:

-- Click Here To Win Rewards Each Time When You Access Internet.

-- Click Here To Get Up To 40% Off On Your Favorite Books.

-- Click Here To Apply To Multiple Colleges With A Single Application.