UVA 196 & POJ 1420 Spreadsheet (DFS)

http://poj.org/problem?id=1420

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



 Spreadsheet 

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

Input

The first line of the input file contains the number of spreadsheets to follow. A spreadsheet starts with a line consisting of two integer numbers, separated by a space, giving the number of columns and rows. The following lines of the spreadsheet each contain a row. A row consists of the cells of that row, separated by a single space.

A cell consists either of a numeric integer value or of a formula. A formula starts with an equal sign (=). After that, one or more cell names follow, separated by plus signs (+). The value of such a formula is the sum of all values found in the referenced cells. These cells may again contain a formula. There are no spaces within a formula.

You may safely assume that there are no cyclic dependencies between cells. So each spreadsheet can be fully computed.

The name of a cell consists of one to three letters for the column followed by a number between 1 and 999 (including) for the row. The letters for the column form the following series: A, B, C, ..., Z, AA, AB, AC, ..., AZ, BA, ..., BZ, CA, ..., ZZ, AAA, AAB, ..., AAZ, ABA, ..., ABZ, ACA, ..., ZZZ. These letters correspond to the number from 1 to 18278. The top left cell has the name A1. See figure 1.

Output

The output of your program should have the same format as the input, except that the number of spreadsheets and the number of columns and rows are not repeated. Furthermore, all formulas should be replaced by their value.

Sample Input

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

Sample Output

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

模拟EXCEL的求和操作,注意有可能运算改格之后的单元格,所以这里用到DFS,已经有值的格子不必再搜,没有值的继续搜

数据范围虽然很大,但实际上开个1000×1000的数组就够了,POJ上100×100都能过,很多人说1000×18278的数组会RE,我对此只想表示呵呵,人云亦云,UVA上根本不限内存,我开了1000×20000的数组在UVA上获得了AC,不过POJ上肯定要MLE的,还有POJ上的数据好像太水了,我有漏洞的代码居然也过了


#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<algorithm>
#include<ctime>
#include<cctype>
#include<cmath>
#include<string>
#include<cstring>
#include<stack>
#include<queue>
#include<list>
#include<vector>
#include<map>
#include<set>
#define sqr(x) ((x)*(x))
#define LL long long
#define itn int
#define INF 0x3f3f3f3f
#define PI 3.1415926535897932384626
#define eps 1e-10
#define mm

using namespace std;

typedef pair<int,int> PAIR;

int n,m;
int MAP[1010][1010];
char s[100];
vector<PAIR> v[1010][1010];

void save(int i,int j)
{
    int p=0;
    int x=0,y=0;
    v[i][j].clear();
    while (s[p]!='\0')
    {
        p++;
        while (isalpha(s[p]))
        {
            x=x*26+s[p]-64;
            p++;
        }
        while (isdigit(s[p]))
        {
            y=y*10+s[p]-48;
            p++;
        }
        //printf("x=%d y=%d\n",x,y);
        v[i][j].push_back(PAIR(y,x));
        x=y=0;
    }
}

int dfs(int x,int y)
{
    if (MAP[x][y]!=-1)  return MAP[x][y];

    int res=0;

    for (int i=0;i!=v[x][y].size();i++)
        res+=dfs(v[x][y][i].first,v[x][y][i].second);

    return res;
}

int main()
{
    #ifndef ONLINE_JUDGE
        freopen("/home/fcbruce/文档/code/t","r",stdin);
    //    freopen("/home/fcbruce/文档/code/t1","w",stdout);
    #endif // ONLINE_JUDGE

    int tt;
    scanf("%d",&tt);

    while (tt--)
    {
        scanf("%d%d",&m,&n);
        memset(MAP,-1,sizeof(MAP));
        for (int i=1;i<=n;i++)
        {
            for (int j=1;j<=m;j++)
            {
                scanf("%s",s);
                if (isdigit(s[0]))
                {
                    MAP[i][j]=atoi(s);
                }
                else
                {
                    //puts(s);
                    save(i,j);
                    //puts("----------------------");
                }
            }
        }

        for (int i=1;i<=n;i++)
        {
            for (int j=1;j<=m;j++)
            {
                if (MAP[i][j]==-1)
                {
                    MAP[i][j]=dfs(i,j);
                }

                printf(j==m?"%d\n":"%d ",MAP[i][j]);
            }
        }
    }

    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值