grid of size n × n, where 0 is a free cell and 1 is a wall. Walk from the top-left corner (0, 0) to the bottom-right corner (n - 1, n - 1), stepping only on free cells.
In one step you may move to any of the eight neighbouring cells, through a side or a corner, like a king in chess.
The length of a path is the number of cells on it, counting the first and the last. Return the length of the shortest path, or -1 if there is none (including when either corner is a wall).grid=[[0, 0, 1], [1, 0, 1], [1, 1, 0]] → 3
The path (0, 0) → (1, 1) → (2, 2) moves diagonally and visits 3 cells.
grid=[[0, 1, 0, 0], [0, 1, 0, 1], [0, 1, 0, 1], [1, 0, 1, 0]] → 6
Go down the left column to (2, 0), then diagonally to (3, 1), (2, 2) and (3, 3): 6 cells in total.
grid=[[0, 1], [1, 1]] → -1
The bottom-right corner is blocked, so there is no path and the answer is -1.
grid=[[0, 0, 1], [1, 0, 1], [1, 1, 0]]
3
Run checks the first 3 tests in your browser. Submit checks all 65 tests on the server, hidden ones included.