0-1 BFS
Time O(rows × cols) Space O(rows × cols)dist[0][0] = 0. Pop from the front; for each neighbour with weight w = grid value, relax dist; push front if w = 0, back if w = 1.
import java.util.*;
class Solution {
public int minimumObstacles(int[][] grid) {
int m = grid.length, n = grid[0].length;
int[][] dist = new int[m][n];
for (int[] row : dist) Arrays.fill(row, Integer.MAX_VALUE);
dist[0][0] = 0;
Deque<int[]> dq = new ArrayDeque<>();
dq.offerFirst(new int[]{0, 0});
int[][] dirs = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
while (!dq.isEmpty()) {
int[] cell = dq.pollFirst();
int r = cell[0], c = cell[1];
for (int[] d : dirs) {
int nr = r + d[0], nc = c + d[1];
if (nr < 0 || nc < 0 || nr >= m || nc >= n) continue;
int w = grid[nr][nc];
if (dist[r][c] + w >= dist[nr][nc]) continue;
dist[nr][nc] = dist[r][c] + w;
if (w == 0) dq.offerFirst(new int[]{nr, nc});
else dq.offerLast(new int[]{nr, nc});
}
}
return dist[m - 1][n - 1];
}
}Verdict: Dijkstra's result without the log factor.