Problem

Instead of using an 8-by-8 array to represent the board in the Eight Queens program, you c...

Instead of using an 8-by-8 array to represent the board in the Eight Queens program, you can use a one-dimensional array to represent only the squares that contain a queen. Letcol be an array of eight integers such that

col[k] = row index of the queen in columnk+ 1

For example, ifcol[2] is 3, then a queen is in the fourth row (square) of the third column—that is, inboard[3] [2 ]. Thus, you usecol[k] to represent a queen instead ofboard [col [k] ] [k].

This scheme requires diat you also store information about whether each queen is subject to attack. Because only one queen per column is permitted, you do not have to check columns. To check for a row attack, define an arrayrowAttack such thatrowAttack [k] is nonzero if the queen in columnh + 1 can be attacked by a queen in its row.

To check for diagonal attacks, observe that diagonals have either a positive slope or a negative slope. Those with a positive slope are parallel to the diagonal that runs from the lower left corner of the board to the upper right corner. Diagonals with a negative slope are parallel to the diagonal that runs from the upper left corner to the lower right corner. Convince yourself that ifboard [i][j ] represents a square, then i+j is constant for squares that are in a diagonal with a positive slope, and i -j is constant for squares that are in a diagonal with a negative slope. You will find that i + j ranges from 0 to 14 and that i - j ranges from -7 to +7. Thus, define arraysposDiagonal andnegDiagonal such that

posDiagonal [k] is true if the queen in columnk+ 1 can be attacked by a queen in its positive-sloped diagonal, and

negDiagonal [k] is true if the queen in columnk + 1 can be attacked by a queen in its negative-sloped diagonal.

Use these ideas to write a program drat solves the Eight Queens problem.

Step-by-Step Solution

Request Professional Solution

Request Solution!

We need at least 10 more requests to produce the solution.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the solution will be notified once they are available.
Add your Solution
Textbook Solutions and Answers Search
Solutions For Problems in Chapter 6
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT