Public domain
#include <stdio.h>
#define MAX 8
int a[MAX];
double count = 0;
main()
{
permute(0);
printf("\nCount = %.0f\n", count);
return 0;
}
permute(int i)
{
for(a[i] = 0; a[i] < MAX; a[i]++)
{
if(i > 0) {
int j;
for(j = i-1; j >= 0; j--)
if(cross_check(i,j,a[i],a[j]))
break;
if(j != -1) continue;
}
if(i + 1 < MAX) {
permute(i+1);
} else {
count++;
show_case();
}
}
}
cross_check(int x,int y,int a,int b)
{
int result = 0;
if(a==b || abs(a-b)==abs(x-y)) result=1;
return result;
}
show_case()
{
int i,j;
printf("No: %.0f \nResult: ", count);
for(i=0; i<MAX; i++)
printf("%d", a[i]);
printf("\n");
for(j = 0; j < MAX; j++)
{
for(i = 0; i < MAX*2+1; i++)
printf("---");
printf("\n");
for(i=0; i < MAX*2+1; i++)
if(a[j]*2+1 == i)
printf("QQQ");
else if(i%2 == 0)
printf(" | ");
else
printf(" ");
printf("\n");
}
for(i = 0; i < MAX*2+1; i++)
printf("---");
printf("\n");
getchar();
}
BY: Pejman Moghadam
TAG: c, queens
DATE: 2011-12-03 00:37:53