简体   繁体   中英

How to rotate a solution to the 8 queens puzzle by 90 degrees?

I am looking at chapter 14.8. "Eight Queens puzzle" of "Learning with Python 3 (RLE)" .

It discusses solutions to the 8 queens problem. For instance, this solution:

在此处输入图像描述

is be represented with a list of 8 values, where each gives the row index of a queen, column by column:

[6, 4, 2, 0, 5, 7, 1, 3]

Now I am trying to solve exercise 4c:

Write a function to rotate a solution by 90 degrees anti-clockwise

My code:

def ninety_degrees(n):
    result = []
    for i in range(len(n)):
        result.append("")
    for i in n:
        result[i] += str(n.index(i))
    return result

print(ninety_degrees([1,1,1,1,1,1,1,1]))

It returns

['', '00000000', '', '', '', '', '', '']

instead of

['', '01234567', '', '', '', '', '', '']

Actually, I think the perfect answer is

['', [0,1,2,3,4,5,6,7], '', '', '', '', '', '']

...but I keep getting errors when I try to have integers instead of strings.

Let's take the example solution that needs to be rotated:

bd = [6, 4, 2, 0, 5, 7, 1, 3]

As stated in the question, this represents this position:

在此处输入图像描述

To rotate it anti-clockwise, consider which will be the first value in the result list: it will concern the queen that is currently in row 0. So you'll have to find the column where that 0 occurs in the current list. The distance between that column index and the right end of the board, will become the row number of that first queen in the result.

Apply this to all other queens, and you get this algorithm:

rot = []
for i in range(len(bd)):
    rot.append(len(bd) - 1 - bd.index(i))

For the example above, the result will be:

rot = [4, 1, 5, 0, 6, 3, 7, 2]

Basically, you have a list of 8 elements which represent the position of the queens: say the list is called queens , queens[i] = j (where i and j are both integers) means there is a queen at (i,j) .

To understand how to perform a rotation by 90 degrees, you need to understand where a queen located at (i,j) goes. Before reading the following, which spoils the solution, try to solve the problem with that hint, or at least try to answer the question “if a queen has coordinates (i, j) , what will be her coordinates after the rotation?”


Solution : If you have a chessboard of size 8x8, then the rotation is of the form (i, j) -> (j, 7-i) (it is 7-i and not 8-i because we start counting at 0 and end at 7). If you are not convinced (and you should not if it's the first time you see this), I let you try with a real chessboard or something to verify that it's a correct formula. If you wonder how to find such a formula, I think you should first learn about linear algebra, and in particular the representation of rotation of the plan as matrices. If you think it's too complicated (even if, really, it isn't that hard, and you should try to learn when possible), just ignore it, it won't be useful for now.

Now, I encourage you to, again, try to code the solution by yourself , now that you have the formula. If you can't come up with a solution, I'll add it to this post.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM