题目

Java/909. Snakes and Ladders 爬坡和梯子

Java/909. Snakes and Ladders 爬坡和梯子


Java/909. Snakes and Ladders 爬坡和梯子

Java/909. Snakes and Ladders 爬坡和梯子

 

 

 

代码部分二(18ms)

class Solution {
    public int snakesAndLadders(int[][] board) {
        int n = board.length;
        int[] b = new int [n*n+1];
        boolean flag = true;
        int x = 1;
        for(int i = n-1; i >= 0; i--)
        {
            if(flag)
            {
                for(int j = 0; j < n; j++)
                    b[x++] = board[i][j];
                
            }else{
                for(int j = n-1; j >= 0; j--)
                    b[x++] = board[i][j];
            }
            flag = !flag; 
        }
        
        int[] counter = new int[n*n+1];
        Arrays.fill(counter, -1);
        Queue<Integer> queue = new ArrayDeque<>();
        queue.add(1);
        counter[1] = 0;
        while(!queue.isEmpty()){
            int cur = queue.poll();
            for(int i = 1; i <= 6; i++)
            {
                if(cur + i > n*n)
                    break;
                int next = cur + i;
                if(b[next] > -1)
                    next = b[next];
                if(next == n*n)
                    return counter[cur] + 1;
                if(counter[next] == -1)
                {
                    queue.add(next);
                    counter[next] = counter[cur] + 1;
                }
            }
        }
        return -1;
    }
}

 

相关文章: