#include <iostream>
#include <cstring>
#include <queue>
#include <set>
#include <vector>
#include <cmath>
#include <stack>
#include <string>
#include <queue>
using namespace std;
#define INF 0x3f3f3f3f
int m, n;
int flag[25][15];//判定是否死锁 0表示已解锁,1表示待解锁,2表示死锁
char str[25][15][80];
int num[25][15];
bool f;
void dfs(int x, int y) {
flag[x][y] = 2;
int s = 0, op = 1, x0 = -1, y0 = -1;
for (int i = 0; str[x][y][i] != '\0'; ++i) {
char ch = str[x][y][i];
if (ch >= 'A' && ch <= 'Z') {
x0 = ch - 'A';
}
else if (ch >= '0' && ch <= '9') {
if (x0 == -1) {
if (y0 == -1) {
y0 = 0;
}
y0 = y0 * 10 + ch - '0';
if (!(str[x][y][i + 1] >= '0' && str[x][y][i + 1] <= '9') || str[x][y][i + 1] == '\0') {
s = s + y0 * op;
y0 = -1;
}
continue;
}
y0 = ch - '0';
if (flag[x0][y0] == 2) {
f = true;
return;
}
else if (flag[x0][y0] == 1) {
dfs(x0, y0);
}
if (flag[x0][y0] == 0) {
s = s + op*num[x0][y0];
}
else {
f = true;
return;
}
x0 = -1, y0 = -1;
}
else {
if (ch == '+') {
op = 1;
}
else {
op = -1;
}
}
}
num[x][y] = s;
flag[x][y] = 0;
}
int main() {
while (~scanf("%d%d", &m, &n)) {
if (m == 0 && n == 0) {
break;
}
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
scanf("%s", str[i][j]);
if ((str[i][j][0] >= '0' && str[i][j][0] <= '9') || str[i][j][0] == '+' || str[i][j][0] == '-') {
int k = 0, op = 1;
if (str[i][j][0] == '-') {
op = -1;
k = 1;
}
else if (str[i][j][0] == '+') {
k = 1;
}
flag[i][j] = 0;
int s = 0;
for (; str[i][j][k] != '\0'; ++k) {
s = s * 10 + str[i][j][k] - '0';
}
s *= op;
num[i][j] = s;
}
else {
flag[i][j] = 1;
}
}
}
f = false;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (flag[i][j] == 1) {
dfs(i, j);
}
}
}
if (f) {
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (flag[i][j] == 2) {
printf("%c%c: %s\n", i + 'A', j + '0', str[i][j]);
}
}
}
}
else {
putchar(' ');
for (int i = 0; i < n; ++i) {
printf("%6d", i);
}
for (int i = 0; i < m; ++i) {
printf("\n%c", i + 'A');
for (int j = 0; j < n; ++j) {
printf("%6d", num[i][j]);
}
}
putchar('\n');
}
putchar('\n');
}
return 0;
}
细节太多了,表达式中可能含数字,输入的可能有负数,打印时右对齐6位,其他没什么太多难的,另外是标记当前表达式是否已解锁为数字,标记其是否已死锁状态或是待解锁状态。