java.lang.Object
g2201_2300.s2258_escape_the_spreading_fire.Solution

public class Solution extends Object
2258 - Escape the Spreading Fire.<p>Hard</p> <p>You are given a <strong>0-indexed</strong> 2D integer array <code>grid</code> of size <code>m x n</code> which represents a field. Each cell has one of three values:</p> <ul> <li><code>0</code> represents grass,</li> <li><code>1</code> represents fire,</li> <li><code>2</code> represents a wall that you and fire cannot pass through.</li> </ul> <p>You are situated in the top-left cell, <code>(0, 0)</code>, and you want to travel to the safehouse at the bottom-right cell, <code>(m - 1, n - 1)</code>. Every minute, you may move to an <strong>adjacent</strong> grass cell. <strong>After</strong> your move, every fire cell will spread to all <strong>adjacent</strong> cells that are not walls.</p> <p>Return <em>the <strong>maximum</strong> number of minutes that you can stay in your initial position before moving while still safely reaching the safehouse</em>. If this is impossible, return <code>-1</code>. If you can <strong>always</strong> reach the safehouse regardless of the minutes stayed, return <code>10<sup>9</sup></code>.</p> <p>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.</p> <p>A cell is <strong>adjacent</strong> to another cell if the former is directly north, east, south, or west of the latter (i.e., their sides are touching).</p> <p><strong>Example 1:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2022/03/10/ex1new.jpg" alt="" /></p> <p><strong>Input:</strong> 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]]</p> <p><strong>Output:</strong> 3</p> <p><strong>Explanation:</strong> The figure above shows the scenario where you stay in the initial position for 3 minutes.</p> <p>You will still be able to safely reach the safehouse.</p> <p>Staying for more than 3 minutes will not allow you to safely reach the safehouse.</p> <p><strong>Example 2:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2022/03/10/ex2new2.jpg" alt="" /></p> <p><strong>Input:</strong> grid = [[0,0,0,0],[0,1,2,0],[0,2,0,0]]</p> <p><strong>Output:</strong> -1</p> <p><strong>Explanation:</strong> The figure above shows the scenario where you immediately move towards the safehouse.</p> <p>Fire will spread to any cell you move towards and it is impossible to safely reach the safehouse.</p> <p>Thus, -1 is returned.</p> <p><strong>Example 3:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2022/03/10/ex3new.jpg" alt="" /></p> <p><strong>Input:</strong> grid = [[0,0,0],[2,2,0],[1,2,0]]</p> <p><strong>Output:</strong> 1000000000</p> <p><strong>Explanation:</strong> The figure above shows the initial grid. Notice that the fire is contained by walls and you will always be able to safely reach the safehouse. Thus, 10<sup>9</sup> is returned.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>2 <= m, n <= 300</code></li> <li><code>4 <= m * n <= 2 * 10<sup>4</sup></code></li> <li><code>grid[i][j]</code> is either <code>0</code>, <code>1</code>, or <code>2</code>.</li> <li><code>grid[0][0] == grid[m - 1][n - 1] == 0</code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • maximumMinutes

      public int maximumMinutes(int[][] grid)