# Thread: Could someone help me with this n queens-based problem?

1. ## Could someone help me with this n queens-based problem?

I have a problem where eight queens are to be placed on an 8x8 chessboard in such a way that no queens will be able to attack each other (i.e., lie along same row, column or diagonal). The program will prompt the user to enter, for each column, the row that contains a queen. For the given configuration, the program should print "Safe" if no queen can attack any other queen (none are sharing the same row, column or diagonal), or print "Unsafe" if an attack is possible. I'm not entirely sure how to do this however, and I'd like some help. I am working from this base code:
Code:

Code:
```  #include <iostream>
using namespace std;
int main() {
int board;
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;
}
}``` 2. Allocate a 2d array, every element initialized to 0.

Each queen is the value 1.

Sum elements in each row, store
Sum elements in each column, store
Sum elements in each diagonal direction, going both ways, store

If any sum is larger than 1, you know it's not safe.

This is how I would do it, at least. 3. Let me first point out that this is also posted here.

Then I will remark that at least you copied some code from somewhere that is at least formatted correctly.

Next I'm going to wish you luck, since just the other day you couldn't even print a simple histogram this topic is likely to be very interesting.

Jim 4. Originally Posted by jimblumberg Let me first point out that this is also posted here.

Then I will remark that at least you copied some code from somewhere that is at least formatted correctly.

Next I'm going to wish you luck, since just the other day you couldn't even print a simple histogram this topic is likely to be very interesting.

Jim
I lol'd. 5. Originally Posted by MutantJohn Allocate a 2d array, every element initialized to 0.

Each queen is the value 1.

Sum elements in each row, store
Sum elements in each column, store
Sum elements in each diagonal direction, going both ways, store

If any sum is larger than 1, you know it's not safe.

This is how I would do it, at least.
I would like to do this, but I still don't quite get it. Where is every element stored in my current program? 6. Originally Posted by Kelton2 I would like to do this, but I still don't quite get it. Where is every element stored in my current program?
Lookup the arrays in your book; "your" program is using an 1d array the suggestion was using an 2d array.

Tim S. 7. Originally Posted by Kelton2 I would like to do this, but I still don't quite get it. Where is every element stored in my current program?
You basically start out with something like this:

Code:
`    bool board = {{false}}; // init to zero`
A small 5*5 board with "queen" at position (board):

    
    
    
    
    

Now, in the loop that gathers users input, for every new entry, iterate through the 8 directions a queen can move, starting with the x and y value that the user has passed in for the new queen position:

= (no change to user value)
-- (iterate down from user value)
++(iterate up from user value)

Direction:[y][x]
------------------------
// left:[=][--]
// left-up:[--][--]
// up:[--][=]
// right-up: [--][++]
// right: [=][++]
// right-down: [++][++]
// down: [++][=]
// left-down: [++][--]

At every step of these iterations, we check to see "is this boolean value at board[y][x] true?", and if it is, then the queen the user has passed in is under attack, and that move is invalid. Popular pages Recent additions 