JAVA Simulation 17 lines with line-by-line Explanation
\nclass Solution {\n    public int[][] spiralMatrixIII(int R, int C, int r0, int c0) {\n        int[][] res= new int[R*C][2];\n        res[0]=new int[]{r0, c0};
```\nclass Solution {\n    public int[][] spiralMatrixIII(int R, int C, int r0, int c0) {\n        int[][] res= new int[R*C][2];\n        res[0]=new int[]{r0, c0};\n        int len=0, idx=1, k=0;\n        int[] d= new int[]{0,1,0,-1,0};\n        while (idx<R*C){\n            len++;\n            for (int round=0; round<2; round++){\n                for (int sz=len; sz>0; sz--){\n                    r0+=d[k];\n                    c0+=d[k+1];\n                    if (r0<0 || r0>=R || c0<0 || c0>=C) continue;\n                    res[idx++]=new int[]{r0, c0};\n                }\n                k=(k+1)%4;\n            }\n        }\n        return res;\n    }\n}\n```\n![image](https://s3-lc-upload.s3.amazonaws.com/users/caraxin/image_1534044938.png)\n\nHappy Coding!\n
[]