RPG Maker
Time limit2sMemory limit512 MB
Given a sparse grid of cities on odd coordinates, build a fixed Hamiltonian cycle ordering and cut it at the last city, marking the resulting path as roads.
- Level
Medium4 of 10
- Topics
- Implementation, Simulation, Matrix
- Solved
- No attempts yet
Problem
You are drawing the map of an RPG. The map is a grid with rows and columns, and every cell holds one of four symbols.
@: the start cell. The story begins here.*: a city cell. The story passes through this cell or ends at it.#: a road cell..: an empty cell.
The start cell and every city cell are already placed under the restrictions given in the input section, and no road cell is placed yet. You decide which empty cells become road cells.
The map must hold one journey. The story must not branch, so the journey must not fork either. A journey is a sequence of cells that satisfies all of the following conditions.
- The journey holds as many city cells as possible.
- The journey consists of distinct cells that are not empty.
- The journey begins at the start cell.
- The journey ends at a city cell.
- Every road cell belongs to the journey. No road cell lies outside the journey.
- Every cell of the journey other than the first and the last shares an edge with exactly two cells of the journey. The first cell and the last cell each share an edge with exactly one cell of the journey.
- The order in which the cities are visited does not matter.
You may turn any number of empty cells into road cells. Print the finished map.
Input
The input is a single test case in the following form.
H W
S1
S2
...
SH
The first line holds two integers and . There are positive integers and with and , and . The next lines are the map without road cells. Line is a string of length . The -th character of is *, @ or . when and are both odd, and . otherwise. The grid holds exactly one @ and at least one city cell.
Output
Several maps can satisfy the conditions, so only the map produced by the construction below is accepted.
Number the rows to from the top and the columns to from the left. Call a cell whose row number and column number are both odd a vertex, and write vertex for the cell in row and column , where and . Two vertices whose coordinates differ by in exactly one place are neighbors, and the single cell between them is the cell that joins them.
Write down a cycle that visits each of the vertices once, in this order.
- Then, for up to in order, write when is odd, and when is even.
- Then
- Then
Two vertices written next to each other in are neighbors, and the last vertex is a neighbor of the first vertex .
Start at the vertex that holds @ and move along the order written in . On reaching the end of , continue from its beginning, and stop once all vertices have been listed once. Cut the resulting list right after its last city vertex. The journey is the remaining vertices together with the joining cell of each pair of vertices that are next to each other in the list.
Print the map on lines. Turn every cell of the journey that was empty in the input into #, and leave every other cell as it is in the input. The journey built this way passes through every city cell.