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; }
相关文章推荐
- uva 196 - Spreadsheet
- UVA 196 Spreadsheet
- uva196 Spreadsheet
- UVA 196 Spreadsheet 解题报告
- UVA 196 - Spreadsheet
- uva196 - Spreadsheet(电子表格)
- uva 196 Spreadsheet(拓扑排序+DFS)
- UVA 196 & POJ 1420 Spreadsheet (DFS)
- uva 196 Spreadsheet(电子表格)
- uva 196 - Spreadsheet
- UVa 196 - Spreadsheet
- UVA196-Spreadsheet(拓扑排序)
- UVA - 196 Spreadsheet
- uva 196 Spreadsheet
- UVa 196 Spreadsheet
- UVA_196_Spreadsheet
- uva 196 - Spreadsheet
- uva 196 Spreadsheet 拓扑排序 。坑爹的一题
- 【uva-196】Spreadsheet (拓扑排序)妈呀居然过了
- 紫书章四 Spreadsheet Tracking UVA - 512(单元格去哪了,模拟结果更简单))