This page is still under construction.

Parts of this page are still being built. What you see may change.

Another Puzzling Problem

Interview

Time limit1sMemory limit128 MB

Summary
Each jigsaw piece carries four integer edge labels; match opposite labels to place every piece in the N by N grid, then print the assembled picture.
Level

Medium5 of 10

Topics
Implementation, Brute force, Matrix, Hash map
Solved
No attempts yet

Problem

Write a program that solves jigsaw puzzles. The input describes the size of the puzzle, the size of the pieces, and every piece. Each piece is drawn with ASCII characters. Your program must print the solved puzzle: all pieces assembled into their correct positions.

Input

The first line contains three integers NN, HH, and WW: the number of pieces along one side of the puzzle (a puzzle is always an N×NN \times N square), the height of a piece, and the width of a piece. Every piece has the same size. The bounds are 2≤N≤102 \le N \le 10 and 1≤H,W≤251 \le H, W \le 25. For example, 2 2 3 describes a 2×22 \times 2 puzzle whose pieces are each 22 characters tall and 33 characters wide.

The remaining input describes the N×NN \times N pieces in arbitrary order. Each piece consists of its image — exactly HH lines of WW characters — followed by one line with four integers in the range [−5,5][-5, 5]: the shapes of the top, left, bottom, and right edges, in that order. A value of 00 marks a straight (outer) edge. Two edges fit together exactly when their values are opposite and sum to 00 (for example +5+5 locks into −5-5, and +4+4 into −4-4). Pieces are never rotated, and no two pieces share the same four edge values (all pieces are distinct). A blank line separates consecutive pieces.

Spaces (ASCII code 32) are ordinary characters and may appear anywhere in a piece, including at the end of a line or as an entire line; they always appear in the input where they belong. Every piece is a solid rectangular block of characters (ASCII codes 32 to 127), so treat a space exactly like any other character.

Output

Print the solved puzzle. There is exactly one way to lay out the N×NN \times N pieces: every outer edge (value 00) lies on the border, the right edge of each piece and the left edge of its right-hand neighbour sum to 00, and the bottom edge of each piece and the top edge of the piece below it sum to 00. Print the resulting picture, which is N⋅HN \cdot H lines of N⋅WN \cdot W characters. The input always has exactly one solution.

Examples4

  1. Example 1

    Input
    2 8 14
    88,           
    8888.         
    :8888b        
      8888        
    -.:888b       
    ' d8888       
     ,88888       
    ':88888       
    0 3 -1 0
    
             o8%88
           o88%888
          8'-    -
         8'       
        d8.-=. ,==
        >8 `~` :`~
        88        
        88b. `-~  
    0 0 -5 -3
    
    .:88888       
    :::8888       
    :' 8888b      
        8888b     
        ,%888b.   
        %%%8--'-. 
       _%-' ---  -
    .-'   =  --.  
    1 4 0 0
    
        888b ~==~ 
        88888o--:'
        `88888| ::
        8888^^'   
       d888       
      d88%        
     /88:.__ ,    
         '''::===.
    5 0 0 -4
    
    Expected output
             o8%8888,           
           o88%8888888.         
          8'-    -:8888b        
         8'         8888        
        d8.-=. ,==-.:888b       
        >8 `~` :`~' d8888       
        88         ,88888       
        88b. `-~  ':88888       
        888b ~==~ .:88888       
        88888o--:':::8888       
        `88888| :::' 8888b      
        8888^^'       8888b     
       d888           ,%888b.   
      d88%            %%%8--'-. 
     /88:.__ ,       _%-' ---  -
         '''::===..-'   =  --.  
    
  2. Example 2

    Input
    2 1 1
    ,
    0 0 5 4
    
    _
    -5 0 0 1
    
    8
    2 -1 0 0
    
    .
    0 -4 -2 0
    
    Expected output
    ,.
    _8
    
  3. Example 3

    Input
    2 3 5
    _8`~-
    @^,._
    b*,--
    0 -2 3 0
    
    b|8^b
    =|'8=
    =~,b'
    -3 2 0 0
    
    *.%|O
    .`*#%
    88O'@
    -2 0 0 -2
    
    8`.^,
    ,._~8
    b-|:|
    0 0 2 2
    
    Expected output
    8`.^,_8`~-
    ,._~8@^,._
    b-|:|b*,--
    *.%|Ob|8^b
    .`*#%=|'8=
    88O'@=~,b'
    
  4. Example 4

    Input
    3 2 4
    %-o%
    `.~/
    4 0 0 -3
    
    /=Ob
    _|*_
    5 0 -4 4
    
    O`@-
    =#:^
    0 1 -2 5
    
    |-%*
     @O|
    2 -2 5 0
    
    |b%|
    o^#O
    -5 3 0 2
    
    b#**
    |@'o
    2 -4 5 2
    
    *.b8
    ~@%8
    0 -5 -2 0
    
    -/o*
    _ .:
    0 0 -5 -1
    
    ~@%*
    8|#:
    -5 -2 0 0
    
    Expected output
    -/o*O`@-*.b8
    _ .:=#:^~@%8
    /=Obb#**|-%*
    _|*_|@'o @O|
    %-o%|b%|~@%*
    `.~/o^#O8|#: