Rust - - 12 ms Beats 100.00%
\n# Code\nrust []\nuse std::collections::{HashMap, VecDeque};\n\nimpl Solution {\n    pub fn minimum_moves(grid: Vec<Vec<i32>>) -> i32 {\n        let n = grid.l
\n# Code\n```rust []\nuse std::collections::{HashMap, VecDeque};\n\nimpl Solution {\n    pub fn minimum_moves(grid: Vec<Vec<i32>>) -> i32 {\n        let n = grid.len() as i32;\n        let mut moves = 0;\n        let mut visited = HashMap::new();\n        let mut queue = VecDeque::new();\n\n        // Initialize with starting position (horizontal, tail at (0,0), head at (0,1))\n        queue.push_back((0, (0, 1)));\n        visited.insert((0, (0, 1)), true);\n\n        while !queue.is_empty() {\n            let level_size = queue.len();\n\n            for _ in 0..level_size {\n                let (position, (x, y)) = queue.pop_front().unwrap();\n\n                // Check if we\'ve reached the target\n                if x == n - 1 && y == n - 1 && position == 0 {\n                    return moves;\n                }\n\n                if position == 0 { // Horizontal position\n                    // Move right\n                    if y + 1 < n && grid[x as usize][y as usize + 1] == 0 && \n                       !visited.contains_key(&(0, (x, y + 1))) {\n                        queue.push_back((0, (x, y + 1)));\n                        visited.insert((0, (x, y + 1)), true);\n                    }\n\n                    // Move down\n                    if x + 1 < n && grid[x as usize + 1][y as usize] == 0 && \n                       grid[x as usize + 1][y as usize - 1] == 0 && \n                       !visited.contains_key(&(0, (x + 1, y))) {\n                        queue.push_back((0, (x + 1, y)));\n                        visited.insert((0, (x + 1, y)), true);\n                    }\n\n                    // Rotate clockwise\n                    if x + 1 < n && grid[x as usize + 1][y as usize] == 0 && \n                       grid[x as usize + 1][y as usize - 1] == 0 && \n                       !visited.contains_key(&(1, (x + 1, y - 1))) {\n                        queue.push_back((1, (x + 1, y - 1)));\n                        visited.insert((1, (x + 1, y - 1)), true);\n                    }\n                } else { // Vertical position\n                    // Move down\n                    if x + 1 < n && grid[x as usize + 1][y as usize] == 0 && \n                       !visited.contains_key(&(1, (x + 1, y))) {\n                        queue.push_back((1, (x + 1, y)));\n                        visited.insert((1, (x + 1, y)), true);\n                    }\n\n                    // Move right\n                    if y + 1 < n && grid[x as usize][y as usize + 1] == 0 && \n                       grid[x as usize - 1][y as usize + 1] == 0 && \n                       !visited.contains_key(&(1, (x, y + 1))) {\n                        queue.push_back((1, (x, y + 1)));\n                        visited.insert((1, (x, y + 1)), true);\n                    }\n\n                    // Rotate counter-clockwise\n                    if y + 1 < n && grid[x as usize][y as usize + 1] == 0 && \n                       grid[x as usize - 1][y as usize + 1] == 0 && \n                       !visited.contains_key(&(0, (x - 1, y + 1))) {\n                        queue.push_back((0, (x - 1, y + 1)));\n                        visited.insert((0, (x - 1, y + 1)), true);\n                    }\n                }\n            }\n\n            moves += 1;\n        }\n\n        -1 // No solution found\n    }\n}\n```
['Rust']