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;
}
相关推荐
文丑颜不良啊1 小时前
LeetCode1941~1960题目解析
java·leetcode
老师我太想进步了20264 小时前
【数据结构-4】算法美学之顺序表
数据结构·算法
风影同志783245 小时前
编码器是激活函数吗?—— 一个看起来很像、其实不是的类比
算法
UIU1145 小时前
P1481 魔族密码
c++·算法·动态规划
文丑颜不良啊5 小时前
LeetCode1961~1980题目解析
java·leetcode
文丑颜不良啊6 小时前
LeetCode2001~2020题目解析
java·leetcode
Zwawa7 小时前
两根线怎么挂住一堆设备:I2C 协议原理
c语言·单片机·嵌入式
加宝的起点雷达8 小时前
40 条候选里 38 条来自同一个源,主卡整屏重样
人工智能·算法·创业
秋天的怀念_David8 小时前
算法一千零一夜 · 第二十六夜 协议的母语
java·spring boot·算法