Ultraviolet A 196-spreadsheet

Source: Internet
Author: User

Question link:

Http://uva.onlinejudge.org/index.php? Option = com_onlinejudge & Itemid = 8 & category = 105 & page = show_problem & problem = 132

Question type: search, Topology Sorting

Question:

In 1979, Dan Bricklin and Bob Frankston wrote visic1c, the first spreadsheet application. It became a huge success and, at that time, was the killer application for the Apple II computers. Today, spreadsheets
Are found on most desktop computers.

The idea behind spreadsheets is very simple, though powerful. a spreadsheet consists of a table where each cell contains either a number or a formula. A formula can compute an expression that depends on the values
Of other cells. text and graphics can be added for presentation purposes.

You are to write a very simple spreadsheet application. Your program shocould accept several spreadsheets. Each cell of the spreadsheet contains either a numeric value (Integers only) or a formula, which only support
Sums. After having computed the values of all formulas, your program shocould output the resulting spreadsheet where all formulas have been replaced by their value.


Figure:Naming of the top left Cells

Translation:

Background: In 1979, Dan Bricklin and Bob Frankston wrote the world's first spreadsheet application. It quickly achieved great success, namely the killer application software of Apple II. Today, workbooks are widely used in most desktop computers.


The ideas behind workbooks are simple but powerful. A workbook contains a table. Each grid contains a number or a formula. A formula is an expression. The value of this expression depends on the value of other grids. You can also add text and graphics for demonstration.


Your task is to write a very simple spreadsheet application. Your program should accept several workbooks. Each cell (GRID) in the workbook contains a number (only an integer) or a formula. This formula only supports addition. Your program should calculate the values of all expressions and output the results of the table after calculation.

Sample input:

14 310 34 37 =A1+B1+C140 17 34 =A2+B2+C2=A1+A2 =B1+B2 =C1+C2 =D1+D2

Sample output:

10 34 37 8140 17 34 9150 51 71 172

Analysis and Summary:


The first time I saw this question, I was scared, but it was really scared! This is not how difficult the question is, but the range of data given by the question: The table contains 999 rows and 18278 columns. If you open

If the two-dimensional array is used, it is necessary to open an array of million, which is a direct runtime error on almost all OJ. That is to say, even if the program is written, it is impossible for the AC.


So I put it down temporarily. Later, my teammates told me that I was thrown into trouble by the question. In fact, I only need to open a 1000*1000 array. Me !!

Since the amount of data is so small, it is easy to do. You only need to use recursive search for this question.


What is troublesome about the question is the conversion of data, which is like A3, B4, and ab22. Letters are equivalent to the number of columns in the 26th notation, A number indicates a row.

When using topological sorting, You need to convert a two-dimensional array into an array. Of course, in addition to the search solution, you can also use topological sorting to solve the problem.



Recursive search solution:

# Include <cstdio> # include <cstring> # include <iostream> # include <cstdlib> # include <cctype> # include <string> # define maxn 1010 # define mark-2147483645/ /used to mark the unobtained result, which is the lattice using namespace STD of the formula; int sheet [maxn] [maxn]; int row, Col; string STR; string FM [maxn] [maxn]; // Save the formula int DFS (int r, int C) {If (sheet [r] [c]! = Mark) return sheet [r] [c]; If (sheet [r] [c] = mark) {int m_row, m_col; char temp [maxn]; string STR = FM [r] [c]; sheet [r] [c] = 0; For (INT I = 1, Pos = 0; I <= Str. size () + 1; ++ I) {If (STR [I] = '+' | I = Str. size () {temp [POS] = '\ 0'; m_row = 0, m_col = 0; int K; For (k = 0; k <strlen (temp) &&! Isdigit (temp [k]); ++ K) {m_col = m_col * 26 + temp [k]-'A' + 1 ;}for (; k <strlen (temp); ++ K) {m_row = m_row * 10 + temp [k]-'0';} Pos = 0; sheet [r] [c] + = DFS (m_row, m_col);} else {temp [POS ++] = STR [I] ;}} return sheet [r] [c] ;}} int main () {# ifdef local freopen ("input.txt", "r", stdin); # endif int T; scanf ("% d", & T); While (t --) {scanf ("% d", & Col, & Row); memset (sheet, 0, sizeof (sheet); int val; For (INT I = 1; I <= row; ++ I) {for (Int J = 1; j <= Col; + + J) {CIN> STR; If (STR [0] = ') {sheet [I] [J] = mark; FM [I] [J] = STR;} else {val = atoi (Str. c_str (); sheet [I] [J] = Val ;}}for (INT I = 1; I <= row; ++ I) {for (Int J = 1; j <= Col; ++ J) if (sheet [I] [J] = mark) {DFS (I, j );}} for (INT I = 1; I <= row; ++ I) {printf ("% d", sheet [I] [1]); For (Int J = 2; j <= Col; ++ J) {printf ("% d", sheet [I] [J]);} printf ("\ n ");}} return 0 ;}


-- The meaning of life is to give it meaning.

Original Http://blog.csdn.net/shuangde800 ,
D_double





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.