怎么最近“皇后问题”这么多人问啊~~~~
刚写的,不太规范,应该可以运行。
其实这些问题应该多看书,看不懂在问!
/****************************************/
#include<stdio.h>
int b[200],n;
int ok(int i)
{
int j,yes1=1,yes2=1;
for (j=1;j<=i-1;j++)
{
if (b[j]==b[i]) return(0);
if (b[j]==j) yes1=0;
if ((b[j]+j)==(n+1)) yes2=0;
}
if (!yes1) {if (b[i]==i) return(0);
else return(1);}
if (!yes2) {if ((b[i]+i)==(n+1)) return(0);
else return(1);}
if ((yes1)&&(yes2)) return(1);
}
void main()
{
int i,j;
long m=0;
printf("input n:");
scanf ("%d",&n);
for (i=1;i<=n;i++) b[i]=1;
i=1;
while (i>=0)
{
i=i+1;
while (b[i]>n)
{
b[i]=1;
i=i-1;
b[i]=b[i]+1;
}
if (!ok(i)) {b[i]=b[i]+1;i=i-1;}
if (i==n) {
for (j=1;j<=n;j++) printf("%5d",b[j]);
printf("\n");
m=m+1;
b[i]=b[i]+1;i=i-1;}
}
printf("total:%ld\n",m);
}