forked from ghostmkg/dsa-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswimInWater.cpp
More file actions
28 lines (22 loc) · 964 Bytes
/
swimInWater.cpp
File metadata and controls
28 lines (22 loc) · 964 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
int swimInWater(vector<vector<int>>& grid) {
int m = grid.size(), n = grid[0].size();
priority_queue<tuple<int,int,int>, vector<tuple<int,int,int>>, greater<>> pq;
vector<pair<int,int>> directions = {{0,1}, {1,0}, {0,-1}, {-1,0}};
set<pair<int,int>> seen;
pq.push({grid[0][0], 0, 0});
while (!pq.empty()) {
auto [max_d, r, c] = pq.top();
pq.pop();
if (seen.count({r, c})) continue;
seen.insert({r, c});
if (r == m-1 && c == n-1) return max_d;
for (auto [dr, dc] : directions) {
int nr = r + dr, nc = c + dc;
if (nr >= 0 && nr < m && nc >= 0 && nc < n && !seen.count({nr, nc})) {
int new_d = max(max_d, grid[nr][nc]);
pq.push({new_d, nr, nc});
}
}
}
return -1;
}