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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
char s[80][80];
pair<string, int> t[80][80];
int operator<(pair<string, int> &s, pair<string, int> &t) {
  if (0) {
  } else if (s.second < t.second) {
    return 1;
  } else if (s.second > t.second) {
    return 0;
  } else {
    return s.first < t.first;
  }
}
pair<string, int> fi(int x, int y) {
  if (t[y][x].second == 0) {
    if ('0' <= s[y][x + 1] && s[y][x + 1] <= '9') {
      pair<string, int> u = fi(x + 1, y);
      if (t[y][x] < u)
        t[y][x] = u;
    }
    if ('0' <= s[y + 1][x] && s[y + 1][x] <= '9') {
      pair<string, int> u = fi(x, y + 1);
      if (t[y][x] < u)
        t[y][x] = u;
    }
    t[y][x] = make_pair(s[y][x] + t[y][x].first, t[y][x].second + 1);
  }
  return t[y][x];
}
int main() {
  int i, j;
  int w, h;
  while (cin >> w >> h, w || h) {
    fill(s[0], s[0] + 80 * 80, 0);
    for (i = 0; i < h; ++i)
      cin >> s[i];
    fill(t[0], t[0] + 80 * 80, make_pair("", 0));
    pair<string, int> mx = make_pair("", 0);
    for (i = 0; i < h; ++i) {
      for (j = 0; j < w; ++j) {
        if ('1' <= s[i][j] && s[i][j] <= '9') {
          pair<string, int> u = fi(j, i);
          if (mx < u)
            mx = u;
        }
      }
    }
    cout << mx.first << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int a[80][80], b[80][80][80], c[80][80], d[80];
int mx[80];
int ln;
void fn(int p, int q, int r) {
  int i;
  if (a[q][r + 1] < 0 && a[q + 1][r] < 0) {
    if (ln == p) {
      for (i = 0; i < p; ++i) {
        if (0) {
        } else if (mx[i] < d[i]) {
          break;
        } else if (mx[i] > d[i]) {
          i = p;
          break;
        }
      }
    }
    if (ln < p || (ln == p && i < p)) {
      memcpy(mx, d, p * sizeof(int));
      ln = p;
    }
  } else {
    if (c[q][r] == p) {
      for (i = 0; i < p; ++i) {
        if (0) {
        } else if (b[q][r][i] < d[i]) {
          break;
        } else if (b[q][r][i] > d[i]) {
          i = p;
          break;
        }
      }
    }
    if (c[q][r] < p || (c[q][r] == p && i < p)) {
      memcpy(b[p][q], d, p * sizeof(int));
      c[p][q] = p;
      if (a[q][r + 1] >= 0) {
        d[p] = a[q][r + 1];
        fn(p + 1, q, r + 1);
      }
      if (a[q + 1][r] >= 0) {
        d[p] = a[q + 1][r];
        fn(p + 1, q + 1, r);
      }
    }
  }
  return;
}
int main() {
  int i, j;
  int w, h;
  while (cin >> w >> h, w || h) {
    memset(a, -1, sizeof(a));
    for (i = 0; i < h; ++i) {
      for (j = 0; j < w; ++j) {
        char s;
        cin >> s;
        if ('0' <= s && s <= '9')
          a[i][j] = s - '0';
      }
    }
    memset(c, 0, sizeof(c));
    mx[0] = 0;
    ln = 1;
    for (i = 0; i < h; ++i) {
      for (j = 0; j < w; ++j) {
        if (a[i][j] > 0) {
          d[0] = a[i][j];
          fn(1, i, j);
        }
      }
    }
    for (i = 0; i < ln; ++i)
      cout << mx[i];
    cout << endl;
  }
  return 0;
}