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

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

string str[71][71];

string func(char a[71][71], bool vis[71][71], int w, int h, int p, int q) {
  if (!vis[p][q]) {
    return str[p][q];
  }
  vis[p][q] = false;
  if (p == h - 1 && q == w - 1) {
    str[p][q] = a[p][q];
    return str[p][q];
  }

  string s = "", t = "";
  int u = p + 1, v = q + 1;
  if (u < h && a[u][q] >= '0' && a[u][q] <= '9') {
    s = func(a, vis, w, h, u, q);
  }
  if (v < w && a[p][v] >= '0' && a[p][v] <= '9') {
    t = func(a, vis, w, h, p, v);
  }

  if (s.size() > t.size() || (t.size() == s.size() && t < s)) {
    str[p][q] = a[p][q] + s;
  } else {
    str[p][q] = a[p][q] + t;
  }

  return str[p][q];
}

bool compareTo(string s1, string s2) {
  int p, q;
  string str1, str2;
  p = q = 0;

  while (p < s1.size() && s1[p] == '0') {
    p++;
  }
  while (q < s2.size() && s2[q] == '0') {
    q++;
  }
  str1 = s1.substr(p);
  str2 = s2.substr(q);

  if (str1.size() < str2.size()) {
    return true;
  } else if (str1.size() > str2.size()) {
    return false;
  }

  return ((s1 > s2) ? false : true);
}

int main() {
  int w, h;
  char a[71][71];
  bool vis[71][71];

  while (cin >> w >> h && (w + h)) {
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> a[i][j];
      }
    }
    fill(&vis[0][0], &vis[70][70], true);
    fill(&str[0][0], &str[70][70], "");
    string s = "";

    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (a[i][j] >= '0' && a[i][j] <= '9' && vis[i][j]) {
          string t = func(a, vis, w, h, i, j);
          if (s.size() < t.size() || (s.size() == t.size() && s < t)) {
            s = t;
          }
        }
      }
    }

    s = "";
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (!vis[i][j] && compareTo(s, str[i][j])) {
          s = str[i][j];
        }
      }
    }

    int p = 0;
    while (p < s.size() && s[p] == '0') {
      p++;
    }

    cout << s.substr(p) << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>

using namespace std;

bool func(char w) {
  char num = '0';
  for (int i = 0; i < 10; i++) {
    if (w == (char)(num + i)) {
      return true;
    }
  }

  return false;
}

string stcomp(string s1, string s2) {
  string str = s1;
  if (s1.length() == s2.length()) {
    long long int u = atoi(s1.c_str());
    long long int v = atoi(s2.c_str());
    if (v > u) {
      str = s2;
    }
  } else if (s1.length() < s2.length()) {
    str = s2;
  }

  return str;
}

string search(char a[71][71], int h, int w, int s, int t) {
  string str, str2 = "", str3 = "";
  str = a[s][t];

  if (s + 1 < h) {
    if (func(a[s + 1][t])) {
      str2 = search(a, h, w, s + 1, t);
    }
  }
  if (t + 1 < w) {
    if (func(a[s][t + 1])) {
      str3 = search(a, h, w, s, t + 1);
    }
  }

  return (str + stcomp(str2, str3));
}

int main() {
  int w, h;
  char a[71][71];
  string str, st;

  while (1) {
    cin >> w >> h;
    if (h == 0 && w == 0) {
      break;
    }
    str = "";
    for (int i = 0; i < h; i++) {
      cin >> a[i];
    }

    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (func(a[i][j])) {
          st = search(a, h, w, i, j);
          str = stcomp(str, st);
        }
      }
    }
    w = 0;
    while (str[w] == '0') {
      w++;
    }

    cout << str.substr(w) << endl;
  }

  return 0;
}