Java BFS
\n    public int minimumMoves(int[][] g) {\n        int n = g.length, step = 0;\n        Set<Integer> vs = new HashSet<>();\n        Queue<int[]> q = new Linked
```\n    public int minimumMoves(int[][] g) {\n        int n = g.length, step = 0;\n        Set<Integer> vs = new HashSet<>();\n        Queue<int[]> q = new LinkedList<>();\n        if (g[0][0] == 1 || g[0][1] == 1) return -1;\n        q.offer(new int[]{0, 1, 0});\n        vs.add(2);\n        int[][] dir = new int[][]{{0, 1}, {1, 0}};\n        while (!q.isEmpty()) {\n            for (int i = 0, len = q.size(); i < len; i++) {\n                int[] curr = q.poll();\n                if (curr[0] == n - 1 && curr[1] == n - 1 && curr[2] == 0) return step;\n                int r = curr[0] + dir[curr[2]][0], c = curr[1] + dir[curr[2]][1], hash = (r * n + c) * 2 + curr[2];\n                if (r < n && c < n && g[r][c] == 0 && !vs.contains(hash)) {\n                    vs.add(hash);\n                    q.offer(new int[]{r, c, curr[2]});\n                }\n                if (curr[2] == 0 && curr[1] > 0) {\n                    r = curr[0] + 1; c = curr[1] - 1; hash = (r * n + c) * 2 + 1;\n                    if (r < n && g[r][curr[1]] == 0 && g[r][c] == 0) {\n                        if (!vs.contains(hash)) {\n                            vs.add(hash);\n                            q.offer(new int[]{r, c, 1});\n                        }\n                        hash = (r * n + ++c) * 2 + 0;\n                        if (!vs.contains(hash)) {\n                            vs.add(hash);\n                            q.offer(new int[]{r, c, 0});\n                        }\n                    }\n                }\n                if (curr[2] == 1 && curr[0] > 0) {\n                    r = curr[0] - 1; c = curr[1] + 1; hash = (r * n + c) * 2 + 0;\n                    if (c < n && g[curr[0]][c] == 0 && g[r][c] == 0 ) {\n                        if (!vs.contains(hash)){\n                            vs.add(hash);\n                            q.offer(new int[]{r, c, 0});\n                        }\n                        hash = (++r * n + c) * 2 + 1;\n                        if (!vs.contains(hash)) {\n                            vs.add(hash);\n                            q.offer(new int[]{r, c, 1});\n                        }\n                    }\n                }\n            }\n            step++;\n        }\n        return -1;\n    }\n```
[]