Skip to content

Commit 88c4337

Browse files
Create 3341. Find Minimum Time to Reach Last Room I.cpp
1 parent c76d86c commit 88c4337

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
using ll = long long;
4+
struct Node {
5+
int r, c;
6+
ll t;
7+
};
8+
struct Cmp {
9+
bool operator()(const Node &a, const Node &b) const {
10+
return a.t > b.t;
11+
}
12+
};
13+
ll minTimeToReach(vector<vector<ll>>& moveTime) {
14+
int n = moveTime.size();
15+
int m = moveTime[0].size();
16+
const ll INF = LLONG_MAX / 4;
17+
vector<vector<ll>> dist(n, vector<ll>(m, INF));
18+
priority_queue<Node, vector<Node>, Cmp> pq;
19+
dist[0][0] = 0;
20+
pq.push({0, 0, 0});
21+
int dr[4] = {1, -1, 0, 0};
22+
int dc[4] = {0, 0, 1, -1};
23+
while (!pq.empty()) {
24+
Node cur = pq.top();
25+
pq.pop();
26+
if (cur.t > dist[cur.r][cur.c]) continue;
27+
if (cur.r == n - 1 && cur.c == m - 1) return cur.t;
28+
for (int i = 0; i < 4; i++) {
29+
int nr = cur.r + dr[i];
30+
int nc = cur.c + dc[i];
31+
if (nr < 0 || nr >= n || nc < 0 || nc >= m) continue;
32+
ll arrival = max(moveTime[nr][nc], cur.t) + 1;
33+
if (arrival < dist[nr][nc]) {
34+
dist[nr][nc] = arrival;
35+
pq.push({nr, nc, arrival});
36+
}
37+
}
38+
}
39+
return dist[n - 1][m - 1];
40+
}
41+
int main() {
42+
int n, m;
43+
cin >> n >> m;
44+
vector<vector<ll>> moveTime(n, vector<ll>(m));
45+
for (int i = 0; i < n; i++) {
46+
for (int j = 0; j < m; j++) {
47+
cin >> moveTime[i][j];
48+
}
49+
}
50+
ll res = minTimeToReach(moveTime);
51+
cout << res << "\n";
52+
return 0;
53+
}

0 commit comments

Comments
 (0)