package leetcode;

/**
 * https://leetcode.com/problems/snakes-and-ladders/
 */
public class Problem909 {
    public int snakesAndLadders(int[][] board) {
        // TODO
        return 0;
    }

    public static void main(String[] args) {
        Problem909 prob = new Problem909();
        System.out.println(prob.snakesAndLadders(new int[][]{
            {-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}
        })); // 4
    }
}

@@ -12,16 +12,70 @@
 tcode;%0A%0A
+import java.util.LinkedList;%0Aimport java.util.Queue;%0A%0A
 /**%0A * h
@@ -214,32 +214,1480 @@
     
-// TODO%0A        return 0
+int%5B%5D b = new int%5B(board.length * board.length) + 1%5D;%0A        int i = 1;%0A        boolean leftToRight = true;%0A        for (int row = board.length - 1; row %3E= 0; row--) %7B%0A            if (leftToRight) %7B%0A                for (int col = 0; col %3C board.length; col++) %7B%0A                    b%5Bi%5D = board%5Brow%5D%5Bcol%5D;%0A                    i++;%0A                %7D%0A            %7D else %7B%0A                for (int col = board.length - 1; col %3E= 0; col--) %7B%0A                    b%5Bi%5D = board%5Brow%5D%5Bcol%5D;%0A                    i++;%0A                %7D%0A            %7D%0A        %7D%0A        int answer = 0;%0A        int target = board.length * board.length;%0A        Queue%3CInteger%3E queue = new LinkedList%3C%3E();%0A        queue.add(1);%0A        outer:%0A        while (!queue.isEmpty()) %7B%0A            Queue%3CInteger%3E tmp = new LinkedList%3C%3E();%0A            answer++;%0A            while (!queue.isEmpty()) %7B%0A                int current = queue.remove();%0A                if (current == target) %7B%0A                    break outer;%0A                %7D%0A                for (int step = 1; step %3C= 6; step++) %7B%0A                    int newStep = current + step;%0A                    if (newStep %3C b.length) %7B%0A                        if (b%5BnewStep%5D %3E= 0) %7B%0A                            tmp.add(b%5BnewStep%5D);%0A                        %7D else %7B%0A                            tmp.add(newStep);%0A                        %7D%0A                    %7D%0A                %7D%0A            %7D%0A            queue.addAll(tmp);%0A        %7D%0A        return answer - 1
 ;%0A  
@@ -1776,24 +1776,26 @@
 oblem909();%0A
+//
         Syst
@@ -1844,16 +1844,18 @@
 nt%5B%5D%5B%5D%7B%0A
+//
         
@@ -1876,32 +1876,34 @@
 1, -1, -1, -1%7D,%0A
+//
             %7B-1,
@@ -1916,32 +1916,34 @@
 1, -1, -1, -1%7D,%0A
+//
             %7B-1,
@@ -1956,32 +1956,34 @@
 1, -1, -1, -1%7D,%0A
+//
             %7B-1,
@@ -2000,24 +2000,26 @@
 1, 13, -1%7D,%0A
+//
             
@@ -2044,16 +2044,18 @@
 1, -1%7D,%0A
+//
         
@@ -2083,16 +2083,18 @@
 -1, -1%7D%0A
+//
         
@@ -2103,16 +2103,167 @@
 ); // 4%0A
+        System.out.println(prob.snakesAndLadders(new int%5B%5D%5B%5D%7B%0A            %7B1, 1, -1%7D,%0A            %7B1, 1, 1%7D,%0A            %7B-1, 1, 1%7D%0A        %7D)); // -1%0A
     %7D%0A%7D%0A

Update problem 909