#include #include #include #include #include using namespace std; #define UNKNOWN (-2147483647) #define ENDFACT (2147483647) class state_t { private: struct elem_t { int term, expr, op; elem_t(int t, int e, int o) { term = t; expr = e; op = o; } }; private: string _s; int _fact, _term, _expr, _op, _lhs; stack _stack; bool _isvalid, _isequal; public: state_t(void) { _lhs = UNKNOWN; _expr = 0; _term = 1; _fact = UNKNOWN; _op = 1; _isvalid = false; _isequal = false; } private: int get_term(void) { return _fact == ENDFACT ? _term : (_term * _fact); } int get_expr(void) { return _expr + _op * get_term(); } public: bool append(char c) { if(_isvalid) return false; if(isdigit(c)) { if(_fact == 0 || _fact >= 1000) return false; if(_fact == UNKNOWN) _fact = 0; _fact = _fact * 10 + (c - '0'); _s += string(1, c); return true; } if(c == '+' || c == '-') { if(_fact == UNKNOWN) return false; _expr = get_expr(); _term = 1; _fact = UNKNOWN; _op = (int)(',' - c); // yes, this is very tricky _s += string(1, c); return true; } if(c == '*') { if(_fact == UNKNOWN) return false; _term = get_term(); _fact = UNKNOWN; _s += string(1, c); return true; } if(c == '(') { if(_fact != UNKNOWN) return false; _stack.push(elem_t(_term, _expr, _op)); _expr = 0; _term = 1; _op = 1; _s += string(1, c); return true; } if(c == ')') { if(_fact == UNKNOWN || _stack.empty()) return false; elem_t e = _stack.top(); _stack.pop(); _term = e.term * get_expr(); _fact = ENDFACT; _expr = e.expr; _op = e.op; _s += string(1, c); return true; } if(c == '=') { if(_fact == UNKNOWN) return false; if(!_stack.empty() || _lhs != UNKNOWN) return false; _lhs = get_expr(); _expr = 0; _term = 1; _fact = UNKNOWN; _op = 1; _s += string(1, c); return true; } if(c == '$') { if(_fact == UNKNOWN) return false; if(!_stack.empty() || _lhs == UNKNOWN) return false; _isvalid = true; _isequal = (_lhs == get_expr()); return true; } return false; } bool isequal(void) { return _isequal; } bool isvalid(void) { return _isvalid; } string tostring(void) { return _s; } }; static char sym[10][10]; static int wd, ht; static set A; void search(int x, int y, state_t state = state_t()) { if(!state.append(sym[x][y])) return; if(state.isvalid()) { if(!state.isequal()) return; string s = state.tostring(); if(A.insert(s).second) cout << s << endl; return; } sym[x][y] ^= 0x80; if((sym[x-1][y] & 0x80) == 0) search(x - 1, y, state); if((sym[x+1][y] & 0x80) == 0) search(x + 1, y, state); if((sym[x][y-1] & 0x80) == 0) search(x, y - 1, state); if((sym[x][y+1] & 0x80) == 0) search(x, y + 1, state); sym[x][y] ^= 0x80; } int main(void) { ifstream cin("equation.txt"); int t = 0; while(cin >> wd >> ht) { if(wd == 0 && ht == 0) break; for(int y = 0; y <= ht + 1; y++) { sym[0][y] = 0x80; sym[wd + 1][y] = 0x80; } for(int x = 1; x <= wd; x++) { sym[x][0] = 0x80; sym[x][ht + 1] = '$'; } for(int y = 1; y <= ht; y++) { for(int x = 1; x <= wd; x++) { cin >> sym[x][y]; } } cout << "Table " << (++t) << endl; A.clear(); for(int x = 1; x <= wd; x++) { search(x, 1); } if(A.empty()) cout << "No Solution" << endl; cout << endl; } return 0; }