[英]8 Queens Solution in Python
很久以前我在网上找到了以下C代码,并尝试用Python实现这个解决方案。 当我编译C代码时,我得到了预期的结果; 当我运行我的Python脚本时,我没有收到任何输出。 下面是C代码和我将它转换为Python。 我应该注意,我正在做这个小项目以努力学习Python语法。 从我在微弱的调试尝试中可以看出,问题在于我实现了diagonalsOK()函数。 任何建议将不胜感激!
#include <stdio.h>
/* SOLUTION TO EIGHT QUEENS PROBLEM
Author: Eilon Lipton
Date: 1/26/2000
http://www.yoe.org/progchan/start.shtml
All code is copyright (C) Eilon Lipton, 2000
You may use it for educational purposes but please give me credit
if you show the solution to others.
*/
/* Since this program outputs many lines, you should probably redirect its
output to a file like so:
eightq > solution.txt
and then open the file solution.txt in any text editor to see the
results */
/* This function resets the board to an empty board */
void clearboard(int board[8][8])
{
int i, j;
for (i = 0; i < 8; i++)
for (j = 0; j < 8; j++)
board[i][j] = 0;
}
/* This function prints out the board to the screen using a simple diagram
*/
void printsolution(int board[8][8])
{
int i, j;
for (i = 0; i < 8; i++)
{
for (j = 0; j < 8; j++)
{
if (board[i][j] == 0)
{
printf("*");
}
else
{
printf("Q");
}
}
printf("\n");
}
printf("\n");
}
/* Counts how many queens are in a certain row */
int rowOK(int row, int board[8][8])
{
int i, counter;
counter = 0;
for (i = 0; i < 8; i++)
{
counter = counter + board[row][i];
}
return counter;
}
/* Counts how many queens are in the two diagonals crossing a certain place
*/
int diagonalsOK(int row, int column, int board[8][8]){
int i, counter;
counter = 0;
/* This function is a bit tricky:
We try every diagonal extending no more than 8 spaces in each of the four
directions
(down/left, down/right, up/left, and up/right */
for (i = 1; i < 8; i++) {
if ((row - i) >= 0) {
if ((column - i) >= 0) {
counter = counter + board[row - i][column - i];
}
if ((column + i) < 8)
{
/* down/right */
counter = counter + board[row - i][column + i];
}
}
if ((row + i) < 8) /* check that row is not out of bounds */
{
if ((column - i) >= 0) /* check that column is not out of bounds */
{
/* up/left */
counter = counter + board[row + i][column - i];
}
if ((column + i) < 8) /* check that column is not out of bounds */
{
/* up/right*/
counter = counter + board[row + i][column + i];
}
}
}
return counter;
}
/* This is the most important function, it is described on the web page */
void addqueen(int column, int board[8][8])
{
int row;
for (row = 0; row < 8; row++)
{
board[row][column] = 1;
if ((rowOK(row, board) == 1) &&
(diagonalsOK(row, column, board) == 0))
{
if (column == 7)
{
printsolution(board);
}
else
{
addqueen(column + 1, board);
}
}
board[row][column] = 0;
}
}
/* Main function */
int main()
{
int board[8][8];
printf("Meow?\n");
clearboard(board);
addqueen(0, board);
return 0;
}
Python转换尝试:
board = [[0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0]]
def clearBoard(board):
for i in range(8):
for j in range(8):
board[i][j] = 0
def printBoard(board):
for i in range(8):
for j in range(8):
if board[i][j] == 1:
print("Q", end="")
else:
print("X", end="")
print("")
print("")
def rowOK(row, board):
counter = 0
for i in range(8):
counter += board[row][i]
return counter
def diagsOK(row, col, board):
counter = 0
for i in range(8):
if (row - i) >= 0:
if (col - i) >= 0:
counter = counter + board[row - i][col - i]
if (col + i) < 7:
counter = counter + board[row - i][col + i]
if (row + i) < 8:
if (col - i) >= 0:
counter = counter + board[row + i][col - i]
if (col + i) < 8:
counter = counter + board[row + i][col + i]
return counter
def addQueen(col, board):
for row in range(8):
board[row][col] = 1
if rowOK(row, board) == 1 & diagsOK(row, col, board) == 0:
#print("Adding first queen...")
if col == 7:
printBoard(board)
else:
addQueen(col + 1, board)
board[row][col] = 0
clearBoard(board)
addQueen(0, board)
对于Starters,在addQueen之后添加print(board)语句,然后查看并将其与注释掉clearBoard的时间进行比较,您可以看到放入的板阵列似乎清除了所有内容,并且会得到0的输出。
这应该可以让您了解正在发生的事情。
您还需要检查列是否正常,但如果您更改了addQueen,请执行以下操作:
def addQueen(col, board):
for row in range(8):
if rowOK(row, board) == 0 and diagsOK(row, col, board) == 0:
board[row][col] = 1
if col == 7:
printBoard(board)
else:
addQueen(col + 1, board)
谢谢大家的意见。 我发现问题必须与我的diagsOK()函数的实现有关。 我仍然不确定我在哪个方面出错了,但我想了解如何正确地写这个并提出了你在下面看到的解决方案。 对于这个问题肯定有一个更加简单和优雅的解决方案,但我想用我知道的方法来解决它。 我测试了下面的解决方案,它的工作原理。 我很高兴 :-)
# Matt Lozier's 8 Queens Solution in Python.
#
# Thanks to Eilon Lipton (elipton@microsoft.com) for his logic
# in implementing the addQueen() function, which I adapted to Python
# from his C implementation.
# This 2D array (or in Python lists of lists) is one solution
board = [[0, 0, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 0],
[1, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 1],
[0, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 1, 0, 0, 0, 0, 0]]
def clearBoard(board):
for i in range(8):
for j in range(8):
board[i][j] = 0
def printBoard(board):
for i in range(8):
for j in range(8):
if board[i][j] == 1:
print("Q", end="")
else:
print("X", end="")
print("")
print("")
def checkColsOK(board):
for i in range(8):
sum = 0
for j in range(8):
sum += board[j][i]
if sum > 1:
return 0
def checkRowsOK(board):
for i in range(8):
sum = 0
for j in range (8):
sum += board[i][j]
if sum > 1:
return 0
def checkDiagsOK(board):
# left to right, bottom up
counter = 8
sum = 0
for i in range(8):
x = i
y = 0
for j in range(counter):
#print(board[y][x], end="")
sum += board[y][x]
x += 1
y +=1
counter -= 1
#print("")
#print("There are ", end="")
#print(sum, end="")
#print(" queens in this diagonal.")
if sum > 1:
return 0
sum = 0
# right to left, top down
counter = 8
sum = 0
for i in range(8):
x = i
y = 0
for j in range(counter):
#print(board[x][y], end="")
sum += board[x][y]
x += 1
y +=1
counter -= 1
#print("")
#print("There are ", end="")
#print(sum, end="")
#print(" queens in this diagonal.")
if sum > 1:
return 0
sum = 0
# right to left, bottom up
counter = 8
sum = 0
for i in reversed(range(8)):
x = i
y = 0
for j in range(counter):
#print(board[x][y], end="")
sum += board[x][y]
x -= 1
y += 1
counter -= 1
#print("")
#print("There are ", end="")
#print(sum, end="")
#print(" queens in this diagonal.")
if sum > 1:
return 0
sum = 0
# left to right, top down
counter = 8
sum = 0
for i in range(8):
x = 7
y = i
for j in range(counter):
#print(board[x][y], end="")
sum += board[x][y]
x -= 1
y += 1
counter -= 1
#print("")
#print("There are ", end="")
#print(sum, end="")
#print(" queens in this diagonal.")
if sum > 1:
return 0
sum = 0
def addQueen(board, col):
row = 0
for row in range(8):
board[row][col] = 1
if (checkRowsOK(board) != 0 and checkDiagsOK(board) != 0):
if col == 7:
printBoard(board)
else:
addQueen(board, col + 1)
board[row][col] = 0
clearBoard(board)
addQueen(board, 0)
#if checkDiagsOK(board) != 0:
# print("Diagonals are OK!")
#if checkRowsOK(board) != 0:
# print("Rows are OK!")
#if checkRowsOK(board) != 0:
# print ("Cols are OK!")
#printBoard(board)
#clearBoard(board)
#printBoard(board)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.