UVa 196 - Spreadsheet

196 - Spreadsheet

 * http://acm.uva.es/p/v1/196.html

Summary
This is an topological sort problem, you just need to replace formulas from input with numbers.

Explanation
This is an topological sort problem. All you need to do is: load integers and formulas, replace formulas with numbers according to the numbers they point to and print output. My program runs in iterations, one for each sheet. Each iteration works as follows:


 * 1) Load up m and n.
 * 2) Create 2-dimenzional m*n field of Integers. (class Integer, not type int)
 * 3) Create 1D m*n field of Strings. (Since UVa didn't allow Vector or List, i had to use the static way.)
 * 4) For each line of input:
 * 5) Load line of input as String.
 * 6) Parse that String using StringTokenizer.
 * 7) For each token:
 * 8) If the parsed token is number, place it in the corresponding space in 2D field.
 * 9) If the parsed token is formula (starts with '='), transform formula into some more readable form.
 * 10) (Example: formula A3 = A2+A1 looks in my program as 3 1 2 1 1 1. Rows first, columns second.)
 * 11) Place modified formula into 1D field of Strings and null into corresponding space in 2D field.

Now that we have all input loaded:
 * 1) Browse through that 1D field of formulas and check them, if you know all of their members (all formula members pointing to 2D field have non-null value). If you do, fill target 2D space with corresponding value and remove that formula from 1D field. If not, skip it and continue with another one.
 * 2) Repeat until there's no formula left.
 * 3) Print output.

Gotchas
Watch out for X and Y. Letter is column, number is line.

Input
1 4 3 10 34 37 =A1+B1+C1 40 17 34 =A2+B2+C2 =A1+A2 =B1+B2 =C1+C2 =D1+D2

Output
10 34 37 81 40 17 34 91 50 51 71 172