-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path3342.cpp
More file actions
34 lines (33 loc) · 1.3 KB
/
3342.cpp
File metadata and controls
34 lines (33 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
typedef pair<int, pair<int, pair<int, int>>> P; // time, step, x, y
class Solution {
public:
vector<pair<int, int>> directions = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
int minTimeToReach(vector<vector<int>>& moveTime) {
int n = moveTime.size();
int m = moveTime[0].size();
priority_queue<P, vector<P>, greater<P>> q; // <time, <x, y>>
q.push({0, {2, {0, 0}}});
while (!q.empty()) {
P node = q.top();
q.pop();
int time = node.first;
int step = node.second.first;
int x = node.second.second.first;
int y = node.second.second.second;
if (moveTime[x][y] == -1) continue;
moveTime[x][y] = -1;
if (x == n - 1 && y == m - 1) return time;
for (auto& direction : directions) {
int xx = x + direction.first;
int yy = y + direction.second;
if (xx < 0 || xx >= n || yy < 0 || yy >= m) continue;
if (moveTime[xx][yy] == -1) continue;
int stepTime = 1;
if (step == 1) stepTime = 2;
int nextTime = max(time + stepTime, moveTime[xx][yy] + stepTime);
q.push({nextTime, {stepTime, {xx, yy}}});
}
}
return -1;
}
};