Advertisement
pb_jiang

LC 6366 WA

Feb 25th, 2023
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.50 KB | None | 0 0
  1. class Solution {
  2.     using pii = pair<int, int>;
  3. public:
  4.     int minimumTime(vector<vector<int>>& g) {
  5.         int m = g.size(), n = g[0].size();
  6.         if (g[1][0] > 1 && g[0][1] > 1) return -1;
  7.         using a3i = array<int, 3>;
  8.         // queue<a3i> q;
  9.         priority_queue <a3i, vector<a3i>, greater<a3i>> q;
  10.         if (g[1][0] <= 1) q.push({1, 1, 0});
  11.         if (g[0][1] <= 1) q.push({1, 0, 1});
  12.        
  13.         int dir[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
  14.         vector<vector<int>> dist(m, vector<int>(n, INT_MAX / 2));
  15.         dist[0][0] = 0;
  16.         set<pii> vis;
  17.         int ans = INT_MAX;
  18.         while(!q.empty()) {
  19.             auto [ts, x, y] = q.top(); q.pop();
  20.             if (x == m - 1 && y == n - 1) ans = min(ans, dist[x][y]);
  21.             ts = dist[x][y];
  22.             if (vis.count({x, y})) continue;
  23.             vis.insert({x, y});
  24.             for (int i = 0; i < 4; ++i) {
  25.                 int nx = x + dir[i][0], ny = y + dir[i][1];
  26.                 if (nx >= 0 && nx < m && ny >= 0 && ny < n) {
  27.                     if (g[nx][ny] <= ts + 1) {
  28.                         dist[nx][ny] = min(dist[nx][ny], ts + 1);
  29.                         q.push({ts + 1, nx, ny});
  30.                     } else {
  31.                         int nts = (g[nx][ny] - ts - 1) / 2 * 2 + ts;
  32.                         q.push({nts + 1, nx, ny});
  33.                         dist[nx][ny] = min(dist[nx][ny], nts + 1);
  34.                     }
  35.                 }
  36.             }
  37.         }
  38.         return ans;
  39.     }
  40. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement