#include<stdio.h>
#include<math.h>
int x[10],c=0;
void nqueens(int,int);
int place(int,int);
main()
{
int n,i;
printf("enter the no of qeens on the board");
scanf("%d",&n);
nqueens(1,n);
printf("%d",c);
}
void nqueens(int k,int n)
{
int i;
for(i=1;i<=n;i++)
{
if(place(k,i))
{
x[k]=i;
if(k==n)
{
c++;
for(i=1;i<=n;i++)
printf("%d",x[i]);
printf("\n");
}
else
nqueens(k+1,n);
}
}
}
int place(int k,int i)
{
int j;
for(j=1;j<=k-1;j++)
if((x[j]==i)||(abs(x[j]-i)==abs(j-k)))
return 0;
return 1;
}
#include<math.h>
int x[10],c=0;
void nqueens(int,int);
int place(int,int);
main()
{
int n,i;
printf("enter the no of qeens on the board");
scanf("%d",&n);
nqueens(1,n);
printf("%d",c);
}
void nqueens(int k,int n)
{
int i;
for(i=1;i<=n;i++)
{
if(place(k,i))
{
x[k]=i;
if(k==n)
{
c++;
for(i=1;i<=n;i++)
printf("%d",x[i]);
printf("\n");
}
else
nqueens(k+1,n);
}
}
}
int place(int k,int i)
{
int j;
for(j=1;j<=k-1;j++)
if((x[j]==i)||(abs(x[j]-i)==abs(j-k)))
return 0;
return 1;
}
No comments:
Post a Comment