3342. Find Minimum Time to Reach Last Room II #1658
-
Topics: There is a dungeon with You are given a 2D array Return the minimum time to reach the room Two rooms are adjacent if they share a common wall, either horizontally or vertically. Example 1:
Example 2:
Example 3:
Constraints:
Hint:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
We need to find the minimum time required to reach the last room in a grid-based dungeon where each move alternates between taking 1 second and 2 seconds. Additionally, each room has a minimum time before you can start moving into it. The solution involves using a modified Dijkstra's algorithm to handle the alternating move times and the minimum start times for each room. Approach
Let's implement this solution in PHP: 3342. Find Minimum Time to Reach Last Room II <?php
/**
* @param Integer[][] $moveTime
* @return Integer
*/
function minTimeToReach($moveTime) {
$n = count($moveTime);
$m = count($moveTime[0]);
$dirs = [[-1, 0], [1, 0], [0, -1], [0, 1]];
$INF = PHP_INT_MAX;
// Initialize distance array with INF for all (i,j,p)
$dist = array_fill(0, $n, []);
for ($i = 0; $i < $n; $i++) {
for ($j = 0; $j < $m; $j++) {
$dist[$i][$j] = [$INF, $INF];
}
}
$dist[0][0][0] = 0;
$pq = new SplPriorityQueue();
$pq->setExtractFlags(SplPriorityQueue::EXTR_BOTH);
$pq->insert([0, 0, 0, 0], -0);
while (!$pq->isEmpty()) {
$element = $pq->extract();
$currentTime = $element['data'][0];
$i = $element['data'][1];
$j = $element['data'][2];
$p = $element['data'][3];
// Check if we have reached the destination
if ($i == $n - 1 && $j == $m - 1) {
return $currentTime;
}
// Skip if a shorter path to this state was already found
if ($currentTime > $dist[$i][$j][$p]) {
continue;
}
foreach ($dirs as $d) {
$ni = $i + $d[0];
$nj = $j + $d[1];
// Check if the neighbor is within bounds
if ($ni < 0 || $ni >= $n || $nj < 0 || $nj >= $m) {
continue;
}
// Determine the cost based on the current parity
$cost = ($p == 0) ? 1 : 2;
$mt = $moveTime[$ni][$nj];
$departureTime = max($currentTime, $mt);
$arrivalTime = $departureTime + $cost;
$newP = 1 - $p;
// Update the distance if a shorter path is found
if ($arrivalTime < $dist[$ni][$nj][$newP]) {
$dist[$ni][$nj][$newP] = $arrivalTime;
$pq->insert([$arrivalTime, $ni, $nj, $newP], -$arrivalTime);
}
}
}
// The problem guarantees a path exists, so this is a fallback
return -1;
}
// Example usage:
$moveTime = [[0, 4], [4, 4]];
echo minTimeToReach($moveTime); // Output: 7
$moveTime = [[0,0,0,0],[0,0,0,0]];
echo minTimeToReach($moveTime); // Output: 6
$moveTime = [[0,1],[1,2]];
echo minTimeToReach($moveTime); // Output: 4
?> Explanation:
This approach efficiently finds the shortest path using Dijkstra's algorithm with state tracking for move parity, ensuring optimal time complexity and handling the constraints effectively. |
Beta Was this translation helpful? Give feedback.
We need to find the minimum time required to reach the last room in a grid-based dungeon where each move alternates between taking 1 second and 2 seconds. Additionally, each room has a minimum time before you can start moving into it. The solution involves using a modified Dijkstra's algorithm to handle the alternating move times and the minimum start times for each room.
Approach