![](/img/trans.png)
[英]Stuck with implementing permutations using recursion. Not getting any output
[英]Finding possible paths on a map using recursion. Help needed
我一直在尝试解决一个问题,我希望能够从左上角到右列通过网格/地图(见下文)。 这个想法是通过网格找到所有可能的路径。
网格
1 2 3
1 2 3
1 2 3
我已经构建了一个递归方法,它的工作原理是找到网格中特定位置(连接点)的所有可能方向(例如,一个人可能能够向南和向东),然后通过再次调用相同的递归方法来遍历可能的方向. 如果我们到达右列,递归结束,我们回到之前的位置并尝试其他方向。
为了避免循环,该方法还跟踪当前路径,因此我们避免返回或循环。 这是我遇到问题的当前路径列表。 当我到达第一个基本情况(结束递归调用)并且递归方法尝试另一个方向时,列表已更改(即使它不应该)包含基本情况的路径。
查看下面的打印输出,我指出了第一个错误。 包含当前路径的列表出于某种原因确实包含它不应该做的历史路径。
[(0, 1), (1, 0)]
在 for 循环 0 0
[['x', 2, 3], [1, 2, 3], [1, 2, 3]]
[(0, 2), (1, 1)]
在 for 循环 0 1
[['x', 'x', 3], [1, 2, 3], [1, 2, 3]]
端点 0 2
在 for 循环 0 1
[['x', 'x', 'x'], [1, 2, 3], [1, 2, 3]]
[(1, 2), (2, 1), (1, 0)]
在 for 循环 1 1
[['x', 'x', 'x'], [1, 'x', 3], [1, 2, 3]] -----> 这行不应该在位置 3 中包含 x第一排
端点 1 2
在 for 循环 1 1
[['x', 'x', 'x'], [1, 'x', 'x'], [1, 2, 3]]
[(2, 2), (2, 0)]
在 for 循环 2 1
[['x', 'x', 'x'], [1, 'x', 'x'], [1, 'x', 3]]
端点 2 2
在 for 循环 2 1
[['x', 'x', 'x'], [1, 'x', 'x'], [1, 'x', 'x']]
[(1, 0)]
在 for 循环 2 0
[['x', 'x', 'x'], [1, 'x', 'x'], ['x', 'x', 'x']]
[]
在 for 循环 1 1
[['x', 'x', 'x'], ['x', 'x', 'x'], ['x', 'x', 'x']]
[]
在 for 循环 0 0
[['x', 'x', 'x'], ['x', 'x', 'x'], ['x', 'x', 'x']]
[]
任何帮助是极大的赞赏!!!!!
#This method is called each time a new path is taken
def goNextPath(tmp_mapOfMudCopy,tmp_y,tmp_x,tmp_high):
# ~ print (tmp_mapOfMudCopy)
tmp_mapOfMudCopy[tmp_y][tmp_x] = 'x' #This marks that we have been here and we dont want to go here again
#Ok first check if we reached an endpoint then just return. This is the base case of the recursion
if tmp_x == (len(tmp_mapOfMudCopy[tmp_y]) - 1):
# tmp_mapOfMudCopy[tmp_y][tmp_x] = 'x' #This marks that we have been here and we dont want to go here again
print('endpoint',tmp_y,tmp_x)
return
#This is not an endpoint so fitmp_mapOfMudCopynd which possible directions we can go and add to list. Check so we dont go the same way again
listPDirections = []
#Check north. Check also if we have been there before
if tmp_y > 0: #First check that north is possible
if tmp_mapOfMudCopy[tmp_y - 1][tmp_x] != 'x': #Now check if we have benn there
listPDirections.append((tmp_y - 1,tmp_x))
#Check east.
if tmp_x < (len(tmp_mapOfMudCopy[tmp_y]) - 1):
if tmp_mapOfMudCopy[tmp_y][tmp_x + 1] != 'x':
listPDirections.append((tmp_y,tmp_x + 1))
#Check south.
if tmp_y < (len(tmp_mapOfMudCopy) - 1):
if tmp_mapOfMudCopy[tmp_y + 1][tmp_x] != 'x':
listPDirections.append((tmp_y + 1,tmp_x))
#Check west.
if tmp_x > 0:
if tmp_mapOfMudCopy[tmp_y][tmp_x - 1] != 'x':
listPDirections.append((tmp_y,tmp_x - 1))
print (listPDirections)
tmp_mapOfMudCopy_path = tmp_mapOfMudCopy.copy() #Copy the map with 'x' marking current path
for direction in listPDirections:
print ('In for loop ',tmp_y,tmp_x)
print(tmp_mapOfMudCopy_path)
goNextPath(tmp_mapOfMudCopy_path,direction[0],direction[1],tmp_high)
#This method finds the shallowest depth of mud route
def findPath():
tmp_mapOfMud = [[1,2,3],[1,2,3],[1,2,3]] #Create a map with two rows and three columns
goNextPath(tmp_mapOfMud,0,0,0) #Call the path finding method and
#Main method this is run when program starts and calls the other methods
if __name__ == '__main__':
findPath()
看看图论......像这样的东西https://www.geeksforgeeks.org/find-paths-given-source-destination/应该会有所帮助。 如果我正确理解你的问题
通过使用 deepcopy 解决了这个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.