You are given a 2D grid initialized with these three possible values:
-1 - A water cell that can not be traversed.0 - A treasure chest.INF - A land cell that can be traversed. We use the integer 2^31 - 1 = 2147483647 to represent INF.Fill each land cell with the distance to its nearest treasure chest. If a land cell cannot reach a treasure chest then the value should remain INF.
Assume the grid can only be traversed up, down, left, or right.
Modify the grid in-place.
Example 1:
Input: [
[2147483647,-1,0,2147483647],
[2147483647,2147483647,2147483647,-1],
[2147483647,-1,2147483647,-1],
[0,-1,2147483647,2147483647]
]
Output: [
[3,-1,0,1],
[2,2,1,-1],
[1,-1,2,-1],
[0,-1,3,4]
]Example 2:
Input: [
[0,-1],
[2147483647,2147483647]
]
Output: [
[0,-1],
[1,2]
]Constraints:
m == grid.lengthn == grid[i].length1 <= m, n <= 100grid[i][j] is one of {-1, 0, 2147483647}
You should aim for a solution with O(m * n) time and O(m * n) space, where m is the number of rows and n is the number of columns in the given grid.
A brute force solution would be to iterate on each land cell and run a BFS from that cells to find the nearest treasure chest. This would be an O((m * n)^2) solution. Can you think of a better way? Sometimes it is not optimal to go from source to destination.
We can see that instead of going from every cell to find the nearest treasure chest, we can do it in reverse. We can just do a BFS from all the treasure chests in grid and just explore all possible paths from those chests. Why? Because in this approach, the treasure chests self mark the cells level by level and the level number will be the distance from that cell to a treasure chest. We don't revisit a cell. This approach is called Multi-Source BFS. How would you implement it?
We insert all the cells (row, col) that represent the treasure chests into the queue. Then, we process the cells level by level, handling all the current cells in the queue at once. For each cell, we mark it as visited and store the current level value as the distance at that cell. We then try to add the neighboring cells (adjacent cells) to the queue, but only if they have not been visited and are land cells.
Before attempting this problem, you should be comfortable with:
For every empty cell (INF), we try to find the shortest path to any treasure (0) by exploring all 4 directions using backtracking DFS.
0), a wall (-1), or goes out of bounds.visited grid so the current path doesn't revisit cells (prevents infinite loops in cycles).This works but is slow because we repeat DFS from many cells and re-explore the same areas again and again.
INF represent empty rooms that need a distance.INF, run DFS(cell) to compute the minimum distance to a 0.DFS(r, c):-1) / already visited → return INF.0) → return 0.(r, c) as visited.min(1 + DFS(neighbor))(r, c) (backtrack).grid[r][c] with the returned minimum distance.class Solution:
def islandsAndTreasure(self, grid: List[List[int]]) -> None:
ROWS, COLS = len(grid), len(grid[0])
directions = [(1, 0), (-1, 0), (0, 1), (0, -1)]
INF = 2147483647
visit = [[False for _ in range(COLS)] for _ in range(ROWS)]
def dfs(r, c):
if (r < 0 or c < 0 or r >= ROWS or
c >= COLS or grid[r][c] == -1 or
visit[r][c]):
return INF
if grid[r][c] == 0:
return 0
visit[r][c] = True
res = INF
for dx, dy in directions:
res = min(res, 1 + dfs(r + dx, c + dy))
visit[r][c] = False
return res
for r in range(ROWS):
for c in range(COLS):
if grid[r][c] == INF:
grid[r][c] = dfs(r, c)Where is the number of rows and is the number of columns in the .
BFS is perfect for shortest path in an unweighted grid.
From one empty cell (INF), we expand level-by-level (distance 0, 1, 2, ...). The first time we reach a treasure cell (0), we are guaranteed that distance is the minimum steps needed.
This approach runs a BFS separately for every INF cell, so it's simpler to think about, but can still be slow because many BFS runs repeat the same work.
INF represent empty rooms.(r, c) in the grid:grid[r][c] == INF, run BFS(r, c) to find the nearest treasure distance.grid[r][c] with that returned distance.BFS(sr, sc):(sr, sc) and a visited[ROWS][COLS].steps = 0.grid[row][col] == 0), return steps.-1)) into the queue.steps.INF (no reachable treasure).class Solution:
def islandsAndTreasure(self, grid: List[List[int]]) -> None:
ROWS, COLS = len(grid), len(grid[0])
directions = [(1, 0), (-1, 0), (0, 1), (0, -1)]
INF = 2147483647
def bfs(r, c):
q = deque([(r, c)])
visit = [[False] * COLS for _ in range(ROWS)]
visit[r][c] = True
steps = 0
while q:
for _ in range(len(q)):
row, col = q.popleft()
if grid[row][col] == 0:
return steps
for dr, dc in directions:
nr, nc = row + dr, col + dc
if (0 <= nr < ROWS and 0 <= nc < COLS and
not visit[nr][nc] and grid[nr][nc] != -1
):
visit[nr][nc] = True
q.append((nr, nc))
steps += 1
return INF
for r in range(ROWS):
for c in range(COLS):
if grid[r][c] == INF:
grid[r][c] = bfs(r, c)Where is the number of rows and is the number of columns in the .
Instead of running BFS from every empty room, run BFS once starting from all treasures (0 cells) at the same time.
Why this works:
0, 1, 2, ...This avoids repeated work and is the optimal approach.
grid[r][c] == 0) into a queue.-1) are never added.dist = 0.dist).grid[r][c] = dist (distance to nearest treasure).dist += 1.INF.class Solution:
def islandsAndTreasure(self, grid: List[List[int]]) -> None:
ROWS, COLS = len(grid), len(grid[0])
visit = set()
q = deque()
def addCell(r, c):
if (min(r, c) < 0 or r == ROWS or c == COLS or
(r, c) in visit or grid[r][c] == -1
):
return
visit.add((r, c))
q.append([r, c])
for r in range(ROWS):
for c in range(COLS):
if grid[r][c] == 0:
q.append([r, c])
visit.add((r, c))
dist = 0
while q:
for i in range(len(q)):
r, c = q.popleft()
grid[r][c] = dist
addCell(r + 1, c)
addCell(r - 1, c)
addCell(r, c + 1)
addCell(r, c - 1)
dist += 1Where is the number of rows and is the number of columns in the .
A common mistake is to run BFS from each empty room to find the nearest treasure, resulting in O((m*n)^2) time complexity. The optimal approach is multi-source BFS starting from all treasures simultaneously, which processes each cell exactly once.
Walls are represented by -1 and should never be added to the queue or updated. Confusing the wall value with the infinity value (2147483647) for empty rooms can cause incorrect distance calculations or infinite loops.
In BFS, the distance should be updated when a cell is first discovered (added to the queue), not when it is processed (removed from the queue). Updating too late can result in cells being added to the queue multiple times with different distances, leading to incorrect results and inefficiency.