C++ | BFS
\nclass Solution {\npublic:\n    int n;\n    bool valid(int i,int j)\n    {\n        if(i<0 || j<0 || i>=n || j>=n)\n            return false;\n        return t
```\nclass Solution {\npublic:\n    int n;\n    bool valid(int i,int j)\n    {\n        if(i<0 || j<0 || i>=n || j>=n)\n            return false;\n        return true;\n    }\n    int minimumMoves(vector<vector<int>>& grid) \n    {\n        int state=0;  //Horizontal-0    Vertical-1\n        n=grid.size();\n        queue<vector<int>>q;\n        vector<vector<bool>>vish(n,vector<bool>(n,false)),visv(n,vector<bool>(n,false));\n        \n        q.push({0,1,state});\n        vish[0][1]=true;\n        int res=0;\n        while(!q.empty())\n        {\n            int size=q.size();\n            while(size--)\n            {\n                int i=q.front()[0],j=q.front()[1],state=q.front()[2];\n                q.pop();\n                if(i==n-1 && j==n-1 && state==0)\n                    return res;\n                if(state==0)\n                {\n                    int x=i,y=j+1;\n                    if(valid(x,y) && grid[x][y]==0 && !vish[x][y])  //Right\n                    {\n                        q.push({x,y,0});\n                        vish[x][y]=true;\n                    }\n                    \n                    int x1=i+1,y1=j-1,x2=i+1,y2=j;\n                    if(valid(x1,y1) && valid(x2,y2) && grid[x1][y1]==0 && grid[x2][y2]==0 && !vish[x2][y2])  //Down\n                    {\n                        q.push({x2,y2,0});\n                        vish[x2][y2]=true;\n                    }\n                    \n                    x=i+1,y=j-1;\n                    if(valid(x,y) && valid(x,y+1) && grid[x][y]==0 && grid[x][y+1]==0 && !visv[x][y])  // Rotate clockwise\n                    {\n                        q.push({x,y,1});\n                        visv[x][y]=true;\n                    }\n                }\n                else\n                {\n                    int x1=i-1,y1=j+1,x2=i,y2=j+1;\n                    if(valid(x1,y1) && valid(x2,y2) && grid[x1][y1]==0 && grid[x2][y2]==0 && !visv[x2][y2])  //Right\n                    {\n                        q.push({x2,y2,1});\n                        visv[x2][y2]=true;\n                    }\n                    \n                    int x=i+1,y=j;\n                    if(valid(x,y) && grid[x][y]==0 && !visv[x][y])  //Down\n                    {\n                        visv[x][y]=true;\n                        q.push({x,y,1});\n                    }\n                    \n                    x=i-1,y=j+1;\n                    if(valid(x,y) && valid(x+1,y) && grid[x][y]==0 && grid[x+1][y]==0 && !vish[x][y])  // Rotate anticlockwise\n                    {\n                        q.push({x,y,0});\n                        vish[x][y]=true;\n                    }\n                }\n            }\n            res++;\n        }\n        return -1;\n    }\n};\n```
[]