473,322 Members | 1,232 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

8 queens

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
1 2413
Banfa
9,065 Expert Mod 8TB
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

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

Similar topics

2
by: Andreas | last post by:
Hi! does anybody know an algorithm to solve the 8-queens-problem in PHP?
3
by: cfanatic | last post by:
Hey all, I been reading through these forums for a long time but have never posted. Well, I got a dillema. I have a program of the Eight Queen's program and I have to make it work without...
5
by: Matt | last post by:
I will be grateful if someone explians this part colfree = FALSE; upfree = FALSE; downfree = FALSE; of the code below. I don't understand how this marks the downward and upward diagonals....
9
by: totalgeekdom | last post by:
Background: The problem I'm trying to solve is. There is a 5x5 grid. You need to fit 5 queens on the board such that when placed there are three spots left that are not threatened by the queen. ...
4
by: Idea | last post by:
hi guys m new out here and need help with an assignment due next week.The assignmnet is some what related to eight queens problem but not fully, you can say the basic is taken from that problem....
2
by: angel120 | last post by:
Since this is my first post, allow me to introduce myself. My name is Angel, and I'm a 3rd year student at Queens College. I currently am taking CS211, which is the second level of C++, CS212 which...
5
by: angel120 | last post by:
So my second hw is to solve the 8 queens problem using a 1D array. The professor gave us a code that outputs the following (labeled as q): 0 2 5 7 6 3 1 4 The spaces 0-7 refer to the column #,...
18
by: cf29 | last post by:
Greetings, I designed in JavaScript a small program on my website called 5 queens. (http://www.cf29.com/design/dame5_eng.php) The goal is to control all the chess board with five queens that...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.