我正在研究迷宫解决方案问题。代码找到目标后,我无法让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']]