:rolleyes: , but you failed to write the function to the appropriate declaration of
BOOL Solution(unsigned hyper solution_to_check);
Printable View
:rolleyes: , but you failed to write the function to the appropriate declaration of
BOOL Solution(unsigned hyper solution_to_check);
Unsigned hyper? What's a hyper? :-) An unsigned long will do just fine; you only need 24 bits to represent solutions somewhat naively, a long long if you represent very naively.
> Unsigned hyper? What's a hyper? :-)
If you type "unsigned hyper" into google, you can find this: http://www.freesoft.org/CIE/RFC/1832/8.htm
>> but you failed to write the function to the appropriate declaration of
BOOL Solution(unsigned hyper solution_to_check);
When did you ever say that that was the appropriate declaration?
Brute force (which is still boring):Code:BOOL Solution(unsigned hyper solution_to_check)
{
int i, j, Queens[8];
for (i = 0; i < 8; i++)
{
Queens[i] = ((solution_to_check >> (i * 3)) & 7) + (i * 8);
}
for (i = 0; i < 8; i++)
{
for (j = 0; j < i; j++)
{
// Same row
if ((Queens[i] / 8) == (Queens[j] / 8))
return false;
// Same column
if (((Queens[i] - Queens[j]) % 8) == 0)
return false;
// Diagonally
if (abs((Queens[i] / 8) - (Queens[j] / 8)) == abs((Queens[i] % 8) - (Queens[j] % 8)))
return false;
}
for (j = i + 1; j < 8; j++)
{
// Same row
if ((Queens[i] / 8) == (Queens[j] / 8))
return false;
// Same column
if (((Queens[i] - Queens[j]) % 8) == 0)
return false;
// Diagonally
if (abs((Queens[i] / 8) - (Queens[j] / 8)) == abs((Queens[i] % 8) - (Queens[j] % 8)))
return false;
}
}
return true;
}
Code:for (unsigned long x = 0; x < 16777216; x++)
{
if (Solution(x))
{
for (i = 0; i < 8; i++)
printf("(%d,%d) ", i + 1, ((x >> (3 * i)) & 7) + 1);
printf("\n");
SolutionCount++;
}
}
Umm, it's your contest, you should be able to choose the appropriate declaration. :)Quote:
>> but you failed to write the function to the appropriate declaration of
BOOL Solution(unsigned hyper solution_to_check);
When did you ever say that that was the appropriate declaration?
Besides, BOOL and unsigned hyper are probably very unportable . . .
If you want to see 8 queens, why not just go see a Sigfried & Roy show 4 times tee-hee
right here -
granted I only specified that the function return type BOOL (a.k.a. bool and very portable), but with the possible number of solutions to check being in some cases larger than the maximum value of a 32bit unsigned , it should be obvious that you need x to be hyper, a.k.a. __int64, _int64 , int64 all very portable.