动态规划 - 走出迷宫的方法数(有障碍物)

时间:2021-04-01 10:50:05

标签: python dynamic-programming

我正在尝试使用动态编程进行编码,这是一个 num_of_paths 函数,用于获取走出迷宫的方法数。所采用的迷宫是一个由 n 元组组成的元组,每个元组具有 m 值。元组内的值是 0 或 1,0 代表该单元格中的炸弹。这是伪代码:

  1. 初始化一个空表(字典),获取行数 n 和列数 m

  2. 填写第一行。对于 j 范围内的 m

    2.1.如果 maze[0][j] 是安全的,请将 table[(0, j)] 设置为 1,因为有一种方法可以到达那里。

    2.2.如果 maze[0][j] 有炸弹,请将 table[(0, k)] where k >= j 设置为 0。由于沿途有一个单元格被破坏,因此无法到达所有后续单元格(第一行)。

  3. 填写第一列。对于 i 范围内的 n

    3.1 如果 maze[i][0] 是安全的,请将 table[(i, 0)] 设置为 1,因为有一种方法可以到达那里。

    3.2 如果maze[i][0]有炸弹,设置table[(i, 0)]及其下的所有单元格为0。原因同row。

  4. 主要 DP 程序 - 填写表格的其余部分。如果 maze[i][j] 有炸弹,则设置 table[(i, j)] = 0。否则,table[(i, j)] = table[(i - 1, j)] + table[(i, j - 1)]

  5. 返回 table[(n - 1, m - 1)]

def num_of_paths(maze):
    # Step 1
    table = {}
    n = len(maze)
    m = len(maze[0])
    # Step 2
    for j in range(m):
        if maze [0][j] == 1: #Safe
            table[(0, j)] = 1
        else: #Bomb
            for k in range(j,):
                table[(0, k)] = 0
    for i in range(n): #Step 3
        if maze[i][0] == 1: #Safe
            table[(i,0)] = 1
        else: #Bomb
            table[(i,0)] = 0
    for j in range(1,m): #Step 4
        for i in range(1,n):
            if maze[i][j] == 0: #Bomb
                table[(i,j)] = 0
            else:
                table[(i,j)] = table[(i - 1, j)] + table[(i, j - 1)]
    return table[(n-1,m-1)] #Step 5

maze  = ((1, 1, 1, 1, 1, 1, 1, 1, 0, 1),
         (1, 0, 0, 1, 1, 1, 0, 0, 1, 1),
         (0, 1, 1, 1, 0, 0, 1, 1, 1, 0),
         (1, 1, 0, 1, 1, 1, 1, 0, 1, 1),
         (0, 1, 0, 1, 0, 0, 1, 0, 1, 0),
         (1, 0, 1, 1, 1, 1, 0, 1, 1, 1),
         (1, 1, 0, 1, 0, 1, 0, 0, 1, 1),
         (0, 1, 1, 1, 1, 1, 1, 1, 1, 0),
         (1, 0, 1, 0, 0, 1, 1, 0, 1, 1),
         (1, 0, 1, 1, 1, 0, 1, 0, 1, 0),
         (1, 1, 0, 1, 0, 1, 0, 1, 1, 1))

最后一行是 KeyError:(0,8)。 我怀疑这是因为密钥不在我的字典中(?) 任何人都有解决此错误的建议或编码的替代方法?

2 个答案:

答案 0 :(得分:1)

   def countPaths(maze):
     
    # If the initial cell is blocked,
    # there is no way of moving anywhere
    if (maze[0][0] == -1):
        return 0
 
    # Initializing the leftmost column
    for i in range(R):
        if (maze[i][0] == 0):
            maze[i][0] = 1
 
        # If we encounter a blocked cell in
        # leftmost row, there is no way of
        # visiting any cell directly below it.
        else:
            break
 
    # Similarly initialize the topmost row
    for i in range(1, C, 1):
        if (maze[0][i] == 0):
            maze[0][i] = 1
 
        # If we encounter a blocked cell in
        # bottommost row, there is no way of
        # visiting any cell directly below it.
        else:
            break
 
    # The only difference is that if a cell is -1,
    # simply ignore it else recursively compute
    # count value maze[i][j]
    for i in range(1, R, 1):
        for j in range(1, C, 1):
             
            # If blockage is found, ignore this cell
            if (maze[i][j] == -1):
                continue
 
            # If we can reach maze[i][j] from
            # maze[i-1][j] then increment count.
            if (maze[i - 1][j] > 0):
                maze[i][j] = (maze[i][j] +
                              maze[i - 1][j])
 
            # If we can reach maze[i][j] from
            # maze[i][j-1] then increment count.
            if (maze[i][j - 1] > 0):
                maze[i][j] = (maze[i][j] +
                              maze[i][j - 1])
 
    # If the final cell is blocked,
    # output 0, otherwise the answer
    if (maze[R - 1][C - 1] > 0):
        return maze[R - 1][C - 1]
    else:
        return 0

您可以访问 Geeksforgeeks 以获取更多说明: Click here

答案 1 :(得分:0)

def num_of_paths(maze):
    # Step 1
    table = {}
    n = len(maze)
    m = len(maze[0])
    # Step 2
    for j in range(m):
        if maze [0][j] == 1: #Safe
            table[(0, j)] = 1
        else: #Bomb
            for k in range(j,m):
                table[(0, k)] = 0
            break
    for i in range(n): #Step 3
        if maze[i][0] == 1: #Safe
            table[(i,0)] = 1
        else: #Bomb
            for k in range(i,n):
                table[(k,0)] = 0
            break
    for j in range(1,m): #Step 4
        for i in range(1,n):
            if maze[i][j] == 0: #Bomb
                table[(i,j)] = 0
            else:
                table[(i,j)] = table[(i - 1, j)] + table[(i, j - 1)]
    return table[(n-1,m-1)] #Step 5

设法调试,结果在我的 else 情况下在 2 for 循环下出现了一些错误。

  1. 必须在行/列的末尾结束内部循环
  2. 必须跳出外部 for 循环

感谢所有帮助过的人!