我有一個列表-
字符作爲網格。選擇行列唯一的元素列表(N皇后)
我想將一個-
更改爲Q
每列和每行。
這裏是我到目前爲止有:
import pprint
import random # I import these classes
grid = [['-'] for n in range(8)]
for i in range (8):
for j in range(8):
inserPoint = random.randrange(8,8)
if (j == inserPoint or i == inserPoint) and (grid[j] != 'Q' or grid[i] != 'Q'):
grid[i][j] = ('Q')
pprint.pprint(grid) #/ how to print one queen per line
這是我的輸出。正如你可以看到有對電網太多Q
S:
[['-','-','-','-','-','-','Q','-'],
['-','-','-','-','Q','Q','-','-']
['-','-','-','-','Q','-','-','-']
['Q','Q','-','-','-','Q','Q','-']
['-','-','Q','-','Q','-','-','-'].
第一步是修復你的縮進。 – IanAuld
第二步應該是修復if語句中的語法錯誤 – inspectorG4dget
請閱讀http://stackoverflow.com/help/how-to-ask –