3286. Find a Safe Walk Through a Grid Medium
1/**
2 * [3286] Find a Safe Walk Through a Grid
3 *
4 * You are given an m x n binary matrix grid and an integer health.
5 * You start on the upper-left corner (0, 0) and would like to get to the lower-right corner (m - 1, n - 1).
6 * You can move up, down, left, or right from one cell to another adjacent cell as long as your health remains positive.
7 * Cells (i, j) with grid[i][j] = 1 are considered unsafe and reduce your health by 1.
8 * Return true if you can reach the final cell with a health value of 1 or more, and false otherwise.
9 *
10 * <strong class="example">Example 1:
11 * <div class="example-block">
12 * Input: <span class="example-io">grid = [[0,1,0,0,0],[0,1,0,1,0],[0,0,0,1,0]], health = 1</span>
13 * Output: <span class="example-io">true</span>
14 * Explanation:
15 * The final cell can be reached safely by walking along the gray cells below.
16 * <img alt="" src="https://assets.leetcode.com/uploads/2024/08/04/3868_examples_1drawio.png" style="width: 301px; height: 121px;" /></div>
17 * <strong class="example">Example 2:
18 * <div class="example-block">
19 * Input: <span class="example-io">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</span>
20 * Output: <span class="example-io">false</span>
21 * Explanation:
22 * A minimum of 4 health points is needed to reach the final cell safely.
23 * <img alt="" src="https://assets.leetcode.com/uploads/2024/08/04/3868_examples_2drawio.png" style="width: 361px; height: 161px;" /></div>
24 * <strong class="example">Example 3:
25 * <div class="example-block">
26 * Input: <span class="example-io">grid = [[1,1,1],[1,0,1],[1,1,1]], health = 5</span>
27 * Output: <span class="example-io">true</span>
28 * Explanation:
29 * The final cell can be reached safely by walking along the gray cells below.
30 * <img alt="" src="https://assets.leetcode.com/uploads/2024/08/04/3868_examples_3drawio.png" style="width: 181px; height: 121px;" />
31 * 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.
32 * </div>
33 *
34 * Constraints:
35 *
36 * m == grid.length
37 * n == grid[i].length
38 * 1 <= m, n <= 50
39 * <font face="monospace">2 <= m * n</font>
40 * 1 <= health <= m + n
41 * grid[i][j] is either 0 or 1.
42 *
43 */
44pub struct Solution {}
45
46// problem: https://leetcode.com/problems/find-a-safe-walk-through-a-grid/
47// discuss: https://leetcode.com/problems/find-a-safe-walk-through-a-grid/discuss/?currentPage=1&orderBy=most_votes&query=
48
49// submission codes start here
50
51impl Solution {
52 pub fn find_safe_walk(grid: Vec<Vec<i32>>, health: i32) -> bool {
53 false
54 }
55}
56
57// submission codes end
58
59#[cfg(test)]
60mod tests {
61 use super::*;
62
63 #[test]
64 fn test_3286() {
65 }
66}
67
Back
© 2025 bowen.ge All Rights Reserved.