You are given an m x n binary matrix grid and an integer health.
You start on the upper-left corner (0, 0) and would like to get to the lower-right corner (m - 1, n - 1).
You can move up, down, left, or right from one cell to another adjacent cell as long as your health remains positive.
Cells (i, j) with grid[i][j] = 1 are considered unsafe and reduce your health by 1.
Return true if you can reach the final cell with a health value of 1 or more, and false otherwise.
Example 1:
Input: grid = [[0,1,0,0,0],[0,1,0,1,0],[0,0,0,1,0]], health = 1
Output: true
Explanation:
The final cell can be reached safely by walking along the gray cells below.

Example 2:
Input: grid = [[0,1,1,0,0,0],[1,0,1,0,0,0],[0,1,1,1,0,1],[0,0,1,0,1,0]], health = 3
Output: false
Explanation:
A minimum of 4 health points is needed to reach the final cell safely.

Example 3:
Input: grid = [[1,1,1],[1,0,1],[1,1,1]], health = 5
Output: true
Explanation:
The final cell can be reached safely by walking along the gray cells below.

Any path that does not go through the cell (1, 1) is unsafe since your health will drop to 0 when reaching the final cell.
Constraints:
m == grid.lengthn == grid[i].length1 <= m, n <= 502 <= m * n1 <= health <= m + ngrid[i][j] is either 0 or 1.This approach uses the Breadth-First Search algorithm to find the shortest path from the top-left corner of the grid to the bottom-right corner while maintaining a positive health level. We explore all possible paths from the start, tracking the health remaining at each step. We use a queue to facilitate the level-order traversal typical of BFS and check each possible move (up, down, left, right) from the current position.
The BFS method starts at (0,0) with the given health. We use a deque for level-wise traversal of the next reachable cells, maintaining a visited set to avoid revisiting cells. We explore possible directions (up, down, left, right) and check for boundary conditions and revisit prevention. The method returns true if we reach the target cell (m-1, n-1) with positive health.
C++
Java
Time Complexity: O(m*n) - since we in the worst case visit each cell once.
Space Complexity: O(m*n) - due to the visited set and queue storage.
This approach applies a depth-first search strategy with memorization to explore paths from the top-left to the bottom-right of the grid. By using a stack, it simulates DFS traversal, recursively checking viable movements. Memorization avoids redundant computations by storing previously calculated results for specific grid positions with remaining health. This optimization increases efficiency and reduces unnecessary recursion.
We leverage a recursive DFS approach where each node in the recursion tree represents a grid cell with current health. We recursively explore all possible valid directions, utilizing a memoization dictionary memo to cache results and avoid redundant calculations. The base case checks if the target is reached, at which point the function returns true.
C++
Java
Time Complexity: O(m*n*health) - Due to potentially exploring paths with overlapping health conditions.
Space Complexity: O(m*n*health) - Memoization storage for each combination of grid position and health.
| Approach | Complexity |
|---|---|
| Breadth-First Search (BFS) | Time Complexity: O(m*n) - since we in the worst case visit each cell once. Space Complexity: O(m*n) - due to the visited set and queue storage. |
| Depth-First Search (DFS) with Memorization | Time Complexity: O(m*n*health) - Due to potentially exploring paths with overlapping health conditions. Space Complexity: O(m*n*health) - Memoization storage for each combination of grid position and health. |
The unfair way I got good at Leetcode • Dave Burji • 596,394 views views
Watch 9 more video solutions →Practice Find a Safe Walk Through a Grid with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor