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;
}