-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswiminRisingWater.cpp
More file actions
31 lines (30 loc) · 1.04 KB
/
swiminRisingWater.cpp
File metadata and controls
31 lines (30 loc) · 1.04 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
// Source: https://leetcode.com/problems/swim-in-rising-water/
// Author: Miao Zhang
// Date: 2021-03-09
class Solution {
public:
int swimInWater(vector<vector<int>>& grid) {
int n = grid.size();
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>> > q;
q.push({grid[0][0], 0 * n + 0});
vector<int> seen(n * n);
vector<vector<int>> dirs{{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
seen[0 * n + 0] = 1;
while (!q.empty()) {
auto node = q.top(); q.pop();
int t = node.first;
int i = node.second / n;
int j = node.second % n;
if (i == n - 1 && j == n - 1) return t;
for (auto d: dirs) {
int x = i + d[0];
int y = j + d[1];
if (x < 0 || x >= n || y < 0 || y >= n) continue;
if (seen[x * n + y]) continue;
seen[x * n + y] = 1;
q.push({max(t, grid[x][y]), x * n + y});
}
}
return -1;
}
};