By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,408 Members | 1,914 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,408 IT Pros & Developers. It's quick & easy.

8 queens

P: 47
I am trying to develop an 8 queens program, and currently it is working however it is printing 87222211 which is obviously wrong, I am trying to print the row of the queen in order from column0-column7 of an array. I am new to C++ and was wondering if anyone could see my mistake? Thanks for taking a look. Here is my code.
Expand|Select|Wrap|Line Numbers
  1. int a[8];
  2.  
  3. void display ();
  4.  
  5. int TestLegal (int currentColumn, int currentRow)
  6. {
  7.     for (int column = 0; column < currentColumn; ++column)    //check row conflict
  8.         if (a[column] == a[currentColumn])    
  9.         return (0);    // return FALSE if both queens are on the same row
  10.  
  11.     for (int column = 0; column < currentColumn; ++column)    //check diagonal conflict
  12.         if (abs(a[column] - column) == (abs(a[currentColumn] - currentColumn)))
  13.         return (0);   // return FALSE if both queens are on the same diagonal.
  14.     return currentRow;
  15. }
  16.  
  17. int placeQueen (int column) {
  18.   if (column == 8)    //Check if all columns have a legal value
  19.   {
  20.     display();
  21.     return (-1);
  22.   }
  23.  
  24.   for (int row = 1; row <= 8; ++row)  // Try all of the legal values for the column
  25.   {
  26.     if (TestLegal (column, row))
  27.     {
  28.       a[column] = row;
  29.       if (placeQueen (column + 1) < 0)
  30.         return -1;
  31.     }
  32.   }
  33.   return (0);
  34. }
  35.  
Feb 16 '08 #1
Share this Question
Share on Google+
1 Reply


Banfa
Expert Mod 5K+
P: 8,916
I have removed some of your (non-relevant) code because we forbid posting complete answers to homework questions. I am assuming this is a homework question on the grounds I had to do the same question for homework when I was learning prolog (18 years ago).

To answer you queries

Expand|Select|Wrap|Line Numbers
  1.         if (abs(a[column] - column) == (abs(a[currentColumn] - currentColumn)))
  2.         return (0);   // return FALSE if both queens are on the same diagonal.
  3.  
I do not believe this is correct logic for calculating if 2 queens are on the same diagonal. I suggest you get a chess board and run a few examples using pen and paper.

Expand|Select|Wrap|Line Numbers
  1.     if (TestLegal (column, row))
  2.     {
  3.       a[column] = row;
  4.       if (placeQueen (column + 1) < 0)
  5.         return -1;
  6.     }
  7.  
The call to TestLegal uses a[column] in it's calculations but you do not set a[column] until after the call returns.


a is a very poor name for a variable.
Feb 16 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.