Problem D: Cheat Case
Problem
Faced with an upcoming math test and certain that even studying now won't save us from failing, we decide to cheat.
Our math teacher, teacher A, is known for hand-writing the math test papers. Our plan is to steal the brush-strokes used by the teacher while creating the test's problems with a pen that can wiretap the brush strokes, a device made by a professor in our neighborhood. However, the professor was unable to create a program to interpret the brush-strokes and reconstruct the problems. Worse, even if the problems could be reconstructed, we wouldn't be smart enough to solve them. Out of options, we decide to ask you, a programmer, to create a program that can interpret brush-strokes and solve the resulting problems.
This test will have problems where you'll have to solve arithmetic expressions. The arithmetic symbols are only + (addition), - (subtraction), and · (multiplication); multiplication takes priority over addition and subtraction. The expressions will always have the correct syntax. In addition, there will not be any numbers other than 0 with a leading 0.
The paper on which the problems are written can be mapped as a grid. The brush-strokes are given as a set of line segments, where each segment is either horizontal or vertical along the X-axis or has the same starting and ending point (a dot). This line segment represents filling  ...