3341. Find Minimum Time to Reach Last Room I #1654
-
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 determine the minimum time required to reach the last room of a dungeon grid starting from the top-left corner (0,0) at time 0. Each room has a minimum time requirement before you can start moving into it, and moving between adjacent rooms takes exactly one second. ApproachThe problem can be efficiently solved using Dijkstra's algorithm, which is typically used to find the shortest path in a graph with non-negative weights. In this context, each room is a node, and the edges between nodes represent the time required to move from one room to another. The key insight is to model the arrival time at each room as the distance in Dijkstra's algorithm, considering both the movement time and the minimum time requirement of each room.
Let's implement this solution in PHP: 3341. Find Minimum Time to Reach Last Room I <?php
/**
* @param Integer[][] $moveTime
* @return Integer
*/
function minTimeToReach($moveTime) {
$n = count($moveTime);
$m = count($moveTime[0]);
// Initialize distance array with PHP_INT_MAX
$dist = array();
for ($i = 0; $i < $n; $i++) {
$dist[$i] = array_fill(0, $m, PHP_INT_MAX);
}
$dist[0][0] = 0;
$pq = new SplPriorityQueue();
$pq->setExtractFlags(SplPriorityQueue::EXTR_DATA);
$pq->insert(array(0, 0, 0), -0);
$dirs = array(array(-1, 0), array(1, 0), array(0, -1), array(0, 1));
while (!$pq->isEmpty()) {
$current = $pq->extract();
$currentTime = $current[0];
$x = $current[1];
$y = $current[2];
// Check if current cell is the destination
if ($x == $n - 1 && $y == $m - 1) {
return $currentTime;
}
// Skip if a shorter path to this cell was already found
if ($currentTime > $dist[$x][$y]) {
continue;
}
// Explore all four directions
foreach ($dirs as $dir) {
$nx = $x + $dir[0];
$ny = $y + $dir[1];
if ($nx >= 0 && $nx < $n && $ny >= 0 && $ny < $m) {
// Calculate the new arrival time
$newTime = max($currentTime, $moveTime[$nx][$ny]) + 1;
// Update if this path is better
if ($newTime < $dist[$nx][$ny]) {
$dist[$nx][$ny] = $newTime;
$pq->insert(array($newTime, $nx, $ny), -$newTime);
}
}
}
}
// The destination is guaranteed to be reachable per problem constraints
return $dist[$n - 1][$m - 1];
}
// Test Example
$moveTime1 = [[0, 4], [4, 4]];
$moveTime2 = [[0, 0, 0], [0, 0, 0]];
$moveTime3 = [[0, 1], [1, 2]];
echo minTimeToReach($moveTime1) . "\n"; // 6
echo minTimeToReach($moveTime2) . "\n"; // 3
echo minTimeToReach($moveTime3) . "\n"; // 3
?> Explanation:
This approach efficiently finds the shortest path using Dijkstra's algorithm, ensuring that we always explore the shortest path first and update our distances accordingly. The solution handles all edge cases and constraints effectively. |
Beta Was this translation helpful? Give feedback.
We need to determine the minimum time required to reach the last room of a dungeon grid starting from the top-left corner (0,0) at time 0. Each room has a minimum time requirement before you can start moving into it, and moving between adjacent rooms takes exactly one second.
Approach
The problem can be efficiently solved using Dijkstra's algorithm, which is typically used to find the shortest path in a graph with non-negative weights. In this context, each room is a node, and the edges between nodes represent the time required to move from one room to another. The key insight is to model the arrival time at each room as the distance in Dijkstra's algorithm, considering both the movement …