https://leetcode.com/problems/find-minimum-time-to-reach-last-room-ii/description
3342. Find Minimum Time to Reach Last Room II
思路:
沒啥好說的昨天那題小改一下,本來走到隔壁房間要花1單位時間,現在改成1、2、1、2。
Java Code:
------------------------------------------
class Solution {
private static final int[][] dirs = new int[][]{{0, 1}, {0, -1}, {1, 0},
{-1, 0}};
public int minTimeToReach(int[][] moveTime) {
int n = moveTime.length;
int m = moveTime[0].length;
int[][] dis = new int[n][m];
for (int i = 0; i < n; i++) {
Arrays.fill(dis[i], Integer.MAX_VALUE);
}
dis[0][0] = 0;
PriorityQueue<int[]> pq = new
PriorityQueue<>(Comparator.comparingInt(a -> a[2]));
pq.add(new int[]{0, 0, 0, 2});
while (!pq.isEmpty()) {
int[] p = pq.poll();
int y = p[0];
int x = p[1];
int cost = p[2];
int round = p[3];
for (int[] dir : dirs) {
int newY = y + dir[0];
int newX = x + dir[1];
if (newX < 0 || newY < 0 || newX >= m || newY >= n) continue;
int nextRound = round == 2 ? 1 : 2;
int nextCost = Math.max(cost, moveTime[newY][newX]) +
nextRound;
if (nextCost < dis[newY][newX]) {
dis[newY][newX] = nextCost;
pq.add(new int[]{newY, newX, nextCost, nextRound});
}
}
}
return dis[n - 1][m - 1];
}
}
------------------------------------------
--
https://i.imgur.com/5xKbxoh.jpeg
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 49.159.104.111 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1746709622.A.67D.html