Java BFS beats 100% and easy to understand
\nclass Solution {\n    public int minimumMoves(int[][] grid) {\n        int n = grid[0].length;\n        Queue<head> q = new LinkedList<>();\n        Set<Strin
```\nclass Solution {\n    public int minimumMoves(int[][] grid) {\n        int n = grid[0].length;\n        Queue<head> q = new LinkedList<>();\n        Set<String> visited = new HashSet<>();\n        head start = new head(0,1,0);\n        q.add(start);\n        q.add(null);\n        int moves = 0;\n        \n        while(!q.isEmpty()){\n          head temp = q.poll();\n            if(temp == null){\n              if(!q.isEmpty()){\n                  q.add(null);\n              }\n              moves++;\n            } else {\n            \n            int x = temp.headx;\n            int y = temp.heady;\n            int status = temp.status;\n            String currKey = x + ":"+y+":"+status;\n            if(visited.contains(currKey)){\n                continue;\n            }\n            visited.add(currKey);\n            if(x == n-1 && y == n-1 && status == 0){\n                return moves;\n            }\n            //r\n            if(y+1 < n  && grid[x][y+1] == 0 && grid[x-status][y+1] == 0){\n                q.add(new head(x,y+1,status));\n            }\n            //d\n            if(isDownPossible(x,y,status,grid)){\n                q.add(new head(x+1,y,status));\n            }\n            // rotate\n            if(status == 0 && x+1 < n && grid[x+1][y-1] == 0 && grid[x+1][y] == 0 ){\n                  q.add(new head(x+1,y-1,1));\n            }\n            // counter \n            if(status == 1 && y+1 < n && grid[x][y+1] == 0 && grid[x-1][y+1] == 0){\n                  q.add(new head(x-1,y+1,0));\n            }\n        }\n            \n        }\n        \n        return -1;\n        \n    }\n    \n    private boolean isDownPossible(int x , int y, int status, int[][] grid){\n       \n        int n = grid[0].length;\n        if(x + 1 < n){\n            if(status ==0){\n                if(grid[x+1][y-1] == 0 && grid[x+1][y] == 0){\n                    return true;\n                }\n            }else{\n                if(grid[x+1][y] == 0 ) {\n                    return true;\n                }\n            }\n         }\n        return false;\n    }\n    \n    class head {\n        int headx ;\n        int heady;\n        int status;\n        public head(int x, int y, int status){\n            this.headx =x;\n            this.heady = y;\n            this.status = status;\n        }\n    }\n}\n```
[]