-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.cpp
56 lines (46 loc) · 1.33 KB
/
Solution.cpp
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <algorithm>
#include <climits>
#include <functional>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
class Solution {
private:
int const INF = 10e5; // m * n <= 10e5
vector<pair<int, int>> const directions = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
public:
int minimumObstacles(vector<vector<int>>& grid) {
int rows = grid.size();
int cols = grid[0].size();
vector<vector<int>> distances(rows, vector<int>(cols, INF));
// {numberOfObstaclesInPath, row, col}
priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>,
greater<>>
minHeap;
distances[0][0] = 0;
minHeap.emplace(0, 0, 0);
while (!minHeap.empty()) {
auto [count, r, c] = minHeap.top();
minHeap.pop();
if (r == rows - 1 && c == cols - 1) {
break;
}
for (auto const& [dr, dc] : directions) {
int nr = r + dr;
int nc = c + dc;
if (nr < 0 || nc < 0 || nr >= rows || nc >= cols ||
count + grid[nr][nc] >= distances[nr][nc]) {
continue;
}
distances[nr][nc] = count + grid[nr][nc];
minHeap.emplace(count + grid[nr][nc], nr, nc);
}
}
return distances[rows - 1][cols - 1];
}
};