forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
32 lines (32 loc) · 1.3 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
class Solution {
public:
int minimumVisitedCells(vector<vector<int>>& grid) {
int m = grid.size(), n = grid[0].size();
vector<vector<int>> dist(m, vector<int>(n, -1));
using pii = pair<int, int>;
priority_queue<pii, vector<pii>, greater<pii>> row[m];
priority_queue<pii, vector<pii>, greater<pii>> col[n];
dist[0][0] = 1;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
while (!row[i].empty() && grid[i][row[i].top().second] + row[i].top().second < j) {
row[i].pop();
}
if (!row[i].empty() && (dist[i][j] == -1 || row[i].top().first + 1 < dist[i][j])) {
dist[i][j] = row[i].top().first + 1;
}
while (!col[j].empty() && grid[col[j].top().second][j] + col[j].top().second < i) {
col[j].pop();
}
if (!col[j].empty() && (dist[i][j] == -1 || col[j].top().first + 1 < dist[i][j])) {
dist[i][j] = col[j].top().first + 1;
}
if (dist[i][j] != -1) {
row[i].emplace(dist[i][j], j);
col[j].emplace(dist[i][j], i);
}
}
}
return dist[m - 1][n - 1];
}
};