[JAVA] BFS solution
int[4] to record snake position\nv1[][] record visited horizontal positions\nv2[][] record visited vertical positions\n\nclass Solution {\n    public int minimu
int[4] to record snake position\nv1[][] record visited horizontal positions\nv2[][] record visited vertical positions\n```\nclass Solution {\n    public int minimumMoves(int[][] grid) {\n        int ind = 0;\n        int n = grid.length;\n        int[][] v1 = new int[n][n];\n        int[][] v2 = new int[n][n];\n        if (grid[0][0] == 1 || grid[0][1] == 1) return -1;\n        int[] s = new int[]{0, 0, 0, 1};\n        Deque<int[]> queue = new ArrayDeque<>();\n        queue.offerLast(s);\n        while (queue.size() > 0) {\n            ind++;\n            System.out.println(ind);\n            int k = queue.size();\n            for (int i = 0; i < k; i++) {\n                int[] temp = queue.pollFirst();\n                if (temp[3] > temp[1]) {\n                    if (temp[3] == n - 1 && temp[2] == n - 1) return ind - 1;\n                    if ((temp[3] + 1 < n) && grid[temp[0]][temp[3] + 1] != 1) {\n                        if (v1[temp[0]][temp[3]] != 1)\n                        queue.offerLast(new int[]{temp[0], temp[3], temp[0], temp[3] + 1});\n                        v1[temp[0]][temp[3]] = 1;\n                    }\n                    if ((temp[0] + 1 < n) && grid[temp[0] + 1][temp[1]] != 1 && grid[temp[0] + 1][temp[3]] != 1) {\n                        if (v2[temp[0]][temp[1]] != 1)\n                        queue.offerLast(new int[]{temp[0], temp[1], temp[0] + 1, temp[1]});\n                        v2[temp[0]][temp[1]] = 1;\n                    }\n                    \n                    if ((temp[0] + 1 < n) && grid[temp[0] + 1][temp[1]] != 1  && grid[temp[0] + 1][temp[3]] != 1) {\n                        if (v1[temp[0] + 1][temp[1]] != 1)\n                        queue.offerLast(new int[]{temp[0] + 1, temp[1], temp[0] + 1, temp[3]});\n                        v1[temp[0] + 1][temp[1]] = 1;\n                    }\n                }\n                else if (temp[2] > temp[0]) {\n                    if ((temp[2] + 1 < n) && grid[temp[2] + 1][temp[1]] != 1) {\n                        if (v2[temp[2]][temp[3]] != 1)\n                        queue.offerLast(new int[]{temp[2], temp[3], temp[2] + 1, temp[3]});\n                        v2[temp[2]][temp[3]] = 1;\n                    }\n                    \n                    if ((temp[1] + 1 < n) && grid[temp[0]][temp[1] + 1] != 1 && grid[temp[2]][temp[1] + 1] != 1) {\n                        if (v1[temp[0]][temp[1]] != 1)\n                        queue.offerLast(new int[]{temp[0], temp[1], temp[0], temp[1] + 1});\n                        v1[temp[0]][temp[1]] = 1;\n                    }\n                    \n                    if ((temp[1] + 1 < n) && grid[temp[0]][temp[1] + 1] != 1 && grid[temp[2]][temp[3] + 1] != 1) {\n                        if (v2[temp[0]][temp[1] + 1] != 1)\n                        queue.offerLast(new int[]{temp[0], temp[1] + 1, temp[2], temp[3] + 1});\n                        v2[temp[0]][temp[1] + 1] = 1;\n                    }\n                }\n            }\n        }\n        return -1;\n    }\n}\n```
[]