Checkmate!

This is a discussion on Checkmate! within the A Brief History of Cprogramming.com forums, part of the Community Boards category; How do you put 8 queens on a chessboard so that they won't capture each other?...

  1. #1
    Registered User FearOfTheDark's Avatar
    Join Date
    Jan 2003
    Posts
    31

    Checkmate!

    How do you put 8 queens on a chessboard so that they won't capture each other?
    -We're living in a illusion!
    -Ok, if that's what you think!

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Being on a programming forum and all, you could write a program that finds the answer for you...
    Or look below:
    Attached Images Attached Images  
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Funniest man in this seat minesweeper's Avatar
    Join Date
    Mar 2002
    Posts
    801
    Won't the top and bottom-most queens capture each other?

  4. #4
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    Put on 8 kings so they're busy?



    Put 4 queens on opposite sides of the board and offset them 1 square to the side. At least using the already posted board, that works.

  5. #5
    BMJ
    BMJ is offline
    Banal internet user BMJ's Avatar
    Join Date
    Aug 2002
    Location
    Chicagoland
    Posts
    1,380
    I don't think it's possible.

  6. #6
    Funniest man in this seat minesweeper's Avatar
    Join Date
    Mar 2002
    Posts
    801
    Golfinguy,

    Do you mean having 4 queens on the same vertical line? Because that clearly won't work.

  7. #7
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    This one actually works. (There are 92 working solutions)
    Attached Images Attached Images  
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  8. #8
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    You could create a program to find the solution using backtracking.... Similar to solving a maze problem.. I have to use something like this when i start coding my chess games single player part..

  9. #9
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by minesweeper
    Won't the top and bottom-most queens capture each other?
    Gee, how could I miss that one???
    And I thought I checked em all....
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  10. #10
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    Originally posted by minesweeper
    Golfinguy,

    Do you mean having 4 queens on the same vertical line? Because that clearly won't work.
    Never mind. I was thinking that 4 of the queens were on the same team.

  11. #11
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    And if you want how I calculated it, here ya go.

    Warning
    This code is uncommented, and basically contest-style code.

    Code:
    #pragma warning( disable: 4530 )
    
    #include <fstream>
    
    using namespace std;
    
    #define MAXSIZE 13
    
    
    bool col[ MAXSIZE ];
    bool updiag[ MAXSIZE * 2 ];
    bool downdiag[ MAXSIZE * 2 ];
    
    ifstream in("checker.in");
    ofstream out("checker.out");
    
    int n = 0;
    int numoutputed = 0;
    
    void outputsoln( int *row ) 
    {
      
      for( int i = 0; i < n; i++ ) {
        
        if( i != 0 ) out<<" ";
        
        out<<row[i] + 1;
        
      }
    
      out<<endl;
      
    }
    
    int calcnum( int i, int *row ) 
    {
      int sum = 0;
      
      if( i == n ) {
        
        outputsoln( row );
        return 1;
        
      }
      
      for( int j = 0; j < n; j++ ) {
        
        if( !col[ j ] && !updiag[ i + j ] && !downdiag[ i - j + n ] ) {
          
          col[ j ] = true;
          updiag[ i + j ] = true;
          downdiag[ i - j + n ] = true;
          row[i] = j;
        
          sum += calcnum( i + 1, row );
        
          col[ j ] = false;
          updiag[ i + j ] = false;
          downdiag[ i - j + n ] = false;
          
        }
        
      }
    
      return sum;
      
    }
    
    
    int main( void ) 
    {
      int rows[ MAXSIZE ];
      
      in>>n;
      
      out<<calcnum( 0, rows )<<endl;
      
      
      return 0;
    }
    Last edited by XSquared; 04-20-2003 at 07:57 PM.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checkmate
    By MadCow257 in forum General AI Programming
    Replies: 3
    Last Post: 12-09-2005, 07:04 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21