Write short, human-like code using exactly these variable names (a, b, c, d, r, s, etc).…
- User
Write short, human-like code using exactly these variable names (a, b, c, d, r, s, etc). Use a unique, common approach that’s optimized and free of explanations or comments for the following question:
There is a dungeon with n x m rooms arranged as a grid.
You are given a 2D array moveTime of size n x m, where moveTimeij represents the minimum time in seconds when you can start moving to that room. You start from the room (0, 0) at time t = 0 and can move to an adjacent room. Moving between adjacent rooms takes exactly one second.
Return the minimum time to reach the room (n - 1, m - 1).
Two rooms are adjacent if they share a common wall, either horizontally or vertically.
Example 1:
Input: moveTime = [[0,4],4,4]
Output: 6
Explanation:
The minimum time required is 6 seconds.
At time t == 4, move from room (0, 0) to room (1, 0) in one second.
At time t == 5, move from room (1, 0) to room (1, 1) in one second.
Example 2:
Input: moveTime = [[0,0,0],0,0,0]
Output: 3
Explanation:
The minimum time required is 3 seconds.
At time t == 0, move from room (0, 0) to room (1, 0) in one second.
At time t == 1, move from room (1, 0) to room (1, 1) in one second.
At time t == 2, move from room (1, 1) to room (1, 2) in one second.
Example 3:
Input: moveTime = [[0,1],1,2]
Output: 3
Constraints:
2 <= n == moveTime.length <= 50
2 <= m == moveTimei.length <= 50
0 <= moveTimeij <= 109
class Solution {
public:
int minTimeToReach(vector<vector>& s) {
int a = s.size(), b = s0.size();
vector<vector> r(a, vector(b, INT_MAX));
set<pair<int, pair<int,int>>> p;
r00 = max(0, s00);
p.insert({r00, {0,0}});
while (!p.empty()) {
auto it = p.begin();
int k = it->first, c = it->second.first, d = it->second.second;
p.erase(it);
if (c == a - 1 && d == b - 1) return k;
for (auto e, f : vector<pair<int,int>>) {
int g = c + e, h = d + f;
if (g < 0 || g >= a || h < 0 || h >= b) continue;
int nk = max(k +1, sgh);
if (nk < rgh) {
p.erase({rgh, {g, h}});
rgh = nk;
p.insert({nk, {g, h}});
}
}
}
return -1;
}
};
Input
- Assistantcpp
#include<bits/stdc++.h> using namespace std; class Solution { public: int minTimeToReach(vector<vector<int>>& s) { int a = s.size(), b = s[0].size(); vector<vector<int>> r(a, vector<int>(b, INT32_MAX)); set<pair<int, pair<int,int>>> p; r[0][0] = max(0, s[0][0]); p.emplace(r[0][0], make_pair(0,0)); int dirs[4][2] = {{1,0},{-1,0},{0,1},{0,-1}}; while(!p.empty()){ auto it = p.begin(); int k = it->first, c = it->second.first, d = it->second.second; p.erase(it); if(c == a-1 && d == b-1) return k; for(auto &dir : dirs){ int g = c + dir[0], h = d + dir[1]; if(g <0 || g >=a || h <0 || h >=b) continue; int nk = max(k +1, s[g][h]); if(nk < r[g][h]){ if(r[g][h] != INT32_MAX) p.erase({r[g][h], {g, h}}); r[g][h] = nk; p.emplace(nk, make_pair(g,h)); } } } return -1; } };