Author: Filip Tvrzsky
Date: 10:44:15 04/10/03
Go up one level in this thread
On April 09, 2003 at 23:56:54, Russell Reagan wrote:
>So...who wants to explain how this works? I don't know how it works, but it
>finds all of the solutions to the 8 queens problem.
>
>#include <stdio.h>
>int v,i,j,k,l,s,a[99];
>void main(void){
>for(s=8;*a-s;v=a[j*=v]-a[i],k=i<s,j+=(v=j<s&&(!k&&!!printf(2+"\n\n%c"-(!l<<!j),"
>#Q"[l^v?(l^j)&1:2])&&++l||a[i]<s&&v&&v-i+j&&v+i-j))&&!(l%=s),v||(i==j?a[i+=k]=0:++a[i])>=s*k&&++a[--i]);
> printf("\n\n");
>}
For those people who want speculate about how it works there is my transcription
to "normal" code. The funcionality should be almost the same except the printing
white and black squares (a little simplification ...).
#include <stdio.h>
int v = 0, i = 0, j = 0, l = 0, a[99];
int main(void)
{
while(a[0] != 8)
{
v = a[j] - a[i];
if (j < 8)
{
if (i == 8)
{
if (l == 0)
{
putchar('\n');
if (j == 0) putchar('\n');
}
if (l == v) putchar('Q');
else putchar('#');
v = ++l || a[i] < 8 ? 1 : 0;
}
else
{
if (a[i] < 8 && v != 0 && v != i - j && v != j - i) v = 1;
else v = 0;
}
}
if (v != 0)
{
l = l % 8;
if (l == 0) j++;
}
else
{
if (i < 8)
{
if (i == j) a[++i] = 0;
else a[i]++;
if (a[i] >= 8) ++a[--i];
}
else
{
if (i == j) a[i] = 0;
else a[i]++;
if (a[i] >= 0) ++a[--i];
}
j = 0;
}
}
}
This page took 0 seconds to execute
Last modified: Thu, 15 Apr 21 08:11:13 -0700
Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.