You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
class Solution {
public int minimumObstacles(int[][] grid) {
int m = grid.length, n = grid[0].length;
PriorityQueue<int[]> pq = new PriorityQueue<>(Comparator.comparingInt(a -> a[2]));
pq.offer(new int[]{0, 0, 0});
int[][] dp = new int[m][n];
for (int i = 0; i < m; i++) {
Arrays.fill(dp[i], m * n);
}
int[][] dirs = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
while (!pq.isEmpty()) {
int[] cur = pq.poll();
if (cur[0] == m - 1 && cur[1] == n - 1) {
return cur[2];
}
for (int[] dir : dirs) {
int x = cur[0] + dir[0], y = cur[1] + dir[1];
if (x < 0 || x >= m || y < 0 || y >= n || cur[2] + grid[x][y] >= dp[x][y]) continue;
dp[x][y] = cur[2] + grid[x][y];
pq.offer(new int[]{x, y, dp[x][y]});
}
}
return dp[m - 1][n - 1];
}
}
这种二维数组迷宫题,一般都用DFS/BFS,但由于时间复杂度,BFS使用多些。
我之前很少做过这种题目,不知道这种有障碍的二维数组怎么做。实际上,做法是,使用BFS,并且记录经过的障碍的数量。
那么接下来要解决两种问题:
解决方法:
这道题解决方法中,首先设置DP数组,存储[i,j]的最小障碍数量,一开始设置为整型最大值,循环中判断到下个点的障碍数量所能否小于dp[i][j]才继续BFS,所以就可以避免环的出现;然后用优先路径以最小障碍数量路径为先。
类似题目:
The text was updated successfully, but these errors were encountered: