Spreadsheet
Spreadsheet |
In 1979, Dan Bricklin and Bob Frankston wrote VisiCalc, the first spreadsheetapplication. It became a huge success and, at that time, was the killerapplication for the Apple II computers. Today, spreadsheets are found onmost desktop computers.
The idea behind spreadsheets is very simple, though powerful. A spreadsheetconsists 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 othercells. Text and graphics can be added for presentation purposes.
You are to write a very simple spreadsheet application. Your program shouldaccept several spreadsheets. Each cell of the spreadsheet contains eithera numeric value (integers only) or a formula, which only support sums. Afterhaving computed the values of all formulas, your program should output theresulting spreadsheet where all formulas have been replaced by their value.
Figure: Naming of the top left cells
Input
The first line of the input file contains the number of spreadsheets tofollow. A spreadsheet starts with a line consisting of two integer numbers,separated by a space, giving the number of columns and rows. The followinglines of the spreadsheet each contain a row. A row consists of the cellsof that row, separated by a single space.
A cell consists either of a numeric integer value or of a formula. A formulastarts 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 allvalues 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 followedby a number between 1 and 999 (including) for the row. The letters for thecolumn 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 leftcell has the name A1. See figure 1.
Output
The output of your program should have the same format as the input, exceptthat the number of spreadsheets and the number of columns and rows are notrepeated. 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
解决方案:首先做这题时,我题意都没理解好。本题说的是给一个表格,有的单元是直接数字,有的是公式。然后输出表格的值,数字直接输出,但公式要算出值再输出。这题要注意公式中的有些单元格的值未必在前面已经得出,必须先把后面已知的算出才知道。所以,本题可以直接递归就可以了。
代码:
#include<iostream>
#include<cstdio>
#include<sstream>
#include<cstdlib>
#include<map>
using namespace std;
int grid[1000][18279];
char fm[1000][18279][100];
bool done[1000][18279];
int solve(int i,int j)
{
if(done[i][j]) return grid[i][j];
for(int k=0;fm[i][j][k];k++)
{
if(fm[i][j][k]=='+'||fm[i][j][k]=='=')
fm[i][j][k]=' ';
}
istringstream ss(fm[i][j]);
string s;
int sum=0;
while(ss>>s)
{
int r=0,c=0;
for(int k=0;s[k];k++)
{
if(isdigit(s[k]))
r=r*10+s[k]-'0';
else c=c*26+s[k]-'A'+1;
}
sum+=solve(r,c);//递归得结果
}
done[i][j]=true;
return (grid[i][j]=sum);
}
int main()
{
int T;
int c,r;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&c,&r);
for(int i=1;i<=r;i++)
for(int j=1;j<=c;j++)
{
scanf("%s",fm[i][j]);
done[i][j]=false;
if(isdigit(fm[i][j][0]))
{
done[i][j]=true;
grid[i][j]=atoi(fm[i][j]);
}
}
for(int i=1;i<=r;i++)
for(int j=1;j<=c;j++)
{
printf("%d%s",solve(i,j),(j==c)?"\n":" ");
}
}
return 0;
}