Trains the technique from
LeetCode 3286Find a Safe Walk Through a GridThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A survey drone must cross the floor of a burnt-out warehouse, laid out as a rectangular grid of bays. grid[i][j] is 1 when bay (i, j) is still scorched and 0 when it is clean. The drone begins on bay (0, 0) and has to finish on bay (grid.length - 1, grid[0].length - 1).
One move takes the drone to a bay directly above, below, left or right of the one it occupies; it may never leave the grid. Every scorched bay the drone occupies, including the bay it starts on and the bay it finishes on, burns one point off its shielding. Clean bays cost nothing. Scorched bays are passable, only expensive. The drone may revisit bays, though occupying a scorched bay again would cost again.
The drone starts with health points of shielding and the crossing counts as safe only if it reaches the final bay with at least 1 point still left. Return true when a safe crossing exists and false otherwise.
Example 1
Running right along row 0, down the last column to row 2, left along row 2 to column 0, down to row 4 and right along row 4 reaches the final bay without ever occupying a scorched bay, so the single shielding point is untouched.
Example 2
Bay `(0, 0)` and bay `(2, 2)` are both scorched and the drone has to occupy both, so every crossing burns at least two points and none can end with a point in hand.
Example 3
Row 1 is scorched apart from its last column, so going right along row 0 to column 3 and then straight down to bay `(2, 3)` keeps the drone on clean bays throughout.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def find_safe_walk(grid: list[list[int]], health: int) -> bool:public boolean findSafeWalk(int[][] grid, int health)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.