UVa 196 - Spreadsheet

题目链接:

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


题目:

In 1979, Dan Bricklin and Bob Frankston wrote VisiCalc, 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 should 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 should output the resulting spreadsheet where all formulas have been replaced by their value.

figure22
Figure:Naming of the top left cells



题目大意翻译:

背景:1979年, Dan Bricklin和Bob Frankston写出了世界上第一款电子表格应用程序。它很快取得了巨大的成功,是当时苹果II型电脑的杀手级应用软件。今天,电子表格在大多数桌面计算机中广泛应用。


电子表格背后的思想虽然非常简单, 但是却非常强大。电子表格包含一个表,其中每个格子中会包含一个数字或公式。一个公式是一个表达式,这个表达式的值取决于其他格子的值。并且还可以添加文本和图形进行演示。


你的任务是写一个非常简单的电子表格应用程序。你的程序应该接受几个电子表格。电子表格中的每个单元(格子)会包含一个数值(只有整数)或一个公式, 这个公式只支持加法。 你的程序应该计算出所有表达式的值,计算完后输出表格的结果。


样例输入:

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


样例输出:

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

分析与总结:


第一次看到这题是,我被吓到了,的的确确真的被吓到了! 倒不是说这题有多么多么地难,而是题目所给的数据范围: 表格有999行,18278列。 如果开一个

二维数组的话,那么就要开个1千8百万的数组, 在当前几乎所有的OJ上都是直接Runtime Error的。也就是说,这题就算写出程序来,也不可能AC的。


于是便暂时放下了。 后来,队友告诉我被题目给坑了, 实际上只需要开个1000*1000的数组就足矣了。 我倒!!

既然数据量这么少,那么就好办了。 这题只需要直接用递归搜索即可。


题目比较麻烦的是数据的转换,就是形如A3,B4, AB22这些的转换,字母相当于是26进制的数,表示的是列数, 数字的是表示行。

用拓扑排序做时,需要做一个转换, 把二维数组转换成一位数组。当然,除了搜索的解法,还可以用拓扑排序来解。



递归搜索解法:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<cstdlib>
#include<cctype>
#include<string>
#define MAXN 1010 
#define MARK -2147483645  //用来标记未得到结果的、是公式的格子

using namespace std;
int sheet[MAXN][MAXN];
int row, col;
string str; 
string fm[MAXN][MAXN]; // 保存公式


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 %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;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值