grid:
- * is you (there is exactly one such cell);
- # is a cell with a snack (there may be any number of them, including none);
- O is an open walkway;
- X is a wall you cannot pass.
In one step you can move to a cell that shares a side with yours, as long as it is not a wall. Return the fewest steps needed to reach any snack cell, or -1 if none can be reached.grid=[["*", "O", "X", "#"], ["O", "O", "O", "O"], ["X", "#", "X", "O"]] → 3
To the snack at the bottom: down to (1, 0), right to (1, 1), down to (2, 1), 3 steps. The snack at the top right would take 5.
grid=[["O", "X", "#"], ["*", "X", "O"]] → -1
A column of walls cuts the snack off, so the answer is -1.
*, X, O, #* appears exactly oncegrid=[["*", "O", "X", "#"], ["O", "O", "O", "O"], ["X", "#", "X", "O"]]
3
Run checks the first 3 tests in your browser. Submit checks all 61 tests on the server, hidden ones included.