主题:N皇后问题::
N皇后问题::
typedef struct {
int *elem;
}sqlisk;
void main()
{
int i,n,j,p,k,q=0;
sqlisk l;
clrscr();
printf("Input the Queen is disk");
scanf("%d",&n);
l.elem=(int*)malloc((n+1)*sizeof(int));
for(i=1;i<=n+1;i++)
l.elem[i]=1;
i=1;
while(1)
{
if (l.elem[i]<=n)
{
k=1;
while ((k<=i-1)&&(l.elem[i]-l.elem[k])*(abs(l.elem[i]-l.elem[k])-abs(i-k))!=0)
k++;
if (k<=i-1)
{
l.elem[i]++;
continue;
}
i++;
if (i<=n) continue;
clrscr();
q++;
printf("\n\n\n\n\n\n\n");
printf(" The is number %d:\n\n",q);
for (i=1;i<=n;i++)
{
printf("\t\t\t\t");
for (j=1;j<=n;j++)
printf(". ");
for (p=n;p>=(n+1-l.elem[i]);p--)
printf("\b\b");
printf("0\n");
}
getch();
l.elem[n]++;
i=n;
continue;
}
else
{
l.elem[i]=1;
i--;
if (i<1) return;
l.elem[i]++;
continue;
}
}
}
typedef struct {
int *elem;
}sqlisk;
void main()
{
int i,n,j,p,k,q=0;
sqlisk l;
clrscr();
printf("Input the Queen is disk");
scanf("%d",&n);
l.elem=(int*)malloc((n+1)*sizeof(int));
for(i=1;i<=n+1;i++)
l.elem[i]=1;
i=1;
while(1)
{
if (l.elem[i]<=n)
{
k=1;
while ((k<=i-1)&&(l.elem[i]-l.elem[k])*(abs(l.elem[i]-l.elem[k])-abs(i-k))!=0)
k++;
if (k<=i-1)
{
l.elem[i]++;
continue;
}
i++;
if (i<=n) continue;
clrscr();
q++;
printf("\n\n\n\n\n\n\n");
printf(" The is number %d:\n\n",q);
for (i=1;i<=n;i++)
{
printf("\t\t\t\t");
for (j=1;j<=n;j++)
printf(". ");
for (p=n;p>=(n+1-l.elem[i]);p--)
printf("\b\b");
printf("0\n");
}
getch();
l.elem[n]++;
i=n;
continue;
}
else
{
l.elem[i]=1;
i--;
if (i<1) return;
l.elem[i]++;
continue;
}
}
}