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;
}
相关推荐
geovindu2 小时前
python:DecisionTree
开发语言·后端·python·算法
geovindu4 小时前
python: Decision Tree Regressor
开发语言·后端·python·算法·决策树
不脱发的程序猿6 小时前
详解UDS CAN诊断:CommunicationControl Service(SID:0x28)
c语言·can·uds·汽车诊断·iso-tp
AllData公司负责人6 小时前
实战二:AllData大模型与AI数据底座-数据传输平台-DBSyncer,搭建 Http 接口同步至 Doris,API 业务数据自动拉取入数仓
大数据·数据库·数据仓库·人工智能·算法·http·doris
武子康7 小时前
火系被选七成,就应该削弱吗?
人工智能·算法·机器学习
炸膛坦客8 小时前
单片机/C/C++八股:(三十七)指针数组 和 数组指针
c语言·开发语言·c++
夜不会漫长8 小时前
C++:C++11特性
开发语言·c++·算法
秋天的怀念_David8 小时前
算法一千零一夜 · 第三十七夜 · 类型的过路费
java·程序人生·算法
xckkcxxck11 小时前
Kaggle S6E10 冠军方案精读(二):245 个模型的合唱团,和一个只会“线性指挥“的老师
算法