内容中心

2026年比较好的机械锁推荐品牌厂家-宁波望通锁业有限公司

To solve this problem, we need to count the number of valid paths from the top-left corner (0,0) to the bottom-right corner (2,2) in a 3x3 grid, where movement is restricted to right or down directions and blocked cells (marked with -1) are avoided.

Approach

The problem can be efficiently solved using dynamic programming (DP) due to its overlapping subproblems and optimal substructure properties. Here's the step-by-step approach:

  1. Edge Case Check: If the start (0,0) or end (2,2) cell is blocked, return 0 immediately as no path exists.
  2. DP Table Initialization: Create a 3x3 DP table where dp[i][j] represents the number of valid paths to reach cell (i,j).
  3. First Row: Fill the first row since movement to the right is the only option here. If a cell is blocked, set its value to 0 and all subsequent cells in the row will also be 0 (since you can't move right from a blocked cell).
  4. First Column: Fill the first column similarly, as movement down is the only option here.
  5. Fill Remaining Cells: For each cell (i,j) where i>0 and j>0, the number of paths to reach it is the sum of paths from the cell above (i-1,j) and the cell to the left (i,j-1), provided the current cell is not blocked.

Solution Code

def count_paths(grid):
    # Check if start or end is blocked
    if grid[0][0] == -1 or grid[2][2] == -1:
        return 0

    # Initialize DP table
    dp = [[0]*3 for _ in range(3)]
    dp[0][0] = 1  # Starting point

    # Fill first row (only right moves allowed)
    for j in range(1, 3):
        if grid[0][j] != -1:
            dp[0][j] = dp[0][j-1]
        else:
            dp[0][j] = 0

    # Fill first column (only down moves allowed)
    for i in range(1, 3):
        if grid[i][0] != -1:
            dp[i][0] = dp[i-1][0]
        else:
            dp[i][0] = 0

    # Fill the rest of the DP table
    for i in range(1, 3):
        for j in range(1, 3):
            if grid[i][j] == -1:
                dp[i][j] = 0
            else:
                dp[i][j] = dp[i-1][j] + dp[i][j-1]

    return dp[2][2]

Explanation

  • Edge Case Handling: The start and end cells are checked first to quickly return 0 if either is blocked.
  • DP Table: The DP table is used to store intermediate results, avoiding redundant calculations.
  • First Row and Column: These are filled by considering only the valid moves (right for the row, down for the column) and setting blocked cells to 0.
  • Remaining Cells: For each cell, the number of paths is the sum of paths from the cell above and left, ensuring blocked cells are skipped.

This approach efficiently computes the number of valid paths with a time complexity of O(1) (since the grid size is fixed at 3x3) and space complexity of O(1) (fixed DP table size).

Example: For a grid with no blocked cells, the result is 6 (as there are 6 valid paths in a 3x3 grid). For a grid with the center cell blocked, the result is 2 (only two valid paths around the blocked cell).

This solution is optimal and handles all possible cases correctly.

宁波望通锁业有限公司

宁波望通锁业有限公司



作者声明:本文包含人工智能生成内容。

在线客服

在线留言
您好,很高兴为您服务,可以留下您的电话或微信吗?