[BOJ 11464] Hacking the Screen
View as PDFThe ZOO management had been wondering for a long time how to increase the number of children visitors to the ZOO. The solution was surprising and unexpected in many ways. They installed a huge screen past the entrance and started to display short quizzes on it. The child in the crowd who first shouts out the answer to the quiz question is granted one day free access to the ZOO. The screen became soon very popular and various types of quizzes are routinely shown there. One type of the quiz is the math quiz containing arithmetic operations on integers. The management worries that older siblings and friends of the children might develop a math quiz screen hacking strategy: Snap the screen with the phone, run the image recognition SW which extracts formulas from the image, evaluates them, and presents the solution to the phone holder who immediately shouts out the answer.</p>
Your task is to assess the difficulty of producing the screen hacking software. To get a better feel of the problem you will first develop a simple toy model application. Your code will read the formula presented in the preprocessed form of ASCII art and evaluate it.
입력 형식
There are multiple test cases. First line of each test case contains two integers R and C (1 ≤ R ≤ 3, 1 ≤ C ≤ 1 000). Each of the following R lines contains C characters. The whole matrix of R × C characters represents a single arithmetic formula written in ASCII art and generated by the following set of rules:</p>
FORMULA -> COMPLEX | FORMULA + COMPLEX | FORMULA - COMPLEX
COMPLEX -> SQRT | FRACTION | TERM
______
SQRT -> \/SIMPLE
SIMPLE
FRACTION -> ======
SIMPLE
SIMPLE -> TERM | SIMPLE + TERM | SIMPLE - TERM
TERM -> INTEGER | INTEGER * TERM
INTEGER -> 0 | 1 | 2 | 3 | ... | 999999 | 1000000
There are also a few additional specifications regarding the layout of the formula.
- The horizontal bar of each
SQRTis made of one or more underscore symbols (’ ’, ascii decimal code 95) and it always occupies the uppermost line of the formula in the screen. - When the formula occupies exactly two lines, then the first line contains only horizontal bars of all
SQRTparts of the formula. - When the formula occupies exactly three lines, then all
TERMs and all arithmetic operation symbols which are not part of anyFRACTIONorSQRToccupy the second line of the formula in the screen. - The length of the horizontal bar of
SQRTis the same as the length ofSIMPLEunder the bar. - The fraction bar in
FRACTIONconsists of one or more equality signs, its length is equal to the maximum of the lengths ofSIMPLEabove the bar andSIMPLEbelow the bar. - There is always exactly one space preceding and following each arithmetic operation symbol (+, -, *) on a particular line.
- The formula exactly fits in to the R × C matrix, there are no blank/empty columns in front of the whole formula or behind it.
The whole formula is evaluated according to the standard arithmetic rules. Namely: Each FORMULA and each TERM is evaluated from left to right. Each SIMPLE is also evaluated from left to right with the additional standard condition that the multiplication has higher priority than the addition/subtraction. Evaluation of SQRT and FRACTION is also standard. The value of any evaluated FORMULA, COMPLEX, SQRT, FRACTION, SIMPLE and TERM is an integer whose absolute value does not exceed 1 000 000.
There is one empty line after each test case. The input is terminated by a line with two zeros.
출력 형식
For each test case print a separate line with the value V of the input formula.
예제 입력
1 13
1 + 2 * 3 - 4
2 16
_________
\/3 * 4 - 3 + 10
3 5
6 * 4
=====
12
3 13
22 __
3 - == - \/16
11
0 0
예제 출력
3
13
2
-3
Comments