Spreadsheet Calculator 电子表格计算器 (Uva 215)

 

原题:https://uva.onlinejudge.org/external/2/215.pdf

有一个M x N的表格,每个单元格是个数字或者表达式。表达式由单元格编号和+ - 号组成

输出单元格的结果

 

思路:用dfs判断有向图环的问题

 

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <utility>
 5 #include <cstring>
 6 #include <sstream>
 7 
 8 using namespace std;
 9 const int MAXR = 20 + 2, MAXC = 10 + 2;
10 string sheet[MAXR][MAXC];
11 int r, c;
12 bool hasCircle[MAXR][MAXC], f[MAXR][MAXC], ok;
13 
14 bool isNumber(int x, int y) {
15     string s = sheet[x][y];
16     int l = s.size();
17     if (isalpha(s[0])) return false;
18     for (int i = 0; i < l; i++) {
19         if (i != 0 && !isdigit(s[i])) return false;
20     }
21     return true;
22 }
23 
24 void dfs(int x, int y) {
25     if (hasCircle[x][y] || isNumber(x, y)) return;
26     if (f[x][y]) {hasCircle[x][y] = 1; return; }
27     f[x][y] = 1;
28     string s = sheet[x][y];
29     char sign; int ans = 0, right, len = sheet[x][y].size(), i = 0;
30     while (i < len) {
31         sign = '+';
32         if (s[i] == '-' || s[i] == '+') {
33             sign = s[i];
34             i++;
35         }
36         if (isalpha(s[i])) {
37             int nx = s[i] - 'A', ny = s[i + 1] - '0';
38             i += 2;
39             dfs(nx, ny);
40             if (hasCircle[nx][ny]) {
41                 hasCircle[x][y] = 1;
42                 return;
43             }
44             right = stoi(sheet[nx][ny]);
45         }
46         else {
47             int j = i + 1;
48             while (j < len && isdigit(s[j])) j++;
49             right = stoi(s.substr(i, j - i));
50             i = j;
51         }
52         ans += right * ((sign == '+') ? 1 : -1);
53     }
54     stringstream ss; ss << ans;
55     ss >> sheet[x][y];
56 }
57 
58 int main() {
59     freopen("in", "r", stdin);
60     //freopen("out", "w", stdout);
61     while (scanf("%d%d", &r, &c) == 2 && r) {
62         memset(f, 0, sizeof(f));
63         memset(hasCircle, 0, sizeof(hasCircle));
64         ok = 1;
65         for (int i = 0; i < r; i++)
66             for (int j = 0; j < c; j++) 
67                 cin >> sheet[i][j];
68             
69         for (int i = 0; i < r; i++)
70             for (int j = 0; j < c; j++) {
71                 if (!f[i][j])
72                     dfs(i, j);
73                 if (hasCircle[i][j]) ok = 0;
74             }
75         if (!ok) {
76             for (int i = 0; i < r; i++)
77                 for (int j = 0; j < c; j++)
78                     if (hasCircle[i][j]) cout << char(i + 'A') << j << ": " << sheet[i][j] << endl;
79         }
80         else {
81             putchar(' ');
82             for (int i = 0; i < c; i++) printf("     %d", i);
83             putchar('\n');
84             for (int i = 0; i < r; i++) {
85                 printf("%c", i + 'A');
86                 for (int j = 0; j < c; j++) {
87                     for (int k = 0; k < 6 - sheet[i][j].size(); k++) putchar(' ');
88                     cout << sheet[i][j];
89                 }
90                 putchar('\n');
91             }
92         }
93         putchar('\n');
94     }
95     
96     return 0;
97 }

 

posted @ 2015-11-05 04:04  BowenCCC~  阅读(449)  评论(0编辑  收藏  举报