Java BFS brute force solution
\nclass Solution {\n    public int minimumMoves(int[][] grid) {\n        int n = grid.length;\n        if(n == 0) return -1;\n        Queue<int[]> q = new Linke
```\nclass Solution {\n    public int minimumMoves(int[][] grid) {\n        int n = grid.length;\n        if(n == 0) return -1;\n        Queue<int[]> q = new LinkedList<>();\n        q.add(new int[]{0, 0, 0, 1});\n        Set<String> visited = new HashSet<>();\n        visited.add("0,0,0,1");\n        int count = 0;\n        while(!q.isEmpty()) {\n            int size = q.size();\n            for(int i = 0; i < size; i++) {\n\t\t\t\t//curr[0] is the row of first cell, curr[1] is the col of the first cell;\n\t\t\t\t//curr[2] is the row of the second cell, curr[3] is the col of the second cell.\n                int[] curr = q.poll();\n                if(curr[0] == n - 1 && curr[1] == n - 2 && curr[2] == n - 1 && curr[3] == n - 1) {\n                    return count;\n                }\n                //be horizontal and move right.\n                if(isHorizontal(curr) && curr[3] < n - 1 && grid[curr[0]][curr[3] + 1] == 0) {\n                    int r1 = curr[2], c1 = curr[3], r2 = r1, c2 = c1 + 1;\n                    String key = r1 + "," + c1 + "," + r2 + "," + c2;\n                    if(!visited.contains(key)) {\n                        visited.add(key);\n                        q.add(new int[]{r1, c1, r2, c2});\n                    }\n                }\n                //be vertical and move down.\n                if(isVertical(curr) && curr[2] < n - 1 && grid[curr[2] + 1][curr[3]] == 0) {\n                    int r1 = curr[2], c1 = curr[3], r2 = r1 + 1, c2 = c1;\n                    String key = r1 + "," + c1 + "," + r2 + "," + c2;\n                    if(!visited.contains(key)) {\n                        visited.add(key);\n                        q.add(new int[]{r1, c1, r2, c2});\n                    }\n                }\n                //be horizontal and rotate clockwise.\n                if(isHorizontal(curr) && curr[0] < n - 1 && grid[curr[0] + 1][curr[1]] == 0 && grid[curr[0] + 1][curr[3]] == 0) {\n                    int r1 = curr[0], c1 = curr[1], r2 = curr[2] + 1, c2 = c1;\n                    String key = r1 + "," + c1 + "," + r2 + "," + c2;\n                    if(!visited.contains(key)) {\n                        visited.add(key);\n                        q.add(new int[]{r1, c1, r2, c2});\n                    }\n                }\n                //be vertical and rotate counterclockwise.\n                if(isVertical(curr) && curr[1] < n - 1 && grid[curr[0]][curr[1] + 1] == 0 && grid[curr[2]][curr[1] + 1] == 0) {\n                    int r1 = curr[0], c1 = curr[1], r2 = r1, c2 = curr[3] + 1;\n                    String key = r1 + "," + c1 + "," + r2 + "," + c2;\n                    if(!visited.contains(key)) {\n                        visited.add(key);\n                        q.add(new int[]{r1, c1, r2, c2});\n                    }\n                }\n                //be horizontal and move down.\n                if(isHorizontal(curr) && curr[0] < n - 1 && grid[curr[0] + 1][curr[1]] == 0 && grid[curr[0] + 1][curr[3]] == 0) {\n                    int r1 = curr[0] + 1, c1 = curr[1], r2 = curr[2] + 1, c2 = curr[3];\n                    String key = r1 + "," + c1 + "," + r2 + "," + c2;\n                    if(!visited.contains(key)) {\n                        visited.add(key);\n                        q.add(new int[]{r1, c1, r2, c2});\n                    }\n                }\n                //be vertical and move right.\n                if(isVertical(curr) && curr[1] < n - 1 && grid[curr[0]][curr[1] + 1] == 0 && grid[curr[2]][curr[1] + 1] == 0) {\n                    int r1 = curr[0], c1 = curr[1] + 1, r2 = curr[2], c2 = curr[3] + 1;\n                    String key = r1 + "," + c1 + "," + r2 + "," + c2;\n                    if(!visited.contains(key)) {\n                        visited.add(key);\n                        q.add(new int[]{r1, c1, r2, c2});\n                    }\n                }\n            }\n            count++;\n        }\n        return -1;\n    }\n    \n    public boolean isHorizontal(int[] loc) {\n        return loc[0] == loc[2];\n    }\n    public boolean isVertical(int[] loc) {\n        return loc[1] == loc[3];\n    }\n}\n```
[]