A mobile gaming studio is A/B testing a new Snakes & Ladders variant for a casual game with millions of daily players. To tune difficulty, the analytics team needs to compute how many dice rolls a player needs at minimum to finish a given board—or determine that the board configuration can trap the player forever.
You are given an n x n integer matrix board representing a Snakes & Ladders board labeled from 1 to n^2 in boustrophedon order (left-to-right on the bottom row, then right-to-left on the next row up, alternating each row).
1.[1, 6].s, you may move to s + d only if s + d <= n^2 (overshooting is not allowed).board[r][c] != -1, you must immediately move to that destination square (a snake or ladder).Return the minimum number of dice rolls required to reach square n^2. If it is impossible to reach n^2, return -1.
Example 1
board = [[-1,-1,-1],[-1,9,8],[-1,-1,-1]]11, roll 1 to land on square 2, then take the ladder to 9 in the same move.Example 2
board = [[-1,-1],[-1,-1]]11, roll 3 to land exactly on square 4.1). Your solution must avoid infinite loops.n^2, the correct answer is -1.