-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path778.cpp
More file actions
31 lines (29 loc) · 1004 Bytes
/
Copy path778.cpp
File metadata and controls
31 lines (29 loc) · 1004 Bytes
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
class Solution {
struct Point {
int t, y, x;
friend bool operator<(const Point& a, const Point& b) {
return a.t > b.t;
}
};
public:
int swimInWater(vector<vector<int>>& grid) {
int lg = grid.size();
vector<vector<int>> ex(lg, vector<int>(lg));
priority_queue<Point> q;
q.push({grid[0][0], 0, 0});
int ans = grid[0][0];
while (!q.empty()) {
Point top = q.top(); q.pop();
int x = top.x, y = top.y;
if (ex[y][x]) continue;
ans = max(ans, top.t);
if (x == y && x == lg-1) return ans;
ex[y][x] = 1;
if (y > 0 && ex[y-1][x] == 0) q.push({grid[y-1][x], y-1, x});
if (y < lg-1 && ex[y+1][x] == 0) q.push({grid[y+1][x], y+1, x});
if (x > 0 && ex[y][x-1] == 0) q.push({grid[y][x-1], y, x-1});
if (x < lg-1 && ex[y][x+1] == 0) q.push({grid[y][x+1], y, x+1});
}
return -1;
}
};