uva 196 - Spreadsheet


 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
根据题目要求数据量极大,百度后ac的代码显示最多为1000X1000
纵坐标是十进制1-999;横坐标是类似26进制A-ZZZ数据最大到1000,
开始天真的以为边读入边算即可,wrong了马上意识到可能前面的值,依赖后面的值可能还存在嵌套关系,
于是dfs,由于初始化所有值为0,因此每次判断当前值,依赖的那个值是否已经知道的条件自然而然时!=0;后来换了标记数组就ac了,表格里的值时可以为0的
看到别人用的拓扑排序,本来wrong了之后想用的,后来还是坚持改自己的终于ac,坚持就是胜利~\(≧▽≦)/~啦啦啦
#include <stdio.h>
#include <string.h>
struct node
{int x[50],y[50],z,num; //存放计算a[I][J]需要的值的坐标,只保存读入是还不知道的值
} a[1000][1000];
int visit[1000][1000]; //标记值是否已经算出来,开始偷懒给所有值初始化为0,用是否0判断是否算出来,表格数据中刚好有值为0,(Y-Y),
int dfs(int X,int Y)
{int i;
 if (a[X][Y].z==0) return a[X][Y].num;
 else
 {
  for (i=1;i<=a[X][Y].z;i++)
  a[X][Y].num=a[X][Y].num+dfs(a[X][Y].x[i],a[X][Y].y[i]); //有向无环图,随便找个点dfs,一定可以找到一条路径结束
  a[X][Y].z=0; //标记为0可以表示a[X][Y]已近计算出来。
  return a[X][Y].num;
 }
}
int main()
{int f,t,l,i,j,k,n,m,num,top,tail,column,row,pos;
 char s[2000];
 scanf("%d",&t);
 while (t--)
 {scanf("%d %d",&n,&m);
  memset(visit,0,sizeof(visit)); 
  for (i=1;i<=m;i++)
  for (j=1;j<=n;j++)
  {a[i][j].z=0;a[i][j].num=0;}
  for (i=1;i<=m;i++)
  {getchar();
   for (j=1;j<=n;j++)
   {scanf("%s",&s);
    l=strlen(s);
    a[i][j].z=0;
    if (s[0]!='=')
       {num=0; f=0; if (s[0]=='-') f=1;
        for (k=f;k<l;k++)
        num=num*10+s[k]-'0';
        a[i][j].num=num;
        if (f==1) a[i][j].num=-a[i][j].num;
        visit[i][j]=1;
       }
       else
       {column=0; row=0; pos=1;
        a[i][j].num=0;
        while (pos<l)
        {while ((s[pos]>='A')&&(s[pos]<='Z')) {column=column*26+s[pos]-'A'+1; ++pos;}
         while ((pos<l)&&(s[pos]>='0')&&(s[pos]<='9')) {row=row*10+s[pos]-'0'; ++pos;}
         if ((visit[row][column]==1)||((i==row)&&(j==column))) a[i][j].num=a[i][j].num+a[row][column].num;
                            else {++a[i][j].z;a[i][j].x[a[i][j].z]=row; a[i][j].y[a[i][j].z]=column;}
         row=0; column=0; ++pos;
        }
       }
   }
  }

  for (i=1;i<=m;i++)
  for (j=1;j<=n;j++)
  if (a[i][j].z>0) dfs(i,j);
  for (i=1;i<=m;i++)
  {for (j=1;j<n;j++)
   printf("%d ",a[i][j].num);
   printf("%d\n",a[i][n].num);
  }
 }
 return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值