RainX_69

Minimum Time to Visit a Cell In a Grid following Rules | OA | MUST DO | Dijkstra Graph

May 11th, 2023
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.94 KB | Source Code | 0 0
  1. https://leetcode.com/problems/minimum-time-to-visit-a-cell-in-a-grid/
  2.  
  3. You are given a m x n matrix grid consisting of non-negative integers where grid[row][col] represents the minimum time required to be able to visit the cell (row, col), which means you can visit the cell (row, col) only when the time you visit it is greater than or equal to grid[row][col].
  4. You are standing in the top-left cell of the matrix in the 0th second, and you must move to any adjacent cell in the four directions: up, down, left, and right. Each move you make takes 1 second.
  5. Return the minimum time required in which you can visit the bottom-right cell of the matrix. If you cannot visit the bottom-right cell, then return -1.
  6.  
  7.  
  8.  
  9. Example 1:
  10. Input: grid = [[0,1,3,2],[5,1,2,5],[4,3,8,6]]
  11. Output: 7
  12. Explanation: One of the paths that we can take is the following:
  13. - at t = 0, we are on the cell (0,0).
  14. - at t = 1, we move to the cell (0,1). It is possible because grid[0][1] <= 1.
  15. - at t = 2, we move to the cell (1,1). It is possible because grid[1][1] <= 2.
  16. - at t = 3, we move to the cell (1,2). It is possible because grid[1][2] <= 3.
  17. - at t = 4, we move to the cell (1,1). It is possible because grid[1][1] <= 4.
  18. - at t = 5, we move to the cell (1,2). It is possible because grid[1][2] <= 5.
  19. - at t = 6, we move to the cell (1,3). It is possible because grid[1][3] <= 6.
  20. - at t = 7, we move to the cell (2,3). It is possible because grid[2][3] <= 7.
  21. The final time is 7. It can be shown that it is the minimum time possible.
  22.  
  23. Example 2
  24. Input: grid = [[0,2,4],[3,2,1],[1,0,4]]
  25. Output: -1
  26. Explanation: There is no path from the top left to the bottom-right cell.
  27.  
  28.  
  29. Constraints:
  30. m == grid.length
  31. n == grid[i].length
  32. 2 <= m, n <= 1000
  33. 4 <= m * n <= 10^5
  34. 0 <= grid[i][j] <= 10^5
  35. grid[0][0] == 0
  36.  
  37. --------------------------------------------------------------------------------------------------------------------------------------
  38.  
  39. class Solution {
  40. public:
  41.     int minimumTime(vector<vector<int>>& grid) {
  42.         int n=grid.size();
  43.         int m=grid[0].size();
  44.        
  45.         int X[4]={-1,1,0,0};
  46.         int Y[4]={0,0,-1,1};
  47.        
  48.         if(grid[0][1]>1 && grid[1][0]>1){ // passing from here makes sure oscillation will always be possible
  49.             return -1;
  50.         }
  51.        
  52.         priority_queue<vector<long long>,vector<vector<long long>>,greater<vector<long long>>> pq;
  53.         vector<vector<long long>> dist(n,vector<long long>(m,INT_MAX));
  54.        
  55.         pq.push({0,0,0});
  56.         dist[0][0]=0;
  57.        
  58.         while(!pq.empty()){
  59.             auto curr=pq.top();
  60.             pq.pop();
  61.                
  62.             long long t=curr[0];
  63.             long long x=curr[1];
  64.             long long y=curr[2];
  65.            
  66.             if(dist[x][y]<t){
  67.                 continue;
  68.             }
  69.                
  70.             for(int i=0;i<4;i++){
  71.                 int newX=X[i]+x;
  72.                 int newY=Y[i]+y;
  73.                
  74.                 if(newX<0 || newY<0 || newX==n || newY==m){
  75.                     continue;
  76.                 }
  77.                
  78.                 if(t+1>=grid[newX][newY] && dist[newX][newY]>t+1){
  79.                     dist[newX][newY]=t+1;
  80.                     pq.push({t+1,newX,newY});  
  81.                 }
  82.                 else if(grid[newX][newY]>dist[x][y]){
  83.                     int difference=grid[newX][newY]-dist[x][y];
  84.                     int required=0;
  85.                     if(difference%2==0){
  86.                         required=difference+dist[x][y]+1;
  87.                     }
  88.                     else{
  89.                         required=difference+dist[x][y];
  90.                     }
  91.                     if(dist[newX][newY]>required){
  92.                         pq.push({required,newX,newY});
  93.                         dist[newX][newY]=required;
  94.                     }
  95.                 }
  96.             }
  97.         }
  98.         return dist[n-1][m-1]==INT_MAX ? -1 : dist[n-1][m-1];
  99.     }
  100. };
  101.  
  102.  
  103. // BRUTE FORCE
  104. class Solution {
  105. public:
  106.     int minimumTime(vector<vector<int>>& grid) {
  107.         queue<vector<int>> q;
  108.         int t=0;
  109.         q.push({0,0,0});
  110.         int X[4]={-1,1,0,0};
  111.         int Y[4]={0,0,-1,1};
  112.         while(!q.empty()){
  113.             int size=q.size();
  114.             while(size--){
  115.                 auto curr=q.front();
  116.                 q.pop();
  117.                
  118.                 int x=curr[0];
  119.                 int y=curr[1];
  120.                 int t=curr[2];
  121.                
  122.                 if(x==grid.size()-1 && y==grid[0].size()-1){
  123.                     return t;
  124.                 }
  125.                
  126.                 for(int i=0;i<4;i++){
  127.                     int newX=X[i]+x;
  128.                     int newY=Y[i]+y;
  129.                     if(newX<0 || newY<0 || newX==grid.size() || newY==grid[0].size() || t+1<grid[newX][newY]){
  130.                         continue;
  131.                     }
  132.                     q.push({newX,newY,t+1});
  133.                 }
  134.             }
  135.         }
  136.         return -1;
  137.     }
  138. };//BRUTE FORCE
Advertisement
Add Comment
Please, Sign In to add comment