#include<stdio.h>
#include<math.h>
int a[9][9],x[9];
int j;
main()
{
clrscr();
eightqueen(1,8);
getch();
return 0;
}
eightqueen(int k,int n)
{
int i;
for(i=1;i<=n;i++)
{
if(place(k,i))
{
x[k]=i;
if(k==n)
{
printf("\n vector X[1---8]\n");
putchar('\n');
for(i=1;i<=n;i++)
printf("%7d",x[i]);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
a[i][j]=0;
for(i=1;i<=n;i++)
a[i][x[i]]=i;
printf("\n Eightqueen placement is \n");
for(i=1;i<=n;i++)
{
puts("\n");
for(j=1;j<=n;j++)
printf("%8d",a[i][j]);
}
}
else
eightqueen(k+1,n);
}
}
return;
}
int place(int k,int i)
{
for(j=1;j<=k-1;j++)
{
if(x[j]==i)
return 0;
if(abs(x[j]-i)==abs(j-k))
return 0;
}
return 1;
}
#include<math.h>
eightqueen(int,int);
int place(int,int);int a[9][9],x[9];
int j;
main()
{
clrscr();
eightqueen(1,8);
getch();
return 0;
}
eightqueen(int k,int n)
{
int i;
for(i=1;i<=n;i++)
{
if(place(k,i))
{
x[k]=i;
if(k==n)
{
printf("\n vector X[1---8]\n");
putchar('\n');
for(i=1;i<=n;i++)
printf("%7d",x[i]);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
a[i][j]=0;
for(i=1;i<=n;i++)
a[i][x[i]]=i;
printf("\n Eightqueen placement is \n");
for(i=1;i<=n;i++)
{
puts("\n");
for(j=1;j<=n;j++)
printf("%8d",a[i][j]);
}
}
else
eightqueen(k+1,n);
}
}
return;
}
int place(int k,int i)
{
for(j=1;j<=k-1;j++)
{
if(x[j]==i)
return 0;
if(abs(x[j]-i)==abs(j-k))
return 0;
}
return 1;
}
No comments:
Post a Comment