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

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

string x[100][100];
bool p[100][100];
int h, w;
char T[11] = "0123456789";
char c;
string dp[100][100];

bool isnum(string p) {
  for (int i = 0; i < 10; i++) {
    if (T[i] == p[0]) {
      return true;
    }
  }
  return false;
}

string _max(string a, string b) {
  if (a.size() < b.size()) {
    return b;
  }
  if (a.size() > b.size()) {
    return a;
  }
  if (a > b) {
    return a;
  }
  return b;
}

int main() {
  while (true) {
    for (int i = 0; i < 100; i++) {
      for (int j = 0; j < 100; j++) {
        dp[i][j] = "";
        x[i][j] = "a";
      }
    }
    cin >> w >> h;
    if (h == 0 && w == 0) {
      break;
    }
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> c;
        string G = "";
        G += c;
        x[i][j] = G;
      }
    }
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (isnum(x[i][j]) == true) {
          if (x[i][j] != "0") {
            dp[i][j] = "" + x[i][j];
          }
          p[i][j] = true;
        } else {
          p[i][j] = false;
        }
      }
    }
    for (int i = 0; i < 70; i++) {
      for (int j = 0; j < h; j++) {
        for (int k = 0; k < w; k++) {
          if (dp[j][k] != "") {
            if (p[j + 1][k] == true) {
              dp[j + 1][k] = _max(dp[j + 1][k], dp[j][k] + x[j + 1][k]);
            }
            if (p[j][k + 1] == true) {
              dp[j][k + 1] = _max(dp[j][k + 1], dp[j][k] + x[j][k + 1]);
            }
          }
        }
      }
    }
    string maxn = "";
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        maxn = _max(maxn, dp[i][j]);
      }
    }
    cout << maxn << endl;
  }
}

Error: TLE

Fixed submission:

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

string x[100][100];
bool p[100][100];
int h, w;
char T[11] = "0123456789";
char c;
string dp[100][100];

bool isnum(string p) {
  for (int i = 0; i < 10; i++) {
    if (T[i] == p[0]) {
      return true;
    }
  }
  return false;
}

string _max(string a, string b) {
  if (a.size() < b.size()) {
    return b;
  }
  if (a.size() > b.size()) {
    return a;
  }
  if (a > b) {
    return a;
  }
  return b;
}

int main() {
  while (true) {
    for (int i = 0; i < 100; i++) {
      for (int j = 0; j < 100; j++) {
        dp[i][j] = "";
        x[i][j] = "a";
      }
    }
    cin >> h >> w;
    if (h == 0 && w == 0) {
      break;
    }
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> c;
        string G = "";
        G += c;
        x[i][j] = G;
      }
    }
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (isnum(x[i][j]) == true) {
          dp[i][j] = "" + x[i][j];
          p[i][j] = true;
        } else {
          p[i][j] = false;
        }
      }
    }
    for (int i = 0; i < 100; i++) {
      for (int j = 0; j < h; j++) {
        for (int k = 0; k < w; k++) {
          if (p[j + 1][k] == true) {
            dp[j + 1][k] = _max(dp[j + 1][k], dp[j][k] + x[j + 1][k]);
          }
          if (p[j][k + 1] == true) {
            dp[j][k + 1] = _max(dp[j][k + 1], dp[j][k] + x[j][k + 1]);
          }
        }
      }
    }
    string maxn = "";
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        maxn = max(maxn, dp[i][j]);
      }
    }
    cout << maxn << endl;
  }
}