
奋斗改变一切!!
#include<stdio.h>
#include<math.h>
const int NUM=8;
char model[]="12345678";
char solve[]="???????? ";
int main()
{
void queen(int);
printf("the 92 solutions of Eight Queen's problem: \n");
queen(0);
printf("\n");
return 0;
}
void queen(int n)
{ static tot=0;
int i=NUM;
char ch,*p,*q;
if(n<i)
for(p=model;i>0;i--,p++)
{
int j;
q=solve;
ch=*p;
if(ch==0)continue;
for(j=n;j>0;j--)
if(abs(*q++-ch)==j)break;
if(j>0)continue;
*q=ch;
*p=j;
queen(n+1);
*p=ch;
}
else
{
printf(solve);
}
}
#include <math.h>
#include <stdio.h>
#include <conio.h>
#define N 8
int local[N];
void show_result()
{
int i;
for(i=0; i<N; i++)
printf("%d%d ",i,local[i]);
printf("\n");
}
int check_cross(int n)
{
int i;
for (i=0; i<n; i++)
{
if (local[i] == local[n] || (n-i) == abs(local[i]-local[n]))
return 1;
}
return 0;
}
void put_chess(int n)
{
int i;
for (i=0; i<N; i++)
{
local[n] = i;
if (!check_cross(n))
{
if (n == N-1) show_result();
else put_chess(n+1);
}
}
}
int main(void)
{
put_chess(0);
getch();
return 0;
}