For people trying to understand why it failed at 39 testcase. Please read question properly.
Issue with 39th test case:\n\nFor counter and clock counter cases, it written that:\n1. Rotate clockwise if it\'s in a horizontal position and the two cells und
Issue with 39th test case:\n\nFor counter and clock counter cases, it written that:\n1. Rotate clockwise if it\'s in a horizontal position and t**he two cells under it are both empt**y. \n2. Rotate counterclockwise if it\'s in a vertical position and the **two cells to its right are both empty**.\n\nRead the bold letters. this will help\n# Code\n```\nclass Solution {\n    int[][] directions = new int[][]{{0, 1}, {1, 0}, {1, -1}, {-1, 1}};\n    int[] directionals = new int[]{2, 2, 0, 1};\n    public int minimumMoves(int[][] grid) {\n        Queue<Pair<Integer, Pair<Integer, Integer>>> queue = new LinkedList<>();\n        queue.offer(new Pair(0, new Pair(1, 0)));\n        int n = grid.length;\n        int m = grid[0].length;\n        boolean[][][] visited = new boolean[n][m][2];\n        visited[0][1][0] = true;\n        int level = 0;\n        while(!queue.isEmpty()) {\n            int size = queue.size();\n            while(size > 0) {\n                Pair<Integer, Pair<Integer, Integer>> node = queue.poll();\n                int x = node.getKey();\n                int y = node.getValue().getKey();\n                int dir = node.getValue().getValue();\n                if(x == (n - 1) && y == (n - 1) && dir == 0) {\n                    return level;\n                }\n                if(dir == 0) {\n\n                    //clockwise\n                     int i1 = x + 1;\n                     int j1 = y - 1;\n                     int dir1 = 1;\n                     if(isValid(i1, j1, n, n) && !visited[i1][j1][dir1] && grid[i1][j1] == 0 && grid[i1][j1 + 1] == 0) {\n                        queue.offer(new Pair(i1, new Pair(j1, 1)));\n                        visited[i1][j1][dir1] = true;\n                     }\n\n                     // down-H  r+1,c   r+1.c-1, V\n                    int i2 = x + 1;\n                    int j2 = y;\n                    int dir2 = 0;\n                     if(isValid(i2, j2, n, n) && !visited[i2][j2][dir2] && grid[i2][j2] == 0 && grid[i2][j2 - 1] == 0) {\n                        queue.offer(new Pair(i2, new Pair(j2, 0)));\n                        visited[i2][j2][dir2] = true;\n                     }\n\n                     //right : r,c+1,\n                     int i3 = x;\n                    int j3 = y + 1;\n                    int dir3 = 0;\n                     if(isValid(i3, j3, n, n) && !visited[i3][j3][dir3] && grid[i3][j3] == 0) {\n                        queue.offer(new Pair(i3, new Pair(j3, 0)));\n                        visited[i3][j3][dir3] = true;\n                     }\n\n                } else {\n                    \n                    //counterclockwise- r -1 , c+1\n                     int i1 = x - 1;\n                     int j1 = y + 1;\n                     int dir1 = 0;\n                     if(isValid(i1, j1, n, n) && !visited[i1][j1][dir1] && grid[i1][j1] == 0 && grid[i1 + 1][j1] == 0) {\n                        queue.offer(new Pair(i1, new Pair(j1, dir1)));\n                        visited[i1][j1][dir1] = true;\n                     }\n\n                     // // right-V  r,c+1  r-1,c+1, H\n                    int i2 = x;\n                    int j2 = y + 1;\n                    int dir2 = 1;\n                     if(isValid(i2, j2, n, n) && !visited[i2][j2][dir2] && grid[i2][j2] == 0 && grid[i2 - 1][j2] == 0 ) {\n                        queue.offer(new Pair(i2, new Pair(j2, dir2)));\n                        visited[i2][j2][dir2] = true;\n                     }\n\n                     // down-V  r+1,c   , V\n                     int i3 = x + 1; \n                    int j3 = y;\n                    int dir3 = 1;\n                     if(isValid(i3, j3, n, n) && !visited[i3][j3][dir3] && grid[i3][j3] == 0) {\n                        queue.offer(new Pair(i3, new Pair(j3, dir3)));\n                        visited[i3][j3][dir3] = true;\n                     }\n\n\n\n                }\n                size -= 1;\n            }\n            level += 1;\n        }\n        return -1;\n    }\n\n    private boolean isValid(int i, int j, int n, int m) {\n        return !(i < 0 || j < 0 || i >= n || j >= m);\n    }\n}\n\n\n//         r,c\n//          head   tail\n// right-H  r,c+1,        , H\n// right-V  r,c+1  r-1,c+1, H\n\n\n// down-V  r+1,c   , V\n\n// clock-H  r+1,c-1,       , V\n\n// rClocl-V  r -1 , c+1    , H\n\n\n//     right:\n\n// - horizontal: 0, 1, horizontal\n// - vertical: 0, 1, vertical\n\n// clockwise: \n\n// horizontal:\n// 1, -1, vertical\n\n// anti:\n// vertical:\n// -1, 1, horizontal\n\n\n\n// right : 0, 1\n// down:  1, 0\n// clockwise: 1, -1\n// anticlock: -1 , 1\n\n\n\n```
['Java']