LeetCode //C - 1293. Shortest Path in a Grid with Obstacles Elimination

1293. Shortest Path in a Grid with Obstacles Elimination

You are given an m x n integer matrix grid where each cell is either 0 (empty) or 1 (obstacle). You can move up, down, left, or right from and to an empty cell in one step.

Return the minimum number of steps to walk from the upper left corner (0, 0) to the lower right corner (m - 1, n - 1) given that you can eliminate at most k obstacles. If it is not possible to find such walk return -1.

Example 1:

Input: grid = \[0,0,0,1,1,0,0,0,0,0,1,1,0,0,0], k = 1

Output: 6

Explanation:

The shortest path without eliminating any obstacle is 10.

The shortest path with one obstacle elimination at position (3,2) is 6. Such path is (0,0) -> (0,1) -> (0,2) -> (1,2) -> (2,2) -> (3,2) -> (4,2).

Example 2:

Input: grid = \[0,1,1,1,1,1,1,0,0], k = 1

Output: -1

Explanation: We need to eliminate at least two obstacles to find such a walk.

Constraints:
  • m == grid.length
  • n == gridi.length
  • 1 <= m, n <= 40
  • 1 <= k <= m * n
  • gridij is either 0 or 1.
  • grid00 == gridm - 1n - 1 == 0

From: LeetCode

Link: 1293. Shortest Path in a Grid with Obstacles Elimination


Solution:

Ideas:

Use BFS, and for each cell record the maximum number of obstacle eliminations remaining when that cell is reached.

Code:
c 复制代码
#include <stdlib.h>

typedef struct {
    int row;
    int col;
    int remaining;
} State;

int shortestPath(int** grid, int gridSize, int* gridColSize, int k) {
    int rows = gridSize;
    int cols = gridColSize[0];

    if (rows == 1 && cols == 1) {
        return 0;
    }

    /* A direct Manhattan path needs rows + cols - 2 steps. */
    if (k >= rows + cols - 2) {
        return rows + cols - 2;
    }

    /*
     * best[r][c] stores the greatest number of eliminations remaining
     * when reaching cell (r, c).
     */
    int** best = malloc(rows * sizeof(int*));

    for (int i = 0; i < rows; i++) {
        best[i] = malloc(cols * sizeof(int));

        for (int j = 0; j < cols; j++) {
            best[i][j] = -1;
        }
    }

    int capacity = rows * cols * (k + 1);
    State* queue = malloc(capacity * sizeof(State));

    int front = 0;
    int rear = 0;

    queue[rear++] = (State){0, 0, k};
    best[0][0] = k;

    int directions[4][2] = {
        {1, 0},
        {-1, 0},
        {0, 1},
        {0, -1}
    };

    int steps = 0;

    while (front < rear) {
        int levelSize = rear - front;

        for (int i = 0; i < levelSize; i++) {
            State current = queue[front++];

            if (current.row == rows - 1 &&
                current.col == cols - 1) {
                for (int r = 0; r < rows; r++) {
                    free(best[r]);
                }

                free(best);
                free(queue);

                return steps;
            }

            for (int d = 0; d < 4; d++) {
                int newRow = current.row + directions[d][0];
                int newCol = current.col + directions[d][1];

                if (newRow < 0 || newRow >= rows ||
                    newCol < 0 || newCol >= cols) {
                    continue;
                }

                int newRemaining =
                    current.remaining - grid[newRow][newCol];

                if (newRemaining < 0) {
                    continue;
                }

                /*
                 * Skip this state if the same cell was previously reached
                 * with at least as many eliminations remaining.
                 */
                if (best[newRow][newCol] >= newRemaining) {
                    continue;
                }

                best[newRow][newCol] = newRemaining;

                queue[rear++] = (State){
                    newRow,
                    newCol,
                    newRemaining
                };
            }
        }

        steps++;
    }

    for (int i = 0; i < rows; i++) {
        free(best[i]);
    }

    free(best);
    free(queue);

    return -1;
}
相关推荐
Nil2081 小时前
leetcode 64最小路径和
算法·leetcode·职场和发展
光影少年1 小时前
AI Agent、自动化与未来工作
人工智能·算法·架构
牧羊人.3332 小时前
计算机视觉基础 第16章|EigenFace,FisherFace,LBPH传统人脸识别算法
人工智能·算法·计算机视觉
jeo陽2 小时前
04-循环实战:素数、最大公约数、级数求和与枚举法
c语言·开发语言·数据结构
Nil2082 小时前
leetcode 72编辑距离
算法·leetcode·职场和发展
神工坊2 小时前
技术分享︱SIMFORGE HSF® 网格加密与热点加速技术实践
算法·hpc·网格·cae·工程仿真·数值引擎·samr
Nil2082 小时前
leetcode 136只出现一次的数字
leetcode·哈希算法·散列表
CV@CV2 小时前
世界模型与智能体协同推理逻辑
人工智能·算法·边缘计算·量子计算·具身智能
jiayoushijie-泽宣3 小时前
SHIFT-PathFormer:专家轨迹蒸馏、连续安全优化与超大迷宫实验
算法