909 Snakes and Ladders

1. Question

On an N x Nboard, the numbers from1toN*Nare written boustrophedonically starting from the bottom left of the board

, and alternating direction each row. For example, for a 6 x 6 board, the numbers are written as follows:

You start on square1of the board (which is always in the last row and first column). Each move, starting from squarex, consists of the following:

  • You choose a destination squareSwith number x+1,x+2,x+3,x+4,x+5, orx+6, provided this number is <= N*N

    .(This choice simulates the result of a standard 6-sided die roll: ie., there are always at most 6 destinations.)

  • IfShas a snake or ladder, you move to the destination of that snake or ladder. Otherwise, you move toS.

A board square on rowrand columnc has a "snake or ladder" ifboard[r][c] != -1. The destination of that snake or ladder isboard[r][c].

Note that you only take a snake or ladder at most once per move: if the destination to a snake or ladder is the start of another snake or ladder, you do not continue moving. (For example, if the board is `[[4,-1],[-1,3]]`, and on the first move your destination square is `2`, then you finish your first move at `3`, because you do not continue moving to `4`.)

Return the least number of moves required to reach squareN*N. If it is not possible, return-1.

Example 1:

Input: [
[-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: 
At the beginning, you start at square 1 [at row 5, column 0].
You decide to move to square 2, and must take the ladder to square 15.
You then decide to move to square 17 (row 3, column 5), and must take the snake to square 13.
You then decide to move to square 14, and must take the ladder to square 35.
You then decide to move to square 36, ending the game.
It can be shown that you need at least 4 moves to reach the N*N-th square, so the answer is 4.

Note:

  1. 2 <= board.length = board[0].length <= 20 board[i][j]is between1andN*Nor is equal to-1.

  2. The board square with number1has no snake or ladder

  3. The board square with numberN*Nhas no snake or ladder.

2. Implementation

class Solution {
    public int snakesAndLadders(int[][] board) {
        int n = board.length;
        int[] num = new int[n * n + 1];
        int k = 1;

        for (int i = n - 1; i >= 0; i--) {
            if ((n - 1 - i) % 2 == 0) {
                for (int j = 0; j < n; j++) {
                    num[k++] = board[i][j];
                }
            }
            else {
                for (int j = n - 1; j >= 0; j--) {
                    num[k++] = board[i][j];
                }
            }
        }

        Queue<Integer> queue = new LinkedList();
        boolean[] visited = new boolean[n * n + 1];
        queue.add(1);
        visited[1] = true;

        int moves = 0;

        while (!queue.isEmpty()) {
            int size = queue.size();

            while (size-- > 0) {
                int curNum = queue.remove();

                if (curNum == n * n) {
                    return moves;
                }

                for (int i = 1; i <= 6 && (curNum + i) <= n * n; i++) {
                    int nextNum = curNum + i;

                    if (num[nextNum] != -1) {
                        nextNum = num[nextNum];
                    }

                    if (!visited[nextNum]) {
                        visited[nextNum] = true;
                        queue.add(nextNum);
                    }
                }
            }
            ++moves;
        }
        return -1;
    }
}

3. Time & Space Complexity

时间复杂度O(n^2), n是board的长度,每个cell只会visit一次,所以总的时间是n^2, 空间复杂度O(n^2)

Last updated

Was this helpful?