Border

Time limit1sMemory limit256 MB

Summary
Given an N by N grid of species (N at most 4), find a non-self-crossing border path from the top-left to the bottom-right corner so that different species end up in separate regions, or report that none exists.
Level

Hard8 of 10

Topics
Graph, Brute force, Implementation, Geometry
Solved
No attempts yet

Problem

There is a country of many animals living on an island surrounded on all sides by the sea.

The country can be represented as an N×N rectangular grid, and inside each cell lives at most one species.

Recently, frequent conflicts between different species have broken out, so the country wants to split one country into several countries so that different species do not meet. To do this, it wants to draw a border that satisfies the following conditions.

  • The border must be a single path that starts at the top-left corner and goes to the bottom-right corner.
  • The path of the border cannot enter the interior of a cell and must be made by passing only along the cell's line segments. It may also move along line segments adjacent to the sea.
  • The path of the border must not pass through a corner it has already passed through.
  • Different species must not be able to meet without crossing the border. Animals cannot move through the sea.
  • It is fine if animals of the same species cannot meet each other.
  • It is fine if there is no species in the country at all.

« A 4×4 island and a border image that satisfies the conditions »

Draw a border that satisfies the above conditions.

Input

The first line gives the integer N (2 ≤ N ≤ 4), the size of the cells.

The next N lines give strings of length N representing the cell information. The j-th character si,j of the i-th string represents the information of the cell in row i, column j. If the cell has no animal, si,j is '.'; if it has an animal, the species type is given as an uppercase letter.

Animals of the same species have the same letter, and different species have different letters.

The input is given only when at least two species live there.

Output

If no border satisfying the conditions exists, output "no" on the first line and output nothing further.

If a border satisfying the conditions exists, output "yes" on the first line, and then output the path from the top-left to the bottom-right in the following 2N+3 lines, each as a string of length 4N+3, according to the format below.

Each cell is represented by 3 x 5 characters as below. The 4 '+' characters mark the corners, the exact center shows the cell information si,j, and both sides are left blank.

+   +
  .  
+   +

Join the cells in an N x N arrangement so that they share corners, thereby representing the whole grid. Below is the shape when N is 3 and 9 cells are joined.

+   +   +   +
  .   .   .  
+   +   +   +
  .   .   .  
+   +   +   +
  .   .   .  
+   +   +   +

For the path, each traversed edge is shown as three consecutive '-' for horizontal edges and as '|' for vertical edges. The example below shows the path when moving in the order down, down, right, up, right, right, down, left, down, right.

+   +   +   +
| .   .   .  
+   +———+———+
| . | .   . |
+———+   +———+
  .   . | .  
+   +   +———+

Finally, place the character '#', representing the sea, around the cells.

###############
#+   +   +   + #
#| .   .   .  #
#+   +———+———+#
#| . | .   . |# 
#+———+   +———+#
#  .   . | .  #
#+   +   +———+#
###############

In the above case, the island is divided into 3 regions in total: {(1, 1), (1, 2), (1, 3), (2, 1)}, {(2, 2), (2, 3), (3, 1), (3, 2)}, {(3, 3)}.

Examples5

  1. Example 1

    Input
    4
    AB.C
    ....
    .C.A
    A..C
    
    Expected output
    yes
    ###################
    #+---+   +---+   +#
    #  A | B | . | C  #
    #+   +---+   +---+#
    #  .   .   .   . |#
    #+   +---+---+   +#
    #  . | C   . | A |#
    #+   +---+   +---+#
    #  A   . | .   C  #
    #+   +   +---+---+#
    ###################
    
  2. Example 2

    Input
    4
    ....
    GORI
    ....
    BO.J
    
    Expected output
    yes
    ###################
    #+---+   +---+---+#
    #  . | . | .   . |#
    #+   +   +   +---+#
    #  G | O | R | I  #
    #+---+   +   +---+#
    #| .   . | .   . |#
    #+---+   +   +---+#
    #  B | O | . | J  #
    #+   +---+   +---+#
    ###################
    
  3. Example 3

    Input
    4
    AAAA
    B...
    ...A
    BBBB
    
    Expected output
    yes
    ###################
    #+---+---+---+---+#
    #  A   A   A   A |#
    #+---+---+   +---+#
    #| B   . | . | .  #
    #+---+   +   +---+#
    #  . | . | .   A |#
    #+---+   +---+---+#
    #| B   B   B   B  #
    #+---+---+---+---+#
    ###################
    
  4. Example 4

    Input
    3
    XYY
    Y.X
    YX.
    
    Expected output
    yes
    ###############
    #+---+   +   +#
    #  X | Y   Y  #
    #+---+   +---+#
    #| Y   . | X |#
    #+   +---+   +#
    #| Y | X   . |#
    #+---+   +   +#
    ###############
    
  5. Example 5

    Input
    2
    XY
    YX
    
    Expected output
    no