You are given an n x n integer matrix board where the cells are labeled from 1 to n^2 in a Boustrophedon style starting from the bottom left of the board (i.e. board[n - 1][0]) and alternating direction each row. You start on square 1 of the board. In each move, starting from square curr, do the following: - Choose a destination square next with a label in the range [curr + 1, min(curr + 6, n^2)]. - If next has a snake or ladder, you must move to the destination of that snake or ladder. Otherwise, you move to next. The game ends when you reach the square n^2. A board square on row r and column c has a snake or ladder if board[r][c] != -1. Return the least number of moves required to reach the square n^2. If it is not possible to reach the square, return -1.
Example 1
Input: board = [[-1,-1,-1,-1,-1,-1],[-1,-1,-1,-1,-1,-1],[-1,-1,-1,-1,-1,-1],[-1,35,-1,-1,13,-1],[-1,-1,-1,-1,-1,-1],[-1,15,-1,-1,-1,-1]]
Output: 4
Explanation: Square 1 -> 2 (ladder to 15) -> 17 -> 13 (ladder to 35) -> 36, four moves.
Example 2
Input: board = [[-1,-1],[-1,3]]
Output: 1
Explanation: The board has 4 squares. From square 1, a single roll of 3 lands directly on the final square 4.
Example 3
Input: board = [[1,1,-1],[1,1,1],[-1,1,1]]
Output: -1
Explanation: Every square you can reach from square 1 (squares 2 to 7) has a snake back to square 1. Square 9 is 8 away, farther than one roll, so it can never be reached.
Treat each square as a graph node with edges to the next 1-6 squares (following any snake or ladder at the landing square). The fewest moves to the last square is the shortest path from square 1, which BFS finds level by level.
Run checks all cases above. Submit evaluates all test cases.