簡體   English   中英

尋路 Python 算法回溯

[英]Pathfinding Python Algorithm Backtracking

給定一個像board這樣的矩陣,我想找到可以讓我找到更多數字1's路徑,因為我知道我只能 go 向上(n+1)和向右(m+1) 我正在嘗試使用回溯解決方案,並且我設法知道在最佳路徑上可以找到多少1's ,但我無法弄清楚如何打印最佳路徑的坐標。

board=[[0,0,0,0,1,0],
       [0,1,0,1,0,0],
       [0,0,0,1,0,1],
       [0,0,1,0,0,1],
       [1,0,0,0,1,0]]

def findPath(board,n,m):
    noo=0
    if board[n][m]>0:
        noo+=1
    if n==len(board)-1 and m==len(board[0])-1:
        return noo
    if n==len(board)-1:
        noo+=findPath(board,n,m+1)
    elif m==len(board[0])-1:
        noo+=findPath(board,n+1,m)
    else:
        noo+=max(findPath(board,n,m+1),findPath(board,n+1,m))
    return noo

print(findPath(board,0,0))

我應該如何或在哪里實現print(n,m)以打印最佳路徑的每個網格的坐標

已編輯

想出了這個解決方案

def path(board,x,y,xl,yl,sol,index,paths):
    sol.append([x,y])
    solaux=sol
    if x==0 and y==0:
        pass
    if board[x][y]>0:
        sol[0]+=1
    if x==xl-1 and y==yl-1:
        paths.append(sol)
        print(sol)
        return paths
    if x==xl-1:
        path(board,x,y+1,len(board),len(board[0]),sol,index,paths)
    elif y==yl-1:
        path(board,x+1,y,len(board),len(board[0]),sol,index,paths)
    else:
        index= len(sol)
        auxnoo= sol[0]
        path(board,x,y+1,len(board),len(board[0]),sol,index,paths)
        sol = sol[0:index]
        sol[0]=auxnoo
        path(board,x+1,y,len(board),len(board[0]),sol,index,paths)
    return paths

起初,您的實現效率非常低,因為您多次為單個單元格執行findPath 例如,如果您有 2x2 網格,則單元格(1, 1)將被訪問兩次:

(1, 0) -- path 2 --> (1, 1)
   ^                   ^
   | path 2            | path 1
   |                   |
(0, 0) -- path 1 --> (0, 1)

所以讓我們記住每個單元格的noo值:

# noo[i][j] == None value means value for cell wasn't calculated before.
# Otherwise noo[i][j] it means calculated answer for cell.
noo=[[None for i in range W] for j in range H]

def findPath(board,n,m):
    if noo[n][m] is not None:
        return noo[n][m]
    noo[n][m] = board[n][m]
    if n==len(board)-1 and m==len(board[0])-1:
        return noo[n][m]
    if n==len(board)-1:
        noo[n][m]+=findPath(board,n,m+1)
    elif m==len(board[0])-1:
        noo[n][m]+=findPath(board,n+1,m)
    else:
        noo[n][m]+=max(findPath(board,n,m+1),findPath(board,n+1,m))
    return noo[n][m]

print(findPath(board,0,0))

我應該如何或在哪里實現 print(n,m) 以打印最佳路徑的每個網格的坐標

其次,沒有簡單的方法將print(n,m)放入給定的代碼中,因為對於給定的單元格,我們不知道它是否屬於任何最佳路徑。 只有當我們 go 回到單元格(0, 0)時,我們才會確定。

但是現在我們有 2d 數組noo :如果noo[i][j]包含值x ,那么最優將是相對於單元格(i, j)定位到下一個右側或向上單元格(i1, j1)的方向noo[i1][j1] >= x - 1noo[i1][j1]可以等於x如果board[i][j] == 0x - 1如果board[i][j] == 1 ) . 讓我們實現最佳路徑打印機:

def printOptimalPath(n, m):
    print(n, m)
    if n < len(board)-1 and noo[n + 1][m] == noo[n][m] - board[n][m]:
        printOptimalPath(n + 1, m)
    elif m < len(board[0])-1 and noo[n][m + 1] == noo[n][m] - board[n][m]:
        printOptimalPath(n, m + 1)

# Usage
printOptimalPath(0, 0)

請注意,如果noo[n+1][m]noo[n][m+1]都滿足上述條件,則意味着存在多個最優路徑,您可以選擇任意方向。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM