Kotlin BFS
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n    fun minimumMoves(grid: Array<IntArray>): Int {\n        val visitGrid = Array(grid.size) {\n            Array(grid[0].size) {\n                arrayOf(false, false)\n            }\n        }\n\n        val queue = LinkedList<Array<Int>>()\n\n        queue.add(arrayOf(0, 1, 0, 0))\n\n        while (queue.isNotEmpty()) {\n            val pos = queue.poll()\n            if (pos[0] == grid.size - 1 && pos[1] == grid[pos[0]].size - 1 && pos[2] == 0) {\n                return pos[3]\n            }\n            if (pos[2] == 0) {\n                if (canGoRightHorizontal(grid, visitGrid, pos[0], pos[1])) {\n                    visitGrid[pos[0]][pos[1] + 1][0] = true\n                    queue.add(arrayOf(pos[0], pos[1] + 1, 0, pos[3] + 1))\n                }\n                if (canGoDownHorizontal(grid, visitGrid, pos[0], pos[1])) {\n                    visitGrid[pos[0] + 1][pos[1]][0] = true\n                    queue.add(arrayOf(pos[0] + 1, pos[1], 0, pos[3] + 1))\n                }\n                if (canTurnClockwise(grid, visitGrid, pos[0], pos[1])) {\n                    visitGrid[pos[0] + 1][pos[1] - 1][1] = true\n                    queue.add(arrayOf(pos[0] + 1, pos[1] - 1, 1, pos[3] + 1))\n                }\n            } else {\n                if (canGoRightVertical(grid, visitGrid, pos[0], pos[1])) {\n                    visitGrid[pos[0]][pos[1] + 1][1] = true\n                    queue.add(arrayOf(pos[0], pos[1] + 1, 1, pos[3] + 1))\n                }\n                if (canGoDownVertical(grid, visitGrid, pos[0], pos[1])) {\n                    visitGrid[pos[0] + 1][pos[1]][1] = true\n                    queue.add(arrayOf(pos[0] + 1, pos[1], 1, pos[3] + 1))\n                }\n                if (canTurnAnticlockwise(grid, visitGrid, pos[0], pos[1])) {\n                    visitGrid[pos[0] - 1][pos[1] + 1][0] = true\n                    queue.add(arrayOf(pos[0] - 1, pos[1] + 1, 0, pos[3] + 1))\n                }\n            }\n        }\n        return -1\n    }\n\n    fun canGoRightVertical(grid: Array<IntArray>, visitGrid: Array<Array<Array<Boolean>>>, y: Int, x: Int): Boolean {\n        return x + 1 < grid[y].size && !visitGrid[y][x + 1][1] && grid[y][x + 1] == 0 && grid[y - 1][x + 1] == 0\n    }\n\n    fun canGoRightHorizontal(grid: Array<IntArray>, visitGrid: Array<Array<Array<Boolean>>>, y: Int, x: Int): Boolean {\n        return x + 1 < grid[y].size && !visitGrid[y][x + 1][0] && grid[y][x + 1] == 0\n    }\n\n    fun canGoDownVertical(grid: Array<IntArray>, visitGrid: Array<Array<Array<Boolean>>>, y: Int, x: Int): Boolean {\n        return y + 1 < grid.size && !visitGrid[y + 1][x][1] && grid[y + 1][x] == 0\n    }\n\n    fun canGoDownHorizontal(grid: Array<IntArray>, visitGrid: Array<Array<Array<Boolean>>>, y: Int, x: Int): Boolean {\n        return y + 1 < grid.size && !visitGrid[y + 1][x][0] && grid[y + 1][x] == 0 && grid[y + 1][x - 1] == 0\n    }\n\n    fun canTurnClockwise(grid: Array<IntArray>, visitGrid: Array<Array<Array<Boolean>>>, y: Int, x: Int): Boolean {\n        return y + 1 < grid.size && !visitGrid[y + 1][x - 1][1] && grid[y + 1][x] == 0 && grid[y + 1][x - 1] == 0\n    }\n\n    fun canTurnAnticlockwise(grid: Array<IntArray>, visitGrid: Array<Array<Array<Boolean>>>, y: Int, x: Int): Boolean {\n        return x + 1 < grid[y].size && !visitGrid[y - 1][x + 1][0] && grid[y][x + 1] == 0 && grid[y - 1][x + 1] == 0\n    }\n}\n```
['Kotlin']