-
-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy path778. Swim_in_Rising_Water
35 lines (32 loc) · 984 Bytes
/
778. Swim_in_Rising_Water
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
34
35
class Solution {
public:
int swimInWater(vector<vector<int>>& grid) {
int n=grid.size(),ans=0;
priority_queue<vector<int>,vector<vector<int>>,greater<vector<int>>> q;
unordered_map<int,bool> visited;
vector<int> d={1,0,-1,0,1};
q.push({grid[0][0],0,0});
visited[0]=true;
while(!q.empty())
{
auto p=q.top();
q.pop();
// ans=max(ans,p[0]);
for(int i=0;i<4;i++)
{
int X=p[1]+d[i];
int Y=p[2]+d[i+1];
if(X>=0&&Y>=0&&X<n&&Y<n)
{
if(X==n-1&&Y==n-1)
return max(p[0],grid[n-1][n-1]);
if(!visited[X*n+Y]){
q.push({max(grid[X][Y],p[0]),X,Y});
visited[X*n+Y]=true;
}
}
}
}
return 0;
}
};