196 – SpreadsheetTime limit: 3.000 seconds |
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.
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 2 3 4 5 |
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
1 2 3 |
10 34 37 81 40 17 34 91 50 51 71 172 |
最后一道拓扑排序了,也是Volume 2最后一道题了。。。
可是难点跟拓扑排序毛线的关系都没有。。。
这道题关键就在于数值范围太大,1000*20000, 邻接矩阵完全carry不起来,所以就得用一维数组来表示矩阵以降低时间复杂度了。
大概思路就是用数组和map互相映射构成图,再套个拓扑排序模板就行了。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
/******************************************************** * File Name: uvaoj196.cpp * Author: razrLeLe * Mail: razrlele@gmail.com * Homepage: https://yueyu.io * Created Time: Sat 21 Feb 2015 09:41:27 AM CST ******************************************************/ #include "vector" #include "set" #include "deque" #include "queue" #include "algorithm" #include "functional" #include "iostream" #include "cstdio" #include "cmath" #include "cstdlib" #include "string" #include "cstring" #include "string.h" #include "map" #include "cctype" #include "list" #include "stack" using namespace std; #define INF 0x3f3f3f3f #define LOCAL int row, col; struct node { int x, y; int place; }; struct chart { vector V; int x, y; }; int grid[1000][20000]; const int maxn = 1000*20000; map<int, chart> m1; int cnt[maxn]; int topo[maxn], t; int table[maxn]; int cnum; int get_C(string in) { node tmp; int len = in.length(); int i; string c="", n=""; for(i = 0; i < len; i++) { if(in[i] < '0' || in[i] > '9') c = c+in[i]; else n = n+in[i]; } tmp.x = atoi(n.c_str())-1; tmp.y = 0; len = c.length(); for(i = 0; i < len; i++) { tmp.y += pow(26, len-1-i)*(c[i]-'A'+1); } tmp.y--; return tmp.x*col+tmp.y; } void transform(int a, int b, string in) { if(in[0] != '=') grid[a][b] = atoi(in.c_str()); else { chart tmp; tmp.x = a; tmp.y = b; int len = in.length(); string tmp0 = ""; for(int i = 1; i < len; i++) { if(in[i] == '+') { tmp.V.push_back(get_C(tmp0));//存入绝对一维坐标 tmp0 = ""; i++; } tmp0 = tmp0+in[i]; } tmp.V.push_back(get_C(tmp0)); m1[cnum] = tmp; table[a*col+b] = cnum; cnum++; } return ; } void DFS(int i) { cnt[i] = 1; chart tmp = m1[i]; int tmpx, tmpy, next; for(vector::iterator p = tmp.V.begin(); p != tmp.V.end(); p++) { tmpx = (*p)/col; tmpy = (*p)%col; if(table[*p] >= 0) { next = table[*p]; if(cnt[next] < 0) DFS(next); } } topo[t++] = i; return ; } void toposort() { t = 0; memset(cnt, -1, sizeof(cnt)); for(int i = 0; i < cnum; i++) { if(cnt[i] < 0) DFS(i); } } void find(int u) { chart tmp = m1[topo[u]]; int x = tmp.x; int y = tmp.y; int tmpx, tmpy; grid[x][y] = 0; for(vector::iterator i = tmp.V.begin(); i != tmp.V.end(); i++) { tmpx = (*i)/col; tmpy = (*i)%col; grid[x][y] += grid[tmpx][tmpy]; } return ; } int main() { #ifdef LOCAL freopen("/home/razrlele/build/data.txt", "r", stdin); freopen("/home/razrlele/build/out.txt", "w", stdout); #endif string in; int numcase; cin >> numcase; while(numcase--) { cin >> col >> row; cnum = 0; memset(grid, -1, sizeof(grid)); memset(table, -1, sizeof(table)); for(int i = 0; i < row; i++) for(int j = 0; j < col; j++) { cin >> in; transform(i, j, in); } toposort(); for(int i = 0; i < cnum; i++) { find(i); } for(int i = 0; i < row; i++) { for(int j = 0; j < col; j++) { if(j) cout << " "; cout << grid[i][j]; } cout << endl; } } return 0; } |