Kotlin - BFS - Fun game, isn't it?!
The idea was "stolen" from this post by @rock. I just made limited modifications with some comments and constants.\n\nTo whom this might concern:\nI spent about
The idea was "stolen" from [this post](https://leetcode.com/problems/minimum-moves-to-reach-target-with-rotations/discuss/393511/JavaPython-3-25-and-17-liner-clean-BFS-codes-w-brief-explanation-and-analysis.) by @rock. I just made limited modifications with some comments and constants.\n\nTo whom this might concern:\nI spent about one hour figuring out why my solution was wrong, only to find that the rotation operation requires <b>2 empty cells</b> in the corresponding positions, instead of 1.\n\nSolution - [github](https://github.com/An7One/leetcode-solutions-kotlin-an7one/tree/main/src/main/kotlin/com/an7one/leetcode/lvl4/lc1210)\n\n<b>Problem List</b>\n#BFS - [github](https://github.com/An7One/leetcode-problems-by-tag-an7one/tree/main/txt/by_algorithm/search/breath_first_search)\n\n```\n/**\n * @author: Leon\n * https://leetcode.com/problems/minimum-moves-to-reach-target-with-rotations/\n *\n * Time Complexity:     O(`n` ^ 2)\n * Space Complexity:    O(`n` ^ 2)\n *\n * Reference:\n * https://leetcode.com/problems/minimum-moves-to-reach-target-with-rotations/discuss/393511/JavaPython-3-25-and-17-liner-clean-BFS-codes-w-brief-explanation-and-analysis.\n */\nclass Solution {\n    private companion object {\n        private const val EMPTY = 0\n\n        // not used\n        // private const val BLOCKED = 1\n    }\n\n    fun minimumMoves(grid: Array<IntArray>): Int {\n        val n = grid.size\n\n        val start = State(0, 0, Facing.RIGHT)\n        val target = State(n - 1, n - 2, Facing.RIGHT)\n\n        // to keep track of the tail of the snake,\n        // so it is required to check 2 steps in the front, to move towards the facing direction\n        val queue = ArrayDeque<State>().also{\n            it.addLast(start)\n        }\n\n        val seen = HashSet<String>()\n\n        var steps = 0\n\n        while (queue.isNotEmpty()) {\n            val size = queue.size\n\n            for (sz in 0 until size) {\n                val cur = queue.removeFirst()\n                if (cur == target) return steps\n\n                val row = cur.row\n                val col = cur.col\n                val dir = cur.dir\n\n                if (!seen.add(cur.toHash())) continue\n\n                if (dir == Facing.RIGHT) { // facing right\n                    if (row + 1 < n && grid[row + 1][col] == EMPTY && grid[row + 1][col + 1] == EMPTY) { // to check if both cells down are available (while facing right)\n                        queue.offer(State(row, col, Facing.DOWN)) // to rotate clockwise (while facing right)\n                        queue.offer(State(row + 1, col, Facing.RIGHT)) // to move down (while facing right)\n                    }\n\n                    if (col + 2 < n && grid[row][col + 2] == EMPTY) { // to check if the cell in front of the snake head is available (while facing right)\n                        queue.offer(State(row, col + 1, Facing.RIGHT)) // to move right (while facing right)\n                    }\n\n                }\n\n                if (dir == Facing.DOWN) { // facing down\n                    if (col + 1 < n && grid[row][col + 1] == EMPTY && grid[row + 1][col + 1] == EMPTY) { // to check if both cells right are available (while facing down)\n                        queue.offer(State(row, col, Facing.RIGHT)) // to rotate anti-clockwise (while facing down)\n                        queue.offer(State(row, col + 1, Facing.DOWN)) // to move right (while facing down)\n                    }\n\n                    if (row + 2 < n && grid[row + 2][col] == EMPTY) { // to check if the cell in front of the snake head is available (while facing down)\n                        queue.offer(State(row + 1, col, Facing.DOWN)) // to move down (while facing down)\n                    }\n                }\n            }\n\n            ++steps\n        }\n\n        return -1\n    }\n\n    private data class State(val row: Int, val col: Int, val dir: Facing) {\n\n        fun toHash(): String = "$row#$col#$dir"\n\n        override fun equals(other: Any?) = (other is State) && row == other.row && col == other.col && dir == other.dir\n\n        override fun hashCode(): Int {\n            return super.hashCode()\n        }\n    }\n\n    private enum class Facing {\n        DOWN, RIGHT\n    }\n}\n```\n\nWhat a @rock!\nWhat a fun game!\nWhat a practice for BFS!\n\nI love algorithm!\nI love Kotlin!
['Breadth-First Search', 'Kotlin']