Hexagonal Tour
Time limit2sMemory limit512 MB
Given a hexagonal building of side N and a start room, find a route that visits every room exactly once and returns to the start, or report -1.
- Level
Medium7 of 10
- Topics
- Implementation, Graph
- Solved
- No attempts yet
Problem
Jeonghu visited a hexagonal building designed by a famous architect who took inspiration from the pentagon, and he was captivated by its unusual beauty. The building is made of regular hexagonal rooms. The rooms are arranged in a regular hexagon with rooms along each side. Put differently, the rooms form rows, and row holds rooms. Two rooms that share an edge are connected by a door, so people can walk between them. A room has up to six neighbors, and each move is written as one of the uppercase letters Q, E, D, C, Z, or A, as shown in the figure below. In the figure, is the -th room in the -th row, and Jeonghu is currently in the fourth room of the third row.

Jeonghu wants to start in the -th room of the -th row, visit every room exactly once, and return to his starting room. Find any such route for him.
Input
Three integers , , and are given on a single line, separated by spaces.
Output
If such a route exists, print any one of them as a string of length . The -th character is one of the uppercase letters Q, E, D, C, Z, or A, and it gives the direction of the -th move. If no such route exists, print -1 only.