Eight Queens problem

I'm working on a unique Eight Queens (N Queens, but always 8) problem that, instead of printing out the chessboard, prints "Yes" if there is exactly one queen in each row and one in each column, and "No" otherwise. I don't quite understand how to do the check for one queen in each row and one in each column, and I need some help figuring it out. The Eight Queens code this is based on is below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  #include <iostream>
using namespace std;
int main() {
   int board[8];
   cout << "Enter the rows containing queens, in order by column: ";
   for(int i = 0; i < 8; ++i) {
       cin >> board[i];
   }
   cout << endl;
   for(int i = 0; i < 8; ++i) {
       for(int a = 0; a < 8; ++a) {
           if(board[a] != i) {
               cout << ".";
          //     cout << ".";
           } else {
             //     cout << ".";
               cout << "Q";
           }
       }
       cout << endl;
   }
}

Other notes:
I don't want to use recursion or multiple functions (if possible)
I don't want to use any header files I haven't already included


I hope someone can help.
I don't want to use recursion or multiple functions (if possible)

Why not?
I don't want to use any header files I haven't already included

Again, why not?

... "Yes" if there is exactly one queen in each row and one in each column, ...

It's the eight queens problem, not the eight rooks. You have to test for diagonal as well.

EDIT: I should add more to my post. I am wondering why it is that you want to avoid the specific approaches to solving this problem. Keep in mind "Because I didn't learn that yet\No one taught me that yet" is not a valid excuse as this site is here to teach.

EDIT 2: int board[8]; This would make for a funny looking board. Chess boards are 8x8 for a total 64 squares. You need a two dimensional array for this problem.
Last edited on
1 and 2. This is for an assignment.
3.Fair enough, even though it didn't say anything about diagonals.

I still need help with this, however.
- First things first, you need a two dimensional array for this. There is no getting around that.

- Next, I suggest making a struct that holds an X and a Y Coord to represent each queen. Once you have this struct then testing if any two share the same row or column is as simple as testing if any share the same value for X or Y.

- Testing diagonal is going to be a bit harder, if you can get the above stuff done and post some code then we can tackle this one next.
keltonfan2 wrote:
I don't want to use recursion or multiple functions (if possible)
I don't want to use any header files I haven't already included
both are possible. but i think they involve much more complicated coding/algorithm understanding than your current state.


so, now at first you can code Rook checking (all are different rows and columns.)
then do Bishop checking. then you can combine them to get the Queen!
More specific on how I could do that?
bump
bool arr[4][4];
initilize all 16 bool to 0.
1. let the user input two coordinate.
2. check Rookwise. (if two Rooks can capture each other)
3. check Bishopwise. (if two Bishops can capture each other)

4. a) if any capture is possible, goto step1.
b) else if no capture is possible, say the user that he succeeded.
bool arr[4][4];
initilize all 16 bool to 0.
1. let the user input two coordinate.
2. check Rookwise. (if two Rooks can capture each other)
3. check Bishopwise. (if two Bishops can capture each other)

4. a) if any capture is possible, goto step1.
b) else if no capture is possible, say the user that he succeeded.

I don't understand.
Last edited on
bump...
oh my god, if you don't understand this.... actual program is 10x hard.
search Google for C++ solution code. try to understand them, then program your own version.
Topic archived. No new replies allowed.