您的位置:首页 > 其它

UVa 196 Spreadsheet

2013-05-08 22:09 459 查看
/*  思路:
先把函数存起来,之后深度遍历.
*/
#include <cstdio>
#include <cstring>
#include <cctype>
const int ROW = 1000 + 10;
const int COLUMN = 18278 + 10;
const int EXP = 200;  //?
const int FUNCTS = COLUMN;
struct cell {
int val;
bool isVal;
};
cell sheets[ROW][COLUMN];
struct funct{
char exp[EXP];
};
funct functs[FUNCTS];
int cur;
int get_funct() {
return cur++;
}
int r, c;
//获得函数中引用的单元格
bool get_ref(char *&exp, int &ref_r, int &ref_c) {
if(!*exp) return false;
exp++; // jump '=','+'
int sys = 26;
ref_c = 0;
while(isalpha(*exp)) {
ref_c = ref_c*sys + *exp - 'A' + 1;
exp++;
}
sys = 10;
ref_r = 0;
while(isdigit(*exp)) {
ref_r = ref_r*sys + *exp - '0';
exp++;
}
return true;
}

int dfs(int u) {
char *s = functs[u].exp;
int ref_r, ref_c;
int sum = 0;
while(get_ref(s, ref_r, ref_c)) {
//printf(" %d,%d ",ref_r, ref_c);
int v = sheets[ref_r][ref_c].val;
if(sheets[ref_r][ref_c].isVal) {
sum += v;
} else {
//printf("|%d| ", v);
int val = dfs(v);
sheets[ref_r][ref_c].isVal = true;
sheets[ref_r][ref_c].val = val;
sum += val;
}
}
return sum;
}

void print() {
for(int i=1; i<=r; i++) {
for(int j=1; j<=c; j++) {
if(j>1) printf(" ");
int val = sheets[i][j].val;
if(sheets[i][j].isVal) printf("%d", val);
else printf("%d", dfs(val));
}
printf("\n");
}
}

int main ()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif
int T;
scanf("%d", &T);
while(T--) {
cur = 0;
scanf("%d%d", &c, &r);
for(int i=1; i<=r; i++) {
for(int j=1; j<=c; j++) {
if(scanf("%d", &(sheets[i][j].val))==1) {
sheets[i][j].isVal = true;
} else {
int f = get_funct();
scanf("%s", functs[f].exp);
sheets[i][j].isVal = false;
sheets[i][j].val = f;
}
}
}
print();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: