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 mostkobstacles. If it is not possible to find such walk return -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:
1
2
3
4
5
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.
We need to find the shortest path from the top-left to the bottom-right of a grid, where we can eliminate up to k obstacles. This is a classic BFS problem, but we must track not only the position but also how many obstacles we have eliminated so far.
classSolution {
public:int shortestPath(vector<vector<int>>& grid, int k) {
int m = grid.size(), n = grid[0].size();
vector<vector<int>> vis(m, vector<int>(n, -1));
queue<tuple<int, int, int>> q;
q.push({0, 0, 0});
vis[0][0] =0;
int steps =0;
int dirs[4][2] = {{0,1},{1,0},{0,-1},{-1,0}};
while (!q.empty()) {
int sz = q.size();
for (int i =0; i < sz; ++i) {
auto [x, y, obs] = q.front(); q.pop();
if (x == m-1&& y == n-1) return steps;
for (auto& d : dirs) {
int nx = x + d[0], ny = y + d[1];
if (nx >=0&& nx < m && ny >=0&& ny < n) {
int nobs = obs + grid[nx][ny];
if (nobs <= k && (vis[nx][ny] ==-1|| nobs < vis[nx][ny])) {
vis[nx][ny] = nobs;
q.push({nx, ny, nobs});
}
}
}
}
steps++;
}
return-1;
}
};
classSolution {
publicintshortestPath(int[][] grid, int k) {
int m = grid.length, n = grid[0].length;
int[][] vis =newint[m][n];
for (int[] row : vis) Arrays.fill(row, -1);
Queue<int[]> q =new LinkedList<>();
q.offer(newint[]{0, 0, 0});
vis[0][0]= 0;
int steps = 0;
int[][] dirs = {{0,1},{1,0},{0,-1},{-1,0}};
while (!q.isEmpty()) {
int sz = q.size();
for (int i = 0; i < sz; i++) {
int[] cur = q.poll();
int x = cur[0], y = cur[1], obs = cur[2];
if (x == m-1 && y == n-1) return steps;
for (int[] d : dirs) {
int nx = x + d[0], ny = y + d[1];
if (nx >= 0 && nx < m && ny >= 0 && ny < n) {
int nobs = obs + grid[nx][ny];
if (nobs <= k && (vis[nx][ny]==-1 || nobs < vis[nx][ny])) {
vis[nx][ny]= nobs;
q.offer(newint[]{nx, ny, nobs});
}
}
}
}
steps++;
}
return-1;
}
}
classSolution {
funshortestPath(grid: Array<IntArray>, k: Int): Int {
val m = grid.size
val n = grid[0].size
val vis = Array(m) { IntArray(n) { -1 } }
val q = ArrayDeque<Triple<Int, Int, Int>>()
q.add(Triple(0, 0, 0))
vis[0][0] = 0var steps = 0val dirs = arrayOf(0 to 1, 1 to 0, 0 to -1, -1 to 0)
while (q.isNotEmpty()) {
repeat(q.size) {
val(x, y, obs) = q.removeFirst()
if (x == m-1&& y == n-1) return steps
for ((dx, dy) in dirs) {
val nx = x + dx
val ny = y + dy
if (nx in0 until m && ny in0 until n) {
val nobs = obs + grid[nx][ny]
if (nobs <= k && (vis[nx][ny] == -1|| nobs < vis[nx][ny])) {
vis[nx][ny] = nobs
q.add(Triple(nx, ny, nobs))
}
}
}
}
steps++ }
return -1 }
}
classSolution:
defshortestPath(self, grid: list[list[int]], k: int) -> int:
from collections import deque
m, n = len(grid), len(grid[0])
vis = [[-1]*n for _ in range(m)]
q = deque([(0, 0, 0)])
vis[0][0] =0 steps =0 dirs = [(0,1),(1,0),(0,-1),(-1,0)]
while q:
for _ in range(len(q)):
x, y, obs = q.popleft()
if x == m-1and y == n-1:
return steps
for dx, dy in dirs:
nx, ny = x+dx, y+dy
if0<= nx < m and0<= ny < n:
nobs = obs + grid[nx][ny]
if nobs <= k and (vis[nx][ny] ==-1or nobs < vis[nx][ny]):
vis[nx][ny] = nobs
q.append((nx, ny, nobs))
steps +=1return-1