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 |
#include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <cctype> #include <string> #include <cmath> #include <sstream> using namespace std; string str[1000][18279]; int val[1000][18279]; bool isDone[1000][18279]; int C,R; int solve(int a,int b) { if(isDone[a][b]) return val[a][b]; string tmpstr; for(int i=0;i<str[a][b].size();i++) { if(str[a][b][i]=='=' || str[a][b][i]=='+') { str[a][b][i]=' '; } } stringstream ss; ss<<str[a][b]; int sum = 0; tmpstr=""; while(ss>>tmpstr) { int r = 0; int c = 0; for(int j=0;j<tmpstr.size();j++) { if(tmpstr[j]>='0' && tmpstr[j]<='9') { r=r*10+(tmpstr[j]-'0'); } else { c = c*26+(tmpstr[j]-'A'+1); } } int tt = solve(r,c); sum+=tt; } isDone[a][b]=1; return (val[a][b]=sum); } int toint(string st) { int sum = 0; for(int i=0;i<st.size();i++) { sum = sum*10+(st[i]-'0'); } return sum; } int main() { int t; cin>>t; while(t--) { cin>>C>>R; for(int i=1;i<=R;i++) { for(int j=1;j<=C;j++) { cin>>str[i][j]; isDone[i][j]=0; if(str[i][j][0]>='0' && str[i][j][0]<='9') { val[i][j]=toint(str[i][j]); isDone[i][j]=1; } } } for(int i=1;i<=R;i++) for(int j=1;j<=C;j++) cout<<solve(i,j)<<(j==C?"\n":" "); } return 0; } |