Under construction

Still being built. Things may change

RPG Maker

Time limit2sMemory limit512 MB

Summary
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 HH rows and WW 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.

  1. The journey holds as many city cells as possible.
  2. The journey consists of distinct cells that are not empty.
  3. The journey begins at the start cell.
  4. The journey ends at a city cell.
  5. Every road cell belongs to the journey. No road cell lies outside the journey.
  6. 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.
  7. 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 HH and WW. There are positive integers nn and mm with H=4n−1H = 4n - 1 and W=4m−1W = 4m - 1, and 1≤n,m≤101 \le n, m \le 10. The next HH lines are the map without road cells. Line i+1i+1 is a string SiS_i of length WW. The jj-th character of SiS_i is *, @ or . when ii and jj 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 11 to HH from the top and the columns 11 to WW from the left. Call a cell whose row number and column number are both odd a vertex, and write vertex (i,j)(i, j) for the cell in row 2i+12i+1 and column 2j+12j+1, where 0≤i<2n0 \le i < 2n and 0≤j<2m0 \le j < 2m. Two vertices whose coordinates differ by 11 in exactly one place are neighbors, and the single cell between them is the cell that joins them.

Write down a cycle CC that visits each of the 4nm4nm vertices once, in this order.

  1. (0,0),(0,1),…,(0,2m−1)(0, 0), (0, 1), \ldots, (0, 2m-1)
  2. Then, for i=1i = 1 up to i=2n−1i = 2n-1 in order, write (i,2m−1),(i,2m−2),…,(i,1)(i, 2m-1), (i, 2m-2), \ldots, (i, 1) when ii is odd, and (i,1),(i,2),…,(i,2m−1)(i, 1), (i, 2), \ldots, (i, 2m-1) when ii is even.
  3. Then (2n−1,0)(2n-1, 0)
  4. Then (2n−2,0),(2n−3,0),…,(1,0)(2n-2, 0), (2n-3, 0), \ldots, (1, 0)

Two vertices written next to each other in CC are neighbors, and the last vertex (1,0)(1, 0) is a neighbor of the first vertex (0,0)(0, 0).

Start at the vertex that holds @ and move along the order written in CC. On reaching the end of CC, continue from its beginning, and stop once all 4nm4nm 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 HH 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.

Examples2

  1. Example 1

    Input
    11 7
    .......
    .......
    *.....*
    .......
    ..@....
    .......
    *......
    .......
    ....*..
    .......
    .......
    
    Expected output
    #######
    #.....#
    *.....*
    #......
    #.@####
    #.....#
    *.#####
    #.#....
    #.##*##
    #.....#
    #######
    
  2. Example 2

    Input
    7 11
    ........*..
    ...........
    ...........
    ...........
    ....*...*..
    ...........
    ..*.@...*..
    
    Expected output
    ########*##
    #.........#
    #.#########
    #.#........
    #.##*###*##
    #.........#
    ##*#@...*##