#include<stdio.h>
#include<math.h>
#include<malloc.h>
#define Stacksize 8
#define STACK_INIT_SIZE 100
int solution=0;
typedef struct
{
int *base;
int *top;
int stacksize;
}Sqstack;
void Push(Sqstack Q,int e);
void Pop(Sqstack Q,int e);
void Initstack(Sqstack Q);
void Placequeen(int c,Sqstack Q);
int Judgement(int i,int c,Sqstack Q);
void output(Sqstack Q,int solution);
int main()
{
Sqstack Q;
Initstack(Q);
printf("There are %d solutions in total.",solution);
Placequeen(0,Q);
return 0;
}
void Push(Sqstack Q,int e)
{
Q.top=Q.base+Q.stacksize;
*Q.top++=e;
}
void Pop(Sqstack Q,int e)
{
e=*--Q.top;
}
void Initstack(Sqstack Q)
{
Q.base=(int *)malloc(STACK_INIT_SIZE * sizeof(int));
Q.top=Q.base;
Q.stacksize=STACK_INIT_SIZE;
}
void Placequeen(int c,Sqstack Q)
{
int i=0;
for(i=0;i<Stacksize;i++)
{
Push(Q,i);
if(Judgement(i,c,Q))
{
if(c<Stacksize)
Placequeen(c+1,Q);
else
{
solution++;
output(Q,solution);
}
}
Pop(Q,i);
}
}
int Judgement(int i,int c,Sqstack Q)
{
int j;
for(j=0;j<i;j++)
{
if((i==*(Q.base+j))||(abs(c-j)==abs(i-*(Q.base+j))))
{
return 0;
break;
}
else return 1;
}
}
void output(Sqstack Q,int solution)
{
printf("NO. %d",solution);
int e;
int i;
int j;
int a[Stacksize][Stacksize];
for(i=0;i<Stacksize;i++)
{
Pop(Q,e);
for(j=0;j<Stacksize;j++)
{
if(j==e) a[i][j]=1;
else a[i][j]=0;
printf("%d ",a[i][j]);
}
printf("\n");
}
}