我正在研究一個迷宮解決方案問題。代碼可以找到目標後,我不能讓Python打印出解決方案列表。但這是作業所必需的。如何讓我的迷宮功能打印出解決方案
有人可以幫我嗎?我剛學了3周的python。我想打印出python走向最終目標的每一步。這裏是我的代碼:
def mazeDetector(row,col):
c= m[row][col]
solution=[]
if c =="W":
print "Wall here: "+ str(row)+ ","+ str(col)
return False
elif c =="V":
print "Visited: " + str(row)+ ","+ str(col)
return False
elif c=="F":
print "Found: "+ str(row)+ ","+ str(col)
print solution
return True
print "visiting:"+ str(row)+ ","+ str(col)
solution.append((row,col),)
m[row][col]=="V"
if (col>0 and mazeDetector(row,col-1)):
return True
elif (row< len(m)-1 and mazeDetector(row+1,col)):
return True
elif (row>0 and mazeDetector(row-1, col)):
return True
elif (col<=len(m)-1 and mazeDetector(row, col+1)):
return True
return False
mazeDetector(1,5)
這裏是迷宮,W
意味着牆壁,P
意味着地方去,S
手段開始,F
意味着決賽:
[['W', 'P', 'P', 'W', 'W', 'W'],
['W', 'W', 'P', 'W', 'P', 'S'],
['W', 'W', 'P', 'W', 'P', 'W'],
['P', 'P', 'P', 'P', 'P', 'W'],
['F', 'W', 'P', 'W', 'W', 'W'],
['W', 'P', 'P', 'P', 'P', 'W']]
謝謝sooo多!!! –
呃我怎麼能讓mazeDectector返回解決方案? –
您可以使用當前結果和解決方案的元組 –