Czech Technical University in Prague
ACM ICPC sponsored by IBM
Central Europe Regional Contest 2007
Gates of Logic
logic.c | logic.C | logic.java | logic.p
The Department of Computer Science and Engineering runs courses dealing not only with algo-
rithms but also with computer hardware. One such introductory course explains basic principles
of integrated circuits ("chips"), binary logic, boolean algebra, etc. As you may know, the very
basic units of logical circuits are called gates. A gate is an element performing one simple logical
operation. It can be connected to other gates using lines.
1
1
X
Y
1
0
=
&
1
Logical circuits may be drawn as pictures with the gates represented as squares with inputs on
the left and outputs on the right. In each square, there is a symbol that determines the gate
type: Number 1 denotes an OR gate (its outputs are 0 if and only if there is no input with the
value of 1), & is an AND gate (outputs are 1 if and only if there is no 0 input), and = is a XOR
gate (outputs are 1 if and only if there is an odd number inputs that have the value of 1).
Your task is to scan such a "picture" and compute values of all named circuit outputs. The
lines may split and join again but you may assume that each "value consumer" (input port of
a gate or a named output) will be connected to exactly one "value source" (output port of a gate
or an input value). There will be no feedback loops, i.e., there exists no cycle that would lead
through the same gate twice.
Input Specification
The input contains several pictures. Each picture consists of at least one and at most 200 rows
composed of the following characters:
* Space (" "). Empty space in the picture. Spaces are used to indent other characters to
appropriate locations, because the exact position of characters is often important. Trailing
spaces at the end of input rows may be present but may also be left out.
* Dash ("-"). Horizontal line. It connects characters on its left and right together, those
characters will always exist and be able to "accept" the connection.
* Pipe ("|"). Vertical line, connects characters that are directly above and below. Like
with the horizontal line, those characters will always accept the connection.
* Plus sign ("+"). Line connection or a bend. Connects characters on all four sides. All
characters that are able to accept the connection are considered connected (there will
always be at least two). However, there may be sides that contain a non-empty character
that is not connected. For example, if a dash is present on a position directly below the
plus sign, they are not considered connected.
* Lowercase letter x ("x"). Crossing of two lines without a connection. All four neigh-
boring characters will accept the connection. The character above is connected to the
one below and the character to the left with the one on the right, but there is no mutual
connection between these two pairs.