UVa 196:spreadsheet

Source: Internet
Author: User
Tags expression numeric value printf strlen

Topic Link:

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

Topic type: Search, topology sort

Topic:

In 1979, Dan Bricklin and Bob Frankston wrote VisiCalc. It became a huge success and, at which time, is the killer application for the Apple II computers. Today, the spreadsheets are found on most desktop computers.

The idea behind spreadsheets are 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 this depends on the same values of other cells. The Text and graphics can is added for presentation purposes.

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

Figure:naming of the top left cells

Translation of the title to the effect:

Background: In 1979, Dan Bricklin and Bob Frankston wrote the world's first spreadsheet application. It soon made a huge success, and was the killer app for the Apple II type computer. Today, spreadsheets are widely used in most desktop computers.

The idea behind spreadsheets, though very simple, is very powerful. A spreadsheet contains a table in which each grid contains a number or a formula. A formula is an expression, and the value of the expression depends on the value of the other grid. You can also add text and graphics to the demo.

Your task is to write a very simple spreadsheet application. Your program should accept a few spreadsheets. Each cell (lattice) in a spreadsheet contains a number (only integers) or a formula that only supports addition. Your program should calculate the values of all expressions, and then output the results of the table after the calculation.

Sample input:

310 =a1+b1+c140 =a2+b2+c2=a1+a2 =b1+b2 =c1+c2 =d1+d2

Sample output:

50 51 71 172 for the eight km

Analysis and Summary:

The first time I saw the problem, I was scared, and indeed I was really scared! Not to say how hard this problem is, but the range of data given by the topic: The table has 999 rows and 18278 columns. If you open a

Two-dimensional array, then we have to open an array of 18 million, in the current almost all OJ are directly runtime error. In other words, this problem even if the program to write, it is not possible to AC.

So he put it down for the time being. Later, my teammates told me that I was given a hole in the title, but I just needed to open a 1000*1000 array. I pour!!

Since the amount of data is so small, then it is good to do. This problem only needs to be directly recursive search.

The problem is that the data conversion, is shaped like a3,b4, AB22 these conversions, the letter is equivalent to the number of 26, which represents the number of columns, the number is to represent the line.

When using topological sorting, you need to make a transition 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.

Recursive Search Solution:

#include <cstdio> #include <cstring> #include <iostream> #include <cstdlib> #include <cctyp e> #include <string> #define MAXN 1010 #define MARK-2147483645//The lattice of the formula used to mark the result is not obtained//URL:HTTP://WWW.BI  
Anceng.cn/programming/sjjg/201410/45711.htm using namespace std;  
int SHEET[MAXN][MAXN];  
int row, col;   
String str; String FM[MAXN][MAXN];  
    Save 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]   
                = ' I ';  
                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];  
the int main () {#ifdef local freopen ("Input.txt", "R", stdin);  
    #endif int T;  
    scanf ("%d", &t);  
      
        while (t--) {scanf ("%d%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; (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; }

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.