My first instinct was just BFS from start to end, which is wrong because the fire is moving too.
Model the problem as a time-expanded graph or use binary search on the waiting time, checking feasibility with BFS. Precompute fire arrival times for each cell using multi-source BFS from all initial fires, then for a given wait time, run BFS from start to safehouse ensuring you arrive before the fire. The answer is the maximum wait time that allows a safe path, or 10^9 if you can wait indefinitely, -1 if impossible even with zero wait.
Pro tip: Clarify that waiting at the start is equivalent to delaying your departure, and that you can also wait at intermediate cells if needed—but the problem only asks for waiting at the start. Mention that binary search works because feasibility is monotonic: if you can wait T minutes, you can wait any T' < T.
Restate the problem: grid with grass, fire, walls; fire spreads each minute; you start at top-left, want to reach bottom-right; you can wait at start. Determine if waiting indefinitely is possible (10^9), impossible (-1), or a finite maximum.
Run multi-source BFS from all initial fire cells to compute the earliest time each cell catches fire. Use a 2D array fireTime, with INF for unreachable cells.
For a candidate wait time W, run BFS from start to safehouse, only moving to cells where your arrival time (W + steps) is strictly less than fireTime[cell]. Also ensure start and safehouse are not on fire at time W and W+steps respectively.
Binary search W in [0, upper bound]. If feasible, try larger; else try smaller. Upper bound can be max fireTime or a large number if fire never reaches safehouse. Handle edge cases: if start or safehouse is fire initially, return -1; if safehouse never catches fire and reachable, return 10^9.
After binary search, if no W >= 0 is feasible, return -1. If feasible for all W up to a large bound (e.g., safehouse never burns), return 10^9. Otherwise return the maximum feasible W.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.