Java BFS solution with exploring all 6 combinations
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n    public int minimumMoves(int[][] grid) {\n        Queue<int[]> q = new LinkedList();\n        int n = grid.length;\n        boolean[][][] v = new boolean[n][n][2];\n        q.add(new int[]{0, 1, 0});\n        v[0][1][0]=true;\n        int res =0;\n        while(!q.isEmpty()){\n            for(int i=q.size();i>0;i--){\n                int[] cur = q.poll();\n                int r=cur[0], c=cur[1], d=cur[2];\n                if(r==n-1 && c==n-1 && d==0)return res;\n                if(d==0) {\n                    if(c+1<n && grid[r][c+1]==0){ //move right\n                        add(q, r, c+1, 0, v);\n                    }\n                    if(r+1<n && grid[r+1][c]==0 && grid[r+1][c-1]==0){\n                        add(q, r+1, c, 0, v); //move right\n                        add(q, r+1, c-1, 1, v); // rotate\n                    }\n                }else {\n                    if(r+1<n && grid[r+1][c]==0){\n                        add(q, r+1, c, 1, v); // down\n                    }\n                    if(c+1<n && grid[r][c+1]==0 && grid[r-1][c+1]==0){\n                        add(q, r, c+1, 1, v); //down\n                        add(q, r-1, c+1, 0, v); //rotate\n                    }\n                }\n            }\n            res++;\n        }\n        return -1;\n    }\n\n    void add(Queue<int[]> q, int r, int c, int d, boolean[][][] v){\n        if(v[r][c][d]) return;\n        q.add(new int[]{r, c, d});\n        v[r][c][d]=true;\n    }\n}\n```
['Java']