Digital Addition
Time limit2sMemory limit256 MB
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:

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 digits in each of the three lines, the printed picture is a lattice of 5 horizontal segment rows holding segments each and 4 vertical segment rows holding segments each. Number the horizontal rows to from the top, the vertical rows to from the top, and the segments inside one row from from the left. The digit in line at position , where is the first summand, the second summand and the sum, uses:
- horizontal segments , and as its top, middle and bottom segment,
- vertical segments and as its top left and top right segment,
- vertical segments and 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 (), the number of digits in each of the three lines.
The next 9 lines describe the printed picture. Line holds the values of horizontal row , and line holds the values of vertical row . 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 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 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.