内容中心

2026年质量好的智能VAV变风量阀厂家选购指南与推荐-熙诚环保科技(苏州)有限公司

To solve this problem, we need to find the shortest path from the top-left corner to the bottom-right corner of a grid, allowing up to k walls to be broken. The solution uses Breadth-First Search (BFS) with state tracking to efficiently explore the grid while considering the number of walls broken.

Approach

  1. State Representation: Each state in our BFS is represented by the current position (x, y), the number of remaining wall breaks b, and the number of steps taken steps.
  2. Visited Tracking: We maintain a 2D array visited where visited[x][y] stores the maximum number of remaining breaks when we last visited cell (x, y). This helps avoid revisiting cells with a worse or equal state (fewer breaks left or more steps).
  3. BFS Exploration: We start from the top-left corner and explore all four directions (up, down, left, right). For each neighbor:
    • If it's an empty cell (0), we move there without using a break.
    • If it's a wall (1), we move there only if we have remaining breaks, reducing the break count by 1.
  4. Termination: The first time we reach the bottom-right corner, we return the number of steps taken (since BFS ensures the shortest path). If the queue is exhausted without reaching the end, return -1.

Solution Code

from collections import deque

def shortest_path(grid, k):
    n = len(grid)
    m = len(grid[0])

    # Edge case: start == end
    if n == 1 and m == 1:
        return 0

    # visited[x][y] = max remaining breaks when visited
    visited = [[-1 for _ in range(m)] for _ in range(n)]
    q = deque()

    # Initialize queue with start cell
    if grid[0][0] == 1:
        if k == 0:
            return -1
        visited[0][0] = k - 1
        q.append((0, 0, k - 1, 0))
    else:
        visited[0][0] = k
        q.append((0, 0, k, 0))

    directions = [(-1, 0), (1, 0), (0, -1), (0, 1)]

    while q:
        x, y, b, steps = q.popleft()

        # Check if we've reached the end
        if x == n - 1 and y == m - 1:
            return steps

        # Explore all neighbors
        for dx, dy in directions:
            nx = x + dx
            ny = y + dy

            if 0 <= nx < n and 0 <= ny < m:
                if grid[nx][ny] == 0:
                    # Move to empty cell without breaking a wall
                    if visited[nx][ny] < b:
                        visited[nx][ny] = b
                        q.append((nx, ny, b, steps + 1))
                else:
                    # Move to wall if we have remaining breaks
                    if b > 0:
                        new_b = b - 1
                        if visited[nx][ny] < new_b:
                            visited[nx][ny] = new_b
                            q.append((nx, ny, new_b, steps + 1))

    # If no path found
    return -1

Explanation

  • State Tracking: The state (x, y, b) ensures we consider both the position and the number of breaks left, which is crucial for handling walls.
  • Efficiency: BFS guarantees the shortest path, and the visited array prevents redundant processing of states, making the solution efficient even for larger grids.
  • Edge Cases: Handles cases like the start and end being the same, walls at the start, and no valid path even with k breaks.

This approach effectively balances exploration and optimization, ensuring we find the shortest path while respecting the wall break constraint. The time complexity is O(nmk) (since each cell can be visited up to k+1 times, once for each possible break count), which is feasible for typical grid sizes. The space complexity is O(nm) for the visited array and O(nm*k) for the queue (worst case). However, in practice, the queue size is much smaller due to the visited array pruning.

熙诚环保科技(苏州)有限公司

熙诚环保科技(苏州)有限公司



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

在线客服

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