java ez 2 understand
\nclass Solution {\n    public int minimumMoves(int[][] grid) {\n        int n=grid.length;\n        Queue<State> queue=new LinkedList<>();\n        boolean vis
```\nclass Solution {\n    public int minimumMoves(int[][] grid) {\n        int n=grid.length;\n        Queue<State> queue=new LinkedList<>();\n        boolean vis[][][]=new boolean[n+1][n+1][2];\n        //1 hori\n        //0 vert\n        State strt=new State(0,1,1),end=new State(n-1,n-1,1);\n        queue.add(strt);\n        int level=0;\n        while(queue.size()!=0){\n            int len=queue.size();\n            while(len-->0){\n                State temp=queue.remove();\n                int i=temp.r,j=temp.c;\n               // System.out.print(i+" "+j+" "+temp.ori+" level "+level);\n                if(temp.r==end.r && temp.c==end.c && temp.ori==end.ori)\n                    return level;\n                if(temp.ori==1){\n                    //rotate\n                    if(i+1<n && j-1>=0 && grid[i+1][j]==0 && grid[i+1][j-1]==0 && !vis[i+1][j-1][0]){\n                        queue.add(new State(i+1,j-1,0));\n                        vis[i+1][j-1][0]=true;\n                    }\n                    //right\n                    if(j+1<n && !vis[i][j+1][temp.ori] && grid[i][j+1]==0){\n                        queue.add(new State(i,j+1,1));\n                        vis[i][j+1][1]=true;\n                    }\n                    //down\n                    if(j-1>=0 && i+1<n && grid[i+1][j]==0 && grid[i+1][j-1]==0 && !vis[i+1][j][1]){\n                        queue.add(new State(i+1,j,1));\n                        vis[i+1][j][1]=true;\n                    }\n                    \n                }else{\n                    //rotate\n                     if(i-1>=0 && j+1<n && grid[i-1][j+1]==0 && grid[i][j+1]==0 && !vis[i-1][j+1][1]){\n                        queue.add(new State(i-1,j+1,1));\n                         vis[i-1][j+1][1]=true;\n                     }\n                     //down\n                     if(i+1<n && !vis[i+1][j][temp.ori] && grid[i+1][j]==0){\n                         queue.add(new State(i+1,j,0));\n                         vis[i+1][j][0]=true;\n                     }\n                    //right\n                    if(j+1<n && i-1>=0 && grid[i-1][j+1]==0 && grid[i][j+1]==0 && !vis[i][j+1][temp.ori]){\n                        queue.add(new State(i,j+1,temp.ori));\n                        vis[i][j+1][temp.ori]=true;\n                    }\n                }\n            }\n            //System.out.println();\n            level++;\n        }\n        return -1;\n        \n    }\n    class State{\n        int r;\n        int c;\n        int ori;\n        State(int r,int c,int ori){\n            this.r=r;\n            this.c=c;\n            this.ori=ori;\n        }\n    }\n}```
['Breadth-First Search']