Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- struct node{
- int x, y, t;
- node(int x, int y, int t): x(x), y(y), t(t){}
- bool operator<(const node &tmp) const{
- return t > tmp.t;
- }
- };
- public:
- int swimInWater(vector<vector<int>>& grid) {
- int ans = 0, rows = grid.size(), cols = grid[0].size();
- int d[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
- bool vis[rows][cols];
- memset(vis, 0, sizeof vis);
- priority_queue<node> q;
- q.push(node(0, 0, grid[0][0]));
- vis[0][0] = true;
- while(!q.empty()){
- node curr = q.top();
- q.pop();
- ans = max(ans, curr.t);
- if(curr.x == rows-1 && curr.y == cols-1)
- return ans;
- for(int i=0; i<4; i++){
- int nextX = curr.x+d[i][0], nextY = curr.y+d[i][1];
- if(nextX < 0 || nextX >= rows || nextY < 0 || nextY >= cols || vis[nextX][nextY])
- continue;
- node next = node(nextX, nextY, grid[nextX][nextY]);
- q.push(next);
- vis[nextX][nextY] = true;
- }
- }
- return ans;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement