Thread: 8 Queens

  1. #31
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    , but you failed to write the function to the appropriate declaration of

    BOOL Solution(unsigned hyper solution_to_check);

  2. #32
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    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.

  3. #33
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    > 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.

  4. #34
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    >> 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?
    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]) &#37; 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;
    }
    Brute force (which is still boring):
    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

  5. #35
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    >> 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?
    Umm, it's your contest, you should be able to choose 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.

  6. #36
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    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

  7. #37
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Quote Originally Posted by Brad0407 View Post
    >> 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?
    right here -
    Quote Originally Posted by abachler View Post
    Code:
    x = 0;
    while(!solution(x)) x++;
    now you just need to write the BOOL solution() function and have it return whether X is a valid solution.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 8 Queens, problem with searching 2D array
    By Sentral in forum C++ Programming
    Replies: 35
    Last Post: 03-11-2009, 04:12 PM
  2. Problems with the eight queens problem
    By Wicket in forum C++ Programming
    Replies: 1
    Last Post: 06-12-2008, 09:29 AM
  3. 8 Queens problem
    By Dashing Boy in forum C++ Programming
    Replies: 14
    Last Post: 10-15-2007, 12:12 PM
  4. and this is my solution for the 8 queens puzzle
    By negevy in forum C Programming
    Replies: 1
    Last Post: 09-01-2005, 11:45 AM
  5. 8 Queens
    By PutoAmo in forum C Programming
    Replies: 2
    Last Post: 03-24-2003, 04:45 AM