问题描述
我最近参加了一场编程比赛,我能够及时回答了七个问题中的六个。最后一个问题是:“从用户那里取一个数字,并根据给定数字的幂制作一个棋盘,放置给定数量的皇后,并以某种方式排列皇后,使它们不会横向、纵向相互威胁和对角线。”例如,如果用户输入 4,在 4x4 (16) 棋盘中将有 4 个皇后。好吧,对于垂直和水平部分,我能够检查它们是否在同一列中,对于对角线部分,我使用了非常糟糕的 while 循环组合,然后将国际象棋游戏中的所有保留位置添加到列表中。问题是我使用 random 模块来生成索引,然后检查它们是否在保留数字中,然后再试一次,直到它不在那里,所以会发生什么情况是只有几种组合在这个国际象棋游戏中,如果开始索引是错误的,程序就会永远停留在 while 循环中。我曾想过从最后一行到第一行,但即便如此,我仍然使用随机索引。如何让程序计算皇后的位置,而不是随机输入一个数字并查看它是否有效?我的代码:(如果我的代码有其他问题,请指出):
import random
# in the program you had to put '1' as the position of the queens and put '0' as the empty places on the board
# and also,if the user entered a number less than 4 the combination would not have been possible
num = int(input("Enter number: "))
if num >= 4:
arr = [[0 for _ in range(num)] for _ in range(num)]
reserved_num = []
oblique = []
for i in range(num):
j = random.randint(0,num - 1)
while [i,j] in reserved_num:
j = random.randint(0,num - 1)
arr[i][j] = 1
for rows in range(num):
reserved_num.append([rows,j])
if i < num-1:
inc = 0
if j == num-1:
while j-inc > 0 and i+inc < num-1:
inc += 1
oblique.append([i+inc,j-inc])
else:
inc = 0
elif j == 0:
while j+inc < num-1 and i+inc < num-1:
inc+= 1
oblique.append([i+inc,j+inc])
else:
inc = 0
else:
while j+inc < num-1 and i+inc < num-1:
inc+= 1
oblique.append([i+inc,j+inc])
else:
inc = 0
while j-inc > 0 and i+inc < num-1:
inc += 1
oblique.append([i+inc,j-inc])
else:
inc = 0
for res in oblique:
reserved_num.append(res)
for items in arr:
print(items)
else:
print("Not Possible! ")
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)