24h購物| | PChome| 登入
2013-07-22 19:29:32| 人氣1,167| 回應0 | 上一篇 | 下一篇

[UVA] 196 - Spreadsheet

推薦 0 收藏 0 轉貼0 訂閱站台


 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

題目描述:

給定表格的內容或者是運算式,計算最後的表格的每個值,保證通通都有解。

題目解法:

題目其實不難,但是數據大小非常可怕!

其次麻煩是 column 的轉換,A-Z, AA-ZZ, AAA-ZZZ,看成 26 進制數時,A->0, B->1 ...,
但因為這麼計算 AA = A,因此在補上 26,同理 AAA = A,也補上 26*26+26。

再來是使用遞迴呼叫求解,我相信題目不會惡整我,導致 stack overflow,因此就這麼挑戰看看。

否則要使用類似拓樸排序去計算之。

#include <stdio.h>
#include <vector>
using namespace std;
vector<int> g[1048576];
int v[1048576], sol[1048576], col, row;
int dfs(int idx) {
if(sol[idx])
return v[idx];
int sum = 0;
sol[idx] = 1;
for(vector<int>::iterator it = g[idx].begin();
it != g[idx].end(); it++)
sum += dfs(*it);
v[idx] = sum;
return sum;
}
int main() {
int testcase;
char s[65536];
scanf("%d", &testcase);
while(testcase--) {
scanf("%d %d", &col, &row);
int vv = col*row;
int i, j, k;
for(i = 0; i < vv; i++) {
scanf("%s", s);
g[i].clear();
v[i] = 0;
sol[i] = 0;
if(s[0] == '=') {
int rr, cc, base;
for(j = 1; s[j]; j++) {
rr = cc = 0;
base = 0;
while(s[j] >= 'A')
cc = cc*26 + s[j]-'A', j++, base++;
while(s[j] >= '0' && s[j] <= '9')
rr = rr*10 + s[j]-'0', j++;
rr--;
if(base == 2) cc += 26;
else if(base == 3) cc += 26*26 + 26;
g[i].push_back(rr*col+cc);
if(s[j] == '\0') break;
}
} else {
sscanf(s, "%d", &v[i]);
sol[i] = 1;
}
}
for(i = 0; i < row; i++) {
for(j = 0; j < col; j++) {
if(j) putchar(' ');
printf("%d", dfs(i*col+j));
}
puts("");
}
}
return 0;
}

台長: Morris
人氣(1,167) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][dp] 10086 - Test the Rods
此分類上一篇:[UVA] 1530 - Floating Point Numbers

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文