Problem p00707 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string>
#include <vector>
using namespace std;
/*
string solve(vector<string> &C, int W, int H, int i, int j, string ret){
  string s[2];
  ret += C[i][j];
  for(int k = 1; k < 3; ++k){
    if(i+k/2 < H && j+k%2 < W && C[i+k/2][j+k%2] <= '9' && C[i+k/2][j+k%2] >=
'0'){ s[k-1] = solve(C,W,H,i+k/2,j+k%2,"");
    }
  }
  if(s[0].size() == s[1].size()){
    ret += max(s[0],s[1]);
  }else{
    if(s[0].size() > s[1].size()) ret += s[0];
    else ret += s[1];
  }
  return ret;
}
*/
int main() {
  int W, H;
  while (cin >> W >> H, W) {
    vector<string> C(H);
    for (int i = 0; i < H; ++i)
      cin >> C[i];
    /*
    string ans;
    for(int i = 0; i < H; ++i){
      if((int)ans.size() > H - i + W) break;
      for(int j = 0; j < W; ++j){
        if(C[i][j] > '9' || C[i][j] <= '0' ||
           (i > 0 && C[i-1][j] > '0' && C[i-1][j] <= '9') ||
           (j > 0 && C[i][j-1] > '0' && C[i][j-1] <= '9'))
          continue;
        if((int)ans.size() > H - i + W - j) break;
        else{
          string t = solve(C,W,H,i,j,"");
          if(ans.size() == t.size()) ans = max(ans, t);
          else if(ans.size() < t.size()) ans = t;
        }
      }
    }
    if(ans == "") cout << 0 << endl;
    else cout << ans << endl;
    */
    string dp[H][W];
    for (int i = 0; i < H; ++i) {
      for (int j = 0; j < W; ++j) {
        if (C[i][j] < '0' || C[i][j] > '9')
          continue;
        string s = string(1, C[i][j]), t = string(1, C[i][j]);
        if (i > 0 && dp[i - 1][j] != "0")
          s = dp[i - 1][j] + C[i][j];
        if (j > 0 && dp[i][j - 1] != "0")
          t = dp[i][j - 1] + C[i][j];
        if (s.size() == t.size())
          dp[i][j] = max(s, t);
        else {
          if (s.size() > t.size())
            dp[i][j] = s;
          else
            dp[i][j] = t;
        }
      }
    }
    string ans = "";
    for (int i = 0; i < H; ++i) {
      for (int j = 0; j < W; ++j) {
        if (ans.size() == dp[i][j].size())
          ans = max(ans, dp[i][j]);
        else {
          if (ans.size() < dp[i][j].size())
            ans = dp[i][j];
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
#include <vector>
using namespace std;

string solve(vector<string> &C, int W, int H, int i, int j, string ret) {
  string s[2];
  ret += C[i][j];
  for (int k = 1; k < 3; ++k) {
    if (i + k / 2 < H && j + k % 2 < W && C[i + k / 2][j + k % 2] <= '9' &&
        C[i + k / 2][j + k % 2] >= '0') {
      s[k - 1] = solve(C, W, H, i + k / 2, j + k % 2, "");
    }
  }
  if (s[0].size() == s[1].size()) {
    ret += max(s[0], s[1]);
  } else {
    if (s[0].size() > s[1].size())
      ret += s[0];
    else
      ret += s[1];
  }
  return ret;
}

int main() {
  int W, H;
  while (cin >> W >> H, W) {
    vector<string> C(H);
    for (int i = 0; i < H; ++i)
      cin >> C[i];
    string ans;
    for (int i = 0; i < H; ++i) {
      for (int j = 0; j < W; ++j) {
        if (C[i][j] > '9' || C[i][j] <= '0' ||
            (i > 0 && C[i - 1][j] > '0' && C[i - 1][j] <= '9') ||
            (j > 0 && C[i][j - 1] > '0' && C[i][j - 1] <= '9'))
          continue;
        else {
          string t = solve(C, W, H, i, j, "");
          if (ans.size() == t.size())
            ans = max(ans, t);
          else if (ans.size() < t.size())
            ans = t;
        }
      }
    }
    if (ans == "")
      cout << 0 << endl;
    else
      cout << ans << endl;
  }
  return 0;
}