|
| 1 | +/** |
| 2 | + * [2258] Escape the Spreading Fire |
| 3 | + * |
| 4 | + * You are given a 0-indexed 2D integer array grid of size m x n which represents a field. Each cell has one of three values: |
| 5 | + * |
| 6 | + * 0 represents grass, |
| 7 | + * 1 represents fire, |
| 8 | + * 2 represents a wall that you and fire cannot pass through. |
| 9 | + * |
| 10 | + * You are situated in the top-left cell, (0, 0), and you want to travel to the safehouse at the bottom-right cell, (m - 1, n - 1). Every minute, you may move to an adjacent grass cell. After your move, every fire cell will spread to all adjacent cells that are not walls. |
| 11 | + * Return the maximum number of minutes that you can stay in your initial position before moving while still safely reaching the safehouse. If this is impossible, return -1. If you can always reach the safehouse regardless of the minutes stayed, return 10^9. |
| 12 | + * Note that even if the fire spreads to the safehouse immediately after you have reached it, it will be counted as safely reaching the safehouse. |
| 13 | + * A cell is adjacent to another cell if the former is directly north, east, south, or west of the latter (i.e., their sides are touching). |
| 14 | + * |
| 15 | + * Example 1: |
| 16 | + * <img alt="" src="https://assets.leetcode.com/uploads/2022/03/10/ex1new.jpg" style="width: 650px; height: 404px;" /> |
| 17 | + * Input: grid = [[0,2,0,0,0,0,0],[0,0,0,2,2,1,0],[0,2,0,0,1,2,0],[0,0,2,2,2,0,2],[0,0,0,0,0,0,0]] |
| 18 | + * Output: 3 |
| 19 | + * Explanation: The figure above shows the scenario where you stay in the initial position for 3 minutes. |
| 20 | + * You will still be able to safely reach the safehouse. |
| 21 | + * Staying for more than 3 minutes will not allow you to safely reach the safehouse. |
| 22 | + * Example 2: |
| 23 | + * <img alt="" src="https://assets.leetcode.com/uploads/2022/03/10/ex2new2.jpg" style="width: 515px; height: 150px;" /> |
| 24 | + * Input: grid = [[0,0,0,0],[0,1,2,0],[0,2,0,0]] |
| 25 | + * Output: -1 |
| 26 | + * Explanation: The figure above shows the scenario where you immediately move towards the safehouse. |
| 27 | + * Fire will spread to any cell you move towards and it is impossible to safely reach the safehouse. |
| 28 | + * Thus, -1 is returned. |
| 29 | + * |
| 30 | + * Example 3: |
| 31 | + * <img alt="" src="https://assets.leetcode.com/uploads/2022/03/10/ex3new.jpg" style="width: 174px; height: 150px;" /> |
| 32 | + * Input: grid = [[0,0,0],[2,2,0],[1,2,0]] |
| 33 | + * Output: 1000000000 |
| 34 | + * Explanation: The figure above shows the initial grid. |
| 35 | + * Notice that the fire is contained by walls and you will always be able to safely reach the safehouse. |
| 36 | + * Thus, 10^9 is returned. |
| 37 | + * |
| 38 | + * |
| 39 | + * Constraints: |
| 40 | + * |
| 41 | + * m == grid.length |
| 42 | + * n == grid[i].length |
| 43 | + * 2 <= m, n <= 300 |
| 44 | + * 4 <= m * n <= 2 * 10^4 |
| 45 | + * grid[i][j] is either 0, 1, or 2. |
| 46 | + * grid[0][0] == grid[m - 1][n - 1] == 0 |
| 47 | + * |
| 48 | + */ |
| 49 | +pub struct Solution {} |
| 50 | + |
| 51 | +// problem: https://leetcode.com/problems/escape-the-spreading-fire/ |
| 52 | +// discuss: https://leetcode.com/problems/escape-the-spreading-fire/discuss/?currentPage=1&orderBy=most_votes&query= |
| 53 | + |
| 54 | +// submission codes start here |
| 55 | + |
| 56 | +impl Solution { |
| 57 | + pub fn maximum_minutes(grid: Vec<Vec<i32>>) -> i32 { |
| 58 | + 0 |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +// submission codes end |
| 63 | + |
| 64 | +#[cfg(test)] |
| 65 | +mod tests { |
| 66 | + use super::*; |
| 67 | + |
| 68 | + #[test] |
| 69 | + #[ignore] |
| 70 | + fn test_2258_example_1() { |
| 71 | + let grid = vec![ |
| 72 | + vec![0, 2, 0, 0, 0, 0, 0], |
| 73 | + vec![0, 0, 0, 2, 2, 1, 0], |
| 74 | + vec![0, 2, 0, 0, 1, 2, 0], |
| 75 | + vec![0, 0, 2, 2, 2, 0, 2], |
| 76 | + vec![0, 0, 0, 0, 0, 0, 0], |
| 77 | + ]; |
| 78 | + |
| 79 | + let result = 3; |
| 80 | + |
| 81 | + assert_eq!(Solution::maximum_minutes(grid), result); |
| 82 | + } |
| 83 | + |
| 84 | + #[test] |
| 85 | + #[ignore] |
| 86 | + fn test_2258_example_2() { |
| 87 | + let grid = vec![vec![0, 0, 0, 0], vec![0, 1, 2, 0], vec![0, 2, 0, 0]]; |
| 88 | + |
| 89 | + let result = -1; |
| 90 | + |
| 91 | + assert_eq!(Solution::maximum_minutes(grid), result); |
| 92 | + } |
| 93 | + |
| 94 | + #[test] |
| 95 | + #[ignore] |
| 96 | + fn test_2258_example_3() { |
| 97 | + let grid = vec![vec![0, 0, 0], vec![2, 2, 0], vec![1, 2, 0]]; |
| 98 | + |
| 99 | + let result = 1000000000; |
| 100 | + |
| 101 | + assert_eq!(Solution::maximum_minutes(grid), result); |
| 102 | + } |
| 103 | +} |
0 commit comments