Nearest Different Maze Exit for a Snake
Problem statement
A non-empty rectangular board uses 0 for passable cells and + for blocked cells. A snake starts at the passable boundary cell (startRow, startCol). Rows and columns are zero-based.
Move one cell at a time up, down, left, or right through passable cells, without revisiting a cell. An exit is any reachable boundary cell different from the starting cell.
Return the exit with the shortest route from the start as [row, column]. If several exits have the same minimum distance, choose the smallest row; if their rows also match, choose the smallest column. Return [-1,-1] when no different exit is reachable. The starting boundary cell never counts as an exit.
Function
nearestSnakeExit(board: String[], startRow: int, startCol: int) → int[]Examples
Example 1
board = ["+++++++00","++00000++","00000++0+","++0++++00","++000000+","++0++0+0+"]startRow = 2startCol = 0return = [5,2]The route (2,0) → (2,1) → (2,2) → (3,2) → (4,2) → (5,2) has five moves and reaches the nearest different boundary cell.
Example 2
board = ["+++++++00","++00000++","00000++0+","++0++++00","++000000+","++0++0+0+"]startRow = 5startCol = 2return = [2,0]Exits (2,0) and (5,5) are each five moves away. Exit (2,0) wins because its row is smaller. The starting cell (5,2) is excluded.
Constraints
- For this exercise, assume
1 <= board.length <= 100, and all rows have the same length from 1 to 100. - Every row contains only the characters
0and+. 0 <= startRow < board.lengthand0 <= startCol < board[0].length.- The start is a passable boundary cell: its row is first or last, or its column is first or last.