小编eye*_*eye的帖子

Python:N-Queen 谜题检查对角线的解决方案

在这里,我必须编写 N 皇后问题的代码,其中用户将输入棋盘的尺寸和皇后的位置。

Enter the dimension of the Chessboard: 4
Enter position of the Queen: 10
Enter position of the Queen: 31
Enter position of the Queen: 02
Enter position of the Queen: 23
Run Code Online (Sandbox Code Playgroud)

根据用户输入,我制作了候选解决方案(这是一个列表),其中候选解决方案的索引代表皇后的列,值代表皇后的行位置。

候选解如下: [1, 3, 0, 2]

由此我制作了一个矩阵来表示皇后的位置:

0, 0, 1, 0
1, 0, 0, 0
0, 0, 0, 1
0, 1, 0, 0
Run Code Online (Sandbox Code Playgroud)

我的问题是我必须通过检查对角线来检查候选解决方案是否为真,并在解决方案为真时打印一条消息。

这是到目前为止我的代码,它将打印候选解决方案和显示皇后位置的矩阵:

#Ask the user to input the dimension of the Chessboard(N)
N=int(input("Enter the dimension of the Chessboard: "))

#Creating a list of …
Run Code Online (Sandbox Code Playgroud)

python matrix n-queens

1
推荐指数
1
解决办法
4023
查看次数

标签 统计

matrix ×1

n-queens ×1

python ×1