Digital Addition

Time limit2sMemory limit256 MB

Summary
Given a black and white picture formed by stacking three seven-segment digit rows, find the lexicographically smallest digit addition that could have produced it.
Level

Medium6 of 10

Topics
Implementation, Brute force, Backtracking
Solved
No attempts yet

Problem

Deidra writes an addition in columns. She writes two non-negative integers one under the other, pads them on the left with zeros so that both have the same number of digits, and writes their sum underneath. For example, 77 plus 05 gives 82. When a carry makes the sum longer than the summands, as in 96 plus 07, she prepends one zero to each summand and writes 096 plus 007 equals 103. Extra leading zeros are allowed, so 007 plus 004 equals 011 is fine, as long as all three numbers have the same length.

Deidra prints the addition on a homemade press. She prints no plus sign and no horizontal line, and every digit is one seven segment cell of this font:

digitlit segments
0top, top left, top right, bottom left, bottom right, bottom
1top right, bottom right
2top, top right, middle, bottom left, bottom
3top, top right, middle, bottom right, bottom
4top left, top right, middle, bottom right
5top, top left, middle, bottom right, bottom
6top, top left, middle, bottom left, bottom right, bottom
7top, top right, bottom right
8all seven segments
9top, top left, top right, middle, bottom right, bottom

Her press has broken spacing, so all digit cells landed on top of each other. Two horizontally adjacent digits were printed so that the two right segments of the left digit fall exactly on the two left segments of the right digit. Two vertically adjacent digits were printed so that the bottom half of the upper digit, a square of four segments, falls exactly on the top half of the lower digit.

A position looks black when at least one digit printed a black segment there. It looks white when every digit that reaches the position left that segment empty.

With ww digits in each of the three lines, the printed picture is a lattice of 5 horizontal segment rows holding ww segments each and 4 vertical segment rows holding w+1w + 1 segments each. Number the horizontal rows 00 to 44 from the top, the vertical rows 00 to 33 from the top, and the segments inside one row from 00 from the left. The digit in line ii at position jj, where i=0i = 0 is the first summand, i=1i = 1 the second summand and i=2i = 2 the sum, uses:

  • horizontal segments (i,j)(i, j), (i+1,j)(i + 1, j) and (i+2,j)(i + 2, j) as its top, middle and bottom segment,
  • vertical segments (i,j)(i, j) and (i,j+1)(i, j + 1) as its top left and top right segment,
  • vertical segments (i+1,j)(i + 1, j) and (i+1,j+1)(i + 1, j + 1) as its bottom left and bottom right segment.

Given the printed picture, restore an addition that prints as it, or report that no addition does.

Input

The first line contains an integer ww (1≤w≤1001 \le w \le 100), the number of digits in each of the three lines.

The next 9 lines describe the printed picture. Line 2i+12i + 1 holds the ww values of horizontal row ii, and line 2i+22i + 2 holds the w+1w + 1 values of vertical row ii. Inside a line the values are separated by single spaces, and each of the five horizontal rows carries one extra leading space, so every horizontal segment sits between its two vertical segments. A value of 1 is a black segment, 0 is a white one.

Output

If no addition prints as the given picture, print NO.

Otherwise print three lines of exactly ww digits each: the first summand, the second summand, and their sum. Several additions can print as the same picture. Print the one whose three lines, read as a single string, are lexicographically smallest. Since every line has exactly ww digits, this means you make the first line as small as possible, and then make the second line as small as possible. The third line is the sum of the first two, so it is fixed after that.

Examples3

  1. Example 1

    Input
    2
     1 1
    0 1 1
     1 0
    0 1 1
     1 1
    0 1 1
     1 0
    0 1 1
     0 0
    
    Expected output
    37
    34
    71
    
  2. Example 2

    Input
    1
     1
    0 1
     1
    1 1
     1
    1 1
     1
    0 1
     0
    
    Expected output
    2
    2
    4
    
  3. Example 3

    Input
    1
     1
    1 0
     1
    1 1
     1
    1 1
     1
    0 1
     0
    
    Expected output
    NO