![](/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.