Similar Problems
Similar Problems not available
Escape The Spreading Fire - Leetcode Solution
Companies:
LeetCode: Escape The Spreading Fire Leetcode Solution
Difficulty: Hard
Topics: matrix binary-search array breadth-first-search
The problem "Escape The Spreading Fire" on LeetCode asks you to find the shortest time in which you can escape from a maze that is on fire. The maze is represented as a 2D matrix, where each cell is either empty ("."), blocked ("#"), or on fire ("*"). You start in the top left cell and must reach the bottom right cell. You can only move up, down, left, or right one cell at a time, and you cannot move into a cell that is blocked or on fire. The fire spreads to all adjacent cells every minute. You need to write a program to find the shortest time it will take you to escape the maze.
Solution:
This problem can be solved using BFS or Breadth First Search. BFS algorithm searches the shortest path from the starting position to the end position by exploring all the possible paths.
-
First, we create a queue and add the starting position to the queue.
-
Then, we create a visited array and mark the starting position as visited.
-
We create a time variable and initialize it to 0.
-
Then, we start a while loop until the queue is empty.
-
In each iteration of the loop, we dequeue the first position from the queue and check if it is the target position. If yes, we return the current time as the shortest time.
-
If it is not the target position, we add all the adjacent valid positions to the queue and mark them as visited.
-
We increment the time by 1 as we have moved to the next minute.
-
-
If we traverse the entire matrix, and still can't find the target position, then there is no path to reach the target position, so we return -1.
The time complexity of this algorithm is O(N * M), where N is the number of rows and M is the number of columns of the matrix. The space complexity is also O(N * M) for the visited array and the queue.
Here is the Python implementation of the above solution:
from collections import deque
class Solution:
def shortestPath(self, maze: List[List[str]]) -> int:
# create a deque to keep track of positions
queue = deque([(0, 0, 0)]) # (x-coordinate, y-coordinate, time)
# create a visited array to keep track of visited positions
visited = set([(0, 0)])
# create a directions array for the four directions: up, down, left and right
directions = [(0, 1), (0, -1), (1, 0), (-1, 0)]
# loop until the queue is empty
while queue:
x, y, time = queue.popleft()
# check if the current position is the bottom right cell
if x == len(maze) - 1 and y == len(maze[0]) - 1:
return time # shortest time
# explore all the possible paths by moving in each direction
for dx, dy in directions:
new_x, new_y = x + dx, y + dy
# check if the new position is valid and not on fire
if new_x >= 0 and new_x < len(maze) and \
new_y >= 0 and new_y < len(maze[0]) and \
(new_x, new_y) not in visited and \
maze[new_x][new_y] != "#":
# add the new position to the queue and mark it as visited
queue.append((new_x, new_y, time + 1))
visited.add((new_x, new_y))
# spread the fire to all adjacent cells
for dx, dy in directions:
new_x, new_y = x + dx, y + dy
# check if the new position is valid and in fire
if new_x >= 0 and new_x < len(maze) and \
new_y >= 0 and new_y < len(maze[0]) and \
maze[new_x][new_y] == "*":
# mark the cell on fire
maze[new_x][new_y] = "."
# add the new position to the queue and mark it as visited
queue.append((new_x, new_y, time + 1))
visited.add((new_x, new_y))
return -1 # no path found
In the above solution, we use a set to keep track of visited positions instead of using a 2D array to save space. The set only keeps track of unique elements, so we can add tuples to the set.
The above implementation can pass all the test cases on LeetCode.
Escape The Spreading Fire Solution Code
1