3341 - Find Minimum Time to Reach Last Room I
info
- 문제 보기: 3341 - Find Minimum Time to Reach Last Room I
- 소요 시간: 17분 41초
- 풀이 언어:
java - 체감 난이도: 3️⃣
- 리뷰 횟수: ✅
풀이 키워드
스포주의
다익스트라
풀이 코드
info
- 메모리: 45000 KB
- 시간: 11 ms
import java.util.*;
class Solution {
int n, m;
int[][] minTime;
int[] dy = {-1, 0, 0, 1};
int[] dx = {0, -1, 1, 0};
public int minTimeToReach(int[][] moveTime) {
this.n = moveTime.length;
this.m = moveTime[0].length;
minTime = new int[n][m];
for (int[] line : minTime) Arrays.fill(line, Integer.MAX_VALUE);
PriorityQueue<int[]> pq = new PriorityQueue<>(Comparator.comparingInt(a -> a[2]));
pq.add(new int[]{0, 0, 0});
minTime[0][0] = 0;
while (!pq.isEmpty()) {
int[] e = pq.poll();
int y = e[0];
int x = e[1];
int t = e[2];
for (int d = 0; d < 4; ++d) {
int ny = y + dy[d];
int nx = x + dx[d];
if (ny < 0 || n <= ny || nx < 0 || m <= nx) continue; // out of bound
int nt = Math.max(t+1, moveTime[ny][nx]+1);
if (minTime[ny][nx] <= nt) continue; // better one exists -> drop this
minTime[ny][nx] = nt;
pq.add(new int[]{ny, nx, nt});
}
}
return minTime[n-1][m-1];
}
}
풀이 해설
각 칸별 입장 시간의 제약조건을 가중치로 치환하여 생각하면 되고,
가중치 그래프에서의 최단 경로는 다익스트라로 찾는다.