acm cz

image

Czech ACM Student Chapter Czech Technical University in Prague

image

Charles University in Prague Technical University of Ostrava Slovak University of Technology Pavol Jozef Sˇaf´arik University in Koˇsice

University of Zˇilina Masaryk University Matej Bel University in Bansk´a Bystrica University of West Bohemia


CTU Open Contest 2015

image


Hacking the Screen


screen.c, screen.cpp, Screen.java, screen.py


The 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.


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.


Input Specification


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:


image

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.


image


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.


Output Specification


For each test case print a separate line with the value V of the input formula.


Sample Input


1 13

1 + 2 * 3 - 4


2 16


image

\/3 * 4 - 3 + 10


3 5

6 * 4

===== 12

Output for Sample Input


3

13

2

-3


3 13


22

3 - == - \/16

11


0 0