Paper Cutting

Time limit2sMemory limit128 MB

Summary
Decide whether five fixed-shape pieces can be translated without rotation to exactly tile an L x L grid, then print the lexicographically smallest piece-number layout or gg if impossible.
Level

Medium7 of 10

Topics
Backtracking, Bit manipulation, Brute force, Implementation
Solved
No attempts yet

Problem

Song Yujin has an L x L square sheet of grid paper. The sheet has L cells across and L cells down, and each cell is a 1 x 1 square.

Cha Younghoon cut the sheet along grid lines into exactly 5 pieces. Yujin must now place the five given pieces back into an L x L square without rotating any piece.

Input

The first line contains the side length L of the square. (3 <= L <= 10)

Then the descriptions of the first through fifth pieces are given in order.

Each piece is described in the following format.

  • The first line contains the piece height N and width M. (1 <= N, M <= L)
  • The next N lines describe the shape of the piece. Each line has length M and consists only of # and ..
  • # means a cell occupied by the piece, and . means an empty cell.
  • In the given bounding rectangle, the first row, last row, first column, and last column each contain at least one #.

Output

If the five pieces can be placed without rotation inside the L x L square so that they do not overlap and every cell is filled, print the piece number occupying each cell. Pieces are numbered 1 through 5 in input order.

Print L lines, each containing the L numbers in that row.

If there are multiple valid arrangements, concatenate all rows from top to bottom and print the arrangement whose resulting string is lexicographically smallest.

If no valid arrangement exists, print gg.

Examples6

  1. Example 1

    Input
    5
    1 5
    #####
    1 5
    #####
    1 5
    #####
    1 5
    #####
    1 5
    #####
    
    Expected output
    11111
    22222
    33333
    44444
    55555
    
  2. Example 2

    Input
    10
    2 2
    ##
    ##
    8 8
    ########
    #......#
    #......#
    #......#
    #......#
    #......#
    #......#
    ########
    6 6
    ######
    #....#
    #....#
    #....#
    #....#
    ######
    4 4
    ####
    #..#
    #..#
    ####
    10 10
    ##########
    #........#
    #........#
    #........#
    #........#
    #........#
    #........#
    #........#
    #........#
    ##########
    
    Expected output
    5555555555
    5222222225
    5233333325
    5234444325
    5234114325
    5234114325
    5234444325
    5233333325
    5222222225
    5555555555
    
  3. Example 3

    Input
    8
    3 6
    #.....
    ######
    ######
    3 4
    .###
    ####
    ####
    6 5
    ###..
    ###..
    ##...
    ####.
    #####
    .####
    5 2
    .#
    .#
    .#
    ##
    ##
    3 5
    ####.
    #####
    ..###
    
    Expected output
    gg
    
  4. Example 4

    Input
    5
    1 5
    #####
    1 5
    #####
    2 5
    #####
    #####
    1 5
    #####
    1 5
    #####
    
    Expected output
    gg
    
  5. Example 5

    Input
    6
    2 2
    ##
    .#
    4 2
    .#
    ##
    ##
    ##
    4 3
    ##.
    ###
    ###
    ##.
    4 6
    ...#..
    ..##..
    ######
    ######
    1 1
    #
    
    Expected output
    331152
    333122
    333422
    334422
    444444
    444444
    
  6. Example 6

    Input
    8
    3 7
    ##.....
    .#####.
    ....###
    7 5
    #....
    ####.
    ####.
    ####.
    #####
    ..###
    ..###
    8 4
    .###
    ..##
    ...#
    ####
    ####
    .###
    .###
    .###
    2 2
    ##
    ##
    1 3
    ###
    
    Expected output
    11555333
    21111133
    22221113
    22223333
    22223333
    22222333
    44222333
    44222333