My Solution
\n/**\n * Time Complexity: O(n * n)\n * Space Complexity: O(n * n)\n * where `n` is the length of the vector `grid`\n */\nclass Solution {\n private:\n  /**\n  
```\n/**\n * Time Complexity: O(n * n)\n * Space Complexity: O(n * n)\n * where `n` is the length of the vector `grid`\n */\nclass Solution {\n private:\n  /**\n   * <0>: the row\n   * <1>: the column\n   */\n  using position_t = array<int, 2>;\n  /**\n   * <first>: a boolean value to indicate whether the snake is horizontal (true) or vertical (false)\n   * <second>: the position of the tail of the snake\n   */\n  using snake_t = pair<bool, position_t>;\n  \n  enum class action_e : uint8_t {\n    move_right = 0,\n    move_down,\n    rotate\n  };\n  \n  static constexpr uint8_t n_actions = 3;\n\n  static constexpr int empty = 0;\n\n public:\n  int minimumMoves(const vector<vector<int>> &grid) {\n    constexpr int unreachable = -1;\n    constexpr int bool_range = 2;\n\n    const int n = static_cast<int>(grid.size());\n    const snake_t target{true, position_t{n - 1, n - 2}};\n    if (!can_reach(grid, target)) {\n      return unreachable;\n    }\n    \n    queue<snake_t> q({make_pair(true, position_t{0, 0})});\n    vector<vector<vector<bool>>> visited(bool_range, vector<vector<bool>>(n, vector<bool>(n)));\n    visited[static_cast<int>(true)][0][0] = true;\n    \n    for (int moves = 0; !q.empty(); ++moves) {\n      for (size_t qs = q.size(); qs > 0; --qs) {\n        const snake_t snake = q.front();\n        if (snake == target) {\n          return moves;\n        }\n        q.pop();\n        for (uint8_t action = 0; action < n_actions; ++action) {\n          const auto [acted, next_snake] = try_act(grid, snake, static_cast<action_e>(action));\n          if (acted) {\n            try_visit(q, visited, next_snake);\n          }\n        }\n      }\n    }\n    return unreachable;\n  }\n  \n private:\n  void try_visit(queue<snake_t> &q, vector<vector<vector<bool>>> &visited, const snake_t &snake) {\n    if (!visited[static_cast<int>(snake.first)][snake.second.front()][snake.second.back()]) {\n      q.emplace(snake);\n      visited[static_cast<int>(snake.first)][snake.second.front()][snake.second.back()] = true;\n    }\n  }\n  \n  bool is_valid(const vector<vector<int>> &grid, const position_t &position) {\n    const int n = static_cast<int>(grid.size());\n    const auto [r, c] = position;\n    return r > -1 && r < n && c > -1 && c < n;\n  }\n  \n  bool is_empty(const vector<vector<int>> &grid, const position_t &position) {\n    return grid[position.front()][position.back()] == empty;\n  }\n  \n  position_t next_position(const position_t &position, const bool to_right) {\n    return {\n      position.front() + (to_right ? 0 : 1),\n      position.back() + (to_right ? 1 : 0)\n    };\n  }\n\n  bool can_reach(const vector<vector<int>> &grid, const position_t &position) {\n    return is_valid(grid, position) && is_empty(grid, position);\n  }\n  \n  bool can_reach(const vector<vector<int>> &grid, const snake_t &snake) {\n    const position_t tail = snake.second;\n    const position_t head(next_position(snake.second, snake.first));\n    return can_reach(grid, tail) && can_reach(grid, head);\n  }\n  \n  pair<bool, snake_t> try_move(const vector<vector<int>> &grid, const snake_t &snake, const bool to_right) {\n    /**\n     * @param grid: the grid\n     * @param snake: the snake\n     * @param to_right: if it\'s true, the snake will try to move right\n     *                  otherwise, the snake will try to move down\n     * @return: a pair\n     *          <first>: a boolean value to indicate\n     *                   whether the snake can move to the target position successfully\n     *          <second>: the snake after moving\n     */\n    const snake_t next_snake{snake.first, next_position(snake.second, to_right)};\n    return make_pair(can_reach(grid, next_snake), next_snake);\n  }\n  \n  pair<bool, snake_t> try_rotate(const vector<vector<int>> &grid, const snake_t &snake) {\n    /**\n     * @param grid: the grid\n     * @param snake: the snake\n     * @return: a pair\n     *          <first>: a boolean value to indicate\n     *                   whether the snake can rotate to the target position successfully\n     *          <second>: the snake after rotating\n     */\n    const snake_t next_snake{!snake.first, snake.second};\n    const position_t right_bottom{snake.second.front() + 1, snake.second.back() + 1};\n    return make_pair(can_reach(grid, next_snake) && can_reach(grid, right_bottom), next_snake);\n  }\n  \n  pair<bool, snake_t> try_act(const vector<vector<int>> &grid, const snake_t &snake, const action_e action) {\n    if (action == action_e::rotate) {\n      return try_rotate(grid, snake);\n    }\n    return try_move(grid, snake, action == action_e::move_right);\n  }\n};\n```
[]