-
Notifications
You must be signed in to change notification settings - Fork 3
/
778.cpp
33 lines (30 loc) · 1.12 KB
/
778.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
typedef tuple<int, int, int> T;
class Solution {
public:
vector<pair<int, int>> directions = {{0, 1}, {0, - 1}, {1, 0}, {-1, 0}};
int swimInWater(vector<vector<int>>& grid) {
int n = grid.size();
vector<vector<bool>> visited(n, vector<bool>(n, false));
priority_queue<T, vector<T>, greater<T>> pq;
pq.push(make_tuple(grid[0][0], 0, 0));
int res = 0;
while (!pq.empty()) {
auto t = pq.top();
pq.pop();
int level = get<0>(t);
int x = get<1>(t);
int y = get<2>(t);
res = max(res, level);
if (x == n - 1 && y == n - 1) return res;
for (auto& direction : directions) {
int nextX = x + direction.first;
int nextY = y + direction.second;
if (nextX < 0 || nextX >= n || nextY < 0 || nextY >= n) continue;
if (visited[nextX][nextY]) continue;
pq.push(make_tuple(grid[nextX][nextY], nextX, nextY));
visited[nextX][nextY] = true;
}
}
return -1;
}
};