Eight Queens, More arrays

Ok. I've always had trouble with the Eight Queens problems that I've been given. Yet again another one appears. This one is supposed to see if eight queens are placed on an 8x8 chessboard in such a way that no queens will be able to attack each other. This means that they have to lie along same row, column, or diagonal.

It's supposed to look like this:

Enter the rows containing queens, in order by column: 2 0 6 4 7 1 3 5

Safe


Or supposed to look like this:
Enter the rows containing queens, in order by column: 2 3 4 0 1 7 6 5

Unsafe

I've been able to handle the other Eight Queens codes with the help from people on this forum but this one I don't even know where to start.
Start by getting the input from the user.

Once you have that, loop through each of the queens and see if any of them are in the same row/column/diagonal from any other. As soon as you find one able to attack another, you can print "Unsafe" and exit. If you never find one that can be attacked... then you know it's safe.


EDIT:

For logic problems... I typically try to think about how I would solve it myself... like... without a computer.

If you were given a chess set and given this problem. What would you do to determine whether or not any queen can attack any other?

Take your answer to that question, and translate it into code.
Last edited on
Ok so I was able to get it down to where I just need to check for the diagonal. Right now I have it where if any of the queens are in the same row or column as another, it will output "Unsafe". But how do I get it to determine if any are on the same diagonal as another?

Here is what I've got:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
using namespace std;

int main()
{
  cout << "Enter the rows containing queens, in order by column: ";
//Collects the data for the array.
  int board[8];
  for(int i = 0; i < 8; i++)
    cin >> board[i];

  int q = 0;
	
	for (int i = 0; i < 8; i++)
	{
		q = 0; 

		for (int j = 0; j < 8; j++)
		{
			if (board [j] == i)
			{
				q++;
			}
		}

  //Already unsafe even without diagonal check.
		if (q != 1)
		{
			cout << endl << "Unsafe";
			return 0;
		}
	}
If two pieces are diagonal from each other... the absolute difference between their X coord will be equal to the absolute difference of their Y coord.

For example...


2,5 is diagonal from 3,4 because:

1
2
3
abs(2-3) == abs(5-4)
abs(-1) == abs(1)
1 == 1


On the other hand, 1,6 and 3,5 are not diagonal:
1
2
3
abs(1-3) != abs(6-5)
abs(-2) != abs(1)
2 != 1
Last edited on
So where would I put that in my code? I understand you're looking at the two numbers of each corner behind the queen and in front of the queen hence the absolute value but I don't know where to put it.
I didn't really give you any code to put anywhere. I outlined the logic involved with determining whether or not 2 pieces are diagonal from each other.

So at whatever point in your code where you are checking to see if 2 pieces are diagonal... you would write some code which does a check based on the above logic.
Topic archived. No new replies allowed.