0% found this document useful (0 votes)
5 views3 pages

N Queens Program

Uploaded by

Shambhavi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

N Queens Program

Uploaded by

Shambhavi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

N Queen's problem using Backtracking

Design and implement C/C++ Program for N Queen's problem using Backtracking.

#include<stdio.h>

#include<math.h>

int x[10];

void main( )

int k, i, j, n, count=1;

int place(int);

printf("Enter the number of Queens\n");

scanf("%d",&n);

if(n==0 || n==2 || n==3 )

printf("No Solution");

else

k=1;

x[1]=0;

while(k)

x[k]= x[k]+1;

while( ( x[k] <=n) && (!place(k) ) )

x[k]= x[k]+1;

if( x[k]<=n )

if(k==n)
{

printf("Solution %d \n",count++);

for ( i=1; i<=n; i++ )

for ( j=1; j<x[i]; j++ )

printf(" * ");

printf(" Q" );

for ( j=x[i]+1; j<=n; j++ )

printf(" * ");

printf(" \n" );

else

k=k+1;

x[k]=0;

else

k=k-1;

}
int place( int p)

int i;

for ( i=1; i<=(p-1); i++ )

if((x[i]==x[p]) || ((abs(x[i]-x[p]))==(abs(i-p))))

return 0;

return 1;

OUTPUT:

You might also like