package leetcode;

/**
 * https://leetcode.com/problems/spiral-matrix-iii/
 */
public class Problem885 {
    public int[][] spiralMatrixIII(int R, int C, int r0, int c0) {
        int size = R * C;
        int[][] result = new int[size][2];
        int count = 0;
        int index = 0;
        int row = r0;
        int col = c0;
        Direction direction = Direction.LEFT;
        result[index][0] = row;
        result[index][1] = col;
        index++;
        while (count < size) {
            if (withinBoundary(R, C, row, col)) {
                result[index][0] = row;
                result[index][1] = col;
                index++;
            }
        }
        return result;
    }

    private enum Direction {
        LEFT, DOWN, RIGHT, UP
    }

    private static boolean withinBoundary(int maxRow, int maxCol, int row, int col) {
        return !(row < 0 || row >= maxRow || col < 0 || col >= maxCol);
    }

    public static void main(String[] args) {
        Problem885 prob = new Problem885();
        System.out.println(prob.spiralMatrixIII(1, 4, 0, 0)); // [[0,0],[0,1],[0,2],[0,3]]
        System.out.println(prob.spiralMatrixIII(5, 6, 1, 4)); // [[1,4],[1,5],[2,5],[2,4],[2,3],[1,3],[0,3],[0,4],[0,5],[3,5],[3,4],[3,3],[3,2],[2,2],[1,2],[0,2],[4,5],[4,4],[4,3],[4,2],[4,1],[3,1],[2,1],[1,1],[0,1],[4,0],[3,0],[2,0],[1,0],[0,0]]
    }
}

@@ -254,17 +254,17 @@
 count = 
-0
+1
 ;%0A      
@@ -341,16 +341,18 @@
 irection
+%5B%5D
  directi
@@ -357,25 +357,135 @@
 tion
+s
  = 
-Direction.LEFT
+new Direction%5B%5D%7B%0A            Direction.RIGHT, Direction.DOWN, Direction.LEFT, Direction.UP%7D;%0A        int directionIndex = 0
 ;%0A  
@@ -555,32 +555,55 @@
        index++;%0A
+        int steps = 1;%0A
         while (c
@@ -613,24 +613,517 @@
 t %3C size) %7B%0A
+            for (int i = 0; i %3C steps; i++) %7B%0A                switch (directions%5BdirectionIndex%5D) %7B%0A                    case LEFT:%0A                        col--;%0A                        break;%0A                    case DOWN:%0A                        row++;%0A                        break;%0A                    case UP:%0A                        row--;%0A                        break;%0A                    case RIGHT:%0A                        col++;%0A                        break;%0A                %7D%0A    
             
@@ -1168,32 +1168,36 @@
                 
+   
+ 
 result%5Bindex%5D%5B0%5D
@@ -1196,32 +1196,36 @@
 ndex%5D%5B0%5D = row;%0A
+    
                 
@@ -1256,32 +1256,36 @@
                 
+    
 index++;%0A       
@@ -1281,33 +1281,340 @@
 ++;%0A            
-%7D
+        count++;%0A                %7D%0A            %7D%0A            if (directions%5BdirectionIndex%5D == Direction.DOWN %7C%7C%0A                directions%5BdirectionIndex%5D == Direction.UP) %7B%0A                steps++;%0A            %7D%0A            directionIndex++;%0A            directionIndex = directionIndex %25 directions.length;
 %0A        %7D%0A     
@@ -1675,19 +1675,20 @@
         
-LEF
+RIGH
 T, DOWN,
@@ -1688,20 +1688,19 @@
 , DOWN, 
-RIGH
+LEF
 T, UP%0A  
@@ -1871,441 +1871,7 @@
    %7D
-%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem885 prob = new Problem885();%0A        System.out.println(prob.spiralMatrixIII(1, 4, 0, 0)); // %5B%5B0,0%5D,%5B0,1%5D,%5B0,2%5D,%5B0,3%5D%5D%0A        System.out.println(prob.spiralMatrixIII(5, 6, 1, 4)); // %5B%5B1,4%5D,%5B1,5%5D,%5B2,5%5D,%5B2,4%5D,%5B2,3%5D,%5B1,3%5D,%5B0,3%5D,%5B0,4%5D,%5B0,5%5D,%5B3,5%5D,%5B3,4%5D,%5B3,3%5D,%5B3,2%5D,%5B2,2%5D,%5B1,2%5D,%5B0,2%5D,%5B4,5%5D,%5B4,4%5D,%5B4,3%5D,%5B4,2%5D,%5B4,1%5D,%5B3,1%5D,%5B2,1%5D,%5B1,1%5D,%5B0,1%5D,%5B4,0%5D,%5B3,0%5D,%5B2,0%5D,%5B1,0%5D,%5B0,0%5D%5D%0A    %7D
 %0A%7D%0A

Solve problem 885