Under construction

Still being built. Things may change

Bar Code

Time limit10sMemory limit1024 MB

Summary
Given the group counts for each row and column, place non-touching bars on the n by n grid borders so every line matches its specification.
Level

Medium5 of 10

Topics
Backtracking, Implementation
Solved
No attempts yet

Problem

Bar Code is a puzzle invented by Thinh Van Duc Lai in 2017. The New York Times first published it.

The puzzle is played on a square grid of n×nn \times n unit squares, which has (n+1)×(n+1)(n+1) \times (n+1) grid points. For a given specification, mark some of the borders of the unit squares by drawing a vertical or horizontal bar. The markings must satisfy these conditions:

  • For each row and column, the specification gives how many separate groups of consecutive bars of the same orientation there must be. For example, the specification 2 1 1 requires 3 groups of 2, 1, and 1 bars, with at least one unmarked border between groups.
  • No 2 bars may touch.

For example, here is a n=6n = 6 puzzle with 77 borders in each row. The following markings meet this specification:

1101010
1101001
1100101
0110101

Here 1 denotes a bar and 0 denotes an unmarked border.

The illustration shows the solution for sample input 3.

Input

The first line contains an integer nn (0<n≤90 < n \le 9). The next nn lines describe the vertical bars. Line ii lists the group sizes for row ii, counting from the top. Each line has one or more non-negative integers. If a line is the single integer 0, no borders may be marked in that row. Otherwise, none of the integers on the line is 0.

The following nn lines describe the horizontal bars. Line jj lists the group sizes for column jj, counting from the left, in the same format.

Output

Print nn strings of length n+1n+1 for the vertical borders. The jj-th character of the ii-th string is 1 if the jj-th vertical border in row ii is a bar, and 0 otherwise.

Then print n+1n+1 strings of length nn for the horizontal borders. The jj-th character of the ii-th string is 1 if the ii-th horizontal border in column jj is a bar, and 0 otherwise.

Rows are counted from top to bottom, and columns from left to right. If several solutions exist, you may print any of them. You may assume that at least one valid marking exists.

Examples3

  1. Example 1

    Input
    2
    1
    0
    0
    3
    
    Expected output
    100
    000
    01
    01
    01
    
  2. Example 2

    Input
    3
    0
    1 1
    1
    1 1
    1
    1
    
    Expected output
    0000
    1001
    0010
    101
    010
    000
    100
    
  3. Example 3

    Input
    6
    1 2
    1 1
    3
    1 1
    1
    2 1
    0
    1
    1 2
    1 1
    1
    1 1
    
    Expected output
    0100011
    1000100
    0111000
    1000001
    0000010
    1100100
    000100
    001000
    000001
    000010
    010100
    001000
    001001