, but you failed to write the function to the appropriate declaration of
BOOL Solution(unsigned hyper solution_to_check);
, 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.
There are 10 types of people in this world, those who cringed when reading the beginning of this sentence and those who salivated to how superior they are for understanding something as simple as binary.
> 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
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
>> 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++; } }
Last edited by Brad0407; 06-22-2007 at 09:24 PM.
Don't quote me on that... ...seriously
Umm, it's your contest, you should be able to choose the appropriate declaration.>> 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 . . .
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
If you want to see 8 queens, why not just go see a Sigfried & Roy show 4 times tee-hee
Last edited by The Brain; 06-24-2007 at 09:45 AM.
- "Problem Solving C++, The Object of Programming" -Walter Savitch
- "Data Structures and Other Objects using C++" -Walter Savitch
- "Assembly Language for Intel-Based Computers" -Kip Irvine
- "Programming Windows, 5th edition" -Charles Petzold
- "Visual C++ MFC Programming by Example" -John E. Swanke
- "Network Programming Windows" -Jones/Ohlund
- "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
- "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel
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.