Thông tin
include<bits/stdc++.h>
using namespace std;
int m,n; string BCC = "0ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int vtdp(string s) { int countSM = 0, countP = 0; for (int i = 0; i < s.length(); i++) { if (s[i] == 'S' || (s[i] == 'M' && s[i+1] == 'A')) countSM++; if (s[i] == ',') countP++; if (countSM == countP) return i; } }
int SUM1(string a, string b, vector<vector<int>>& E) { //a1 cot j, a2 hang i int a1 = BCC.find(a[0]); int a2 = stoi(a.substr(1,1)); int b1 = BCC.find(b[0]); int b2 = stoi(b.substr(1,1)); return E[a2][a1] + E[b2][b1];; }
int MAX1(string a, string b, vector<vector<int>>& E) { int a1 = BCC.find(a[0]); int a2 = stoi(a.substr(1,1)); int b1 = BCC.find(b[0]); int b2 = stoi(b.substr(1,1)); return max(E[a2][a1],E[b2][b1]); }
int XULI2(string s,vector<vector<int>>& E) { int a1 = 0,a2 = 0; if (s[0] == 'S') { int vtp = vtdp(s); if (s[4] != 'S' && s[4] != 'M' && s[vtp+1] != 'S' && s[vtp+1] != 'M') { return SUM1(s.substr(4,2),s.substr(7,2),E); } //TH1 if (s[4] == 'S' || s[4] == 'M') { a1 = XULI(s.substr(4,vtp),E); } else if (s[4] != 'S' && s[4] != 'M') { a1 = E[stoi(s.substr(5,1))][BCC.find(s[4])]; } //TH2 if (s[vtp+1] == 'S' || s[vtp+1] == 'M') { a2 = XULI(s.substr(vtp+1,s.length()),E); cout << s.substr(vtp+1,s.length()) << endl; } else if (s[vtp+1] != 'S' && s[vtp+1] != 'M') { a2 = E[stoi(s.substr(vtp+2,1))][BCC.find(s[vtp+1])]; } return a1 + a2; } else if (s[0] == 'M') { int vtp = (3+s.length()) / 2; if (s[4] != 'S' && s[4] != 'M' && s[vtp+1] != 'S' && s[vtp+1] != 'M') { return MAX1(s.substr(4,2),s.substr(7,2),E); } //TH1 if (s[4] == 'S' || s[4] == 'M') { a1 = XULI(s.substr(4,vtp),E); } else if (s[4] != 'S' && s[4] != 'M') { a1 = E[stoi(s.substr(5,1))][BCC.find(s[4])]; } //TH2 if (s[vtp+1] == 'S' || s[vtp+1] == 'M') { a2 = XULI(s.substr(vtp+1,s.length()),E); } else if (s[vtp+1] != 'S' && s[vtp+1] != 'M') { a2 = E[stoi(s.substr(vtp+2,1))][BCC.find(s[vtp+1])]; } return max(a1,a2); } }
int main() { cin >> m >> n; vector<vector<int>> E(m+1,vector<int>(n+1)); // i hang (123), j cot (ABC) for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { cin >> E[i][j]; } } int Q; cin >> Q; vector<string> QC(Q); for (int i = 0; i < Q; i++) { cin >> QC[i]; cout << XULI(QC[i],E); } }