UVa 196 - Spreadsheet

196 - Spreadsheet
If anyone of you is native english writing person, please correct my rather messy text. Thanx.
 * http://acm.uva.es/p/v1/195.html

Summary
This is a standard Category:I don't know problem, you just need to replace formulas from input with numbers.

Explanation
This is a standard Category:I don't know 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.

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

My Way
I used Java, so my description may not work for other languages. My program runs in iterations, one for each sheet. Each iteration works as follows:

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

Now that we have all input loaded: 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. Repeat until there's no formula left. Print output.

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