题解:题意是将给出的表格内是表达式的都将值最后输出出来,因此要注意计算表达式的优先顺序,方法深搜,找到直到不是表达式而是数值的就返回这个值。带字母的是26进制数。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
const int N = 1005;
const int INF = 0x3f3f3f3f;
int row, col, g[N][N], n;
string s[N][N];
void init() {
memset(g, 0, sizeof(g));
n = 0;
}
int dfs(int x, int y) {
if (g[x][y] != INF)
return g[x][y];
string str = s[x][y];
int len = str.size();
int temp = 0, x1, y1, val = 0;
for (int i = 1; i < len; i++) {
if (isalpha(str[i])) {
for (; i < len && isalpha(str[i]); i++)
temp = temp * 26 + str[i] - 'A' + 1;
y1 = temp;
temp = 0;
}
if (isdigit(str[i])) {
for (; i < len && isdigit(str[i]); i++)
temp = temp * 10 + str[i] - '0';
x1 = temp;
temp = 0;
}
if (str[i] == '+') {
val += dfs(x1, y1);
}
if (i == len) {
val += dfs(x1, y1);
g[x][y] = val;
val = 0;
}
}
return g[x][y];
}
int main() {
int t;
string str;
scanf("%d", &t);
while (t--) {
scanf("%d%d", &col, &row);
getchar();
init();
for (int i = 1; i <= row; i++)
for (int j = 1; j <= col; j++) {
cin >> str;
if (str[0] == '=') {
g[i][j] = INF;
s[i][j] = str;
}
else
g[i][j] = atoi(str.c_str());
}
for (int i = 1; i <= row; i++)
for (int j = 1; j <= col; j++)
if (g[i][j] == INF)
dfs(i, j);
for (int i = 1; i <= row; i++) {
for (int j = 1; j <= col - 1; j++)
printf("%d ", g[i][j]);
printf("%d\n", g[i][col]);
}
}
return 0;
}