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

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define repq(i, a, n) for (int(i) = (a); (i) <= (n); (i)++)
#define repr(i, a, n) for (int(i) = (a); (i) >= (n); (i)--)
#define all(v) begin(v), end(v)
#define pb(a) push_back(a)
#define fr first
#define sc second
#define INF 2000000000
#define int long long int

#define X real()
#define Y imag()
#define EPS (1e-10)
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).X, (b).X) && EQ((a).Y, (b).Y))
#define LE(n, m) ((n) < (m) + EPS)
#define LEQ(n, m) ((n) <= (m) + EPS)
#define GE(n, m) ((n) + EPS > (m))
#define GEQ(n, m) ((n) + EPS >= (m))

typedef vector<int> VI;
typedef vector<VI> MAT;
typedef pair<int, int> pii;
typedef long long ll;

typedef complex<double> P;
typedef pair<P, P> L;
typedef pair<P, double> C;

int dx[] = {1, 0};
int dy[] = {0, 1};
int const MOD = 1000000007;
ll mod_pow(ll x, ll n) {
  return (!n) ? 1 : (mod_pow((x * x) % MOD, n / 2) * ((n & 1) ? x : 1)) % MOD;
}
int madd(int a, int b) { return (a + b) % MOD; }
int msub(int a, int b) { return (a - b + MOD) % MOD; }
int mmul(int a, int b) { return (a * b) % MOD; }
int minv(int a) { return mod_pow(a, MOD - 2); }
int mdiv(int a, int b) { return mmul(a, minv(b)); }

namespace std {
bool operator<(const P &a, const P &b) {
  return a.X != b.X ? a.X < b.X : a.Y < b.Y;
}
} // namespace std

int w, h;
char board[110][110];
string dp[110][110];

string comp(string s1, string s2) {
  if (s1.length() == s2.length())
    return max(s1, s2);
  else if (s1.length() > s2.length())
    return s1;
  else
    return s2;
}

signed main() {
  while (cin >> w >> h, w || h) {
    repq(i, 0, h) repq(j, 0, w) dp[i][j] = "";
    repq(i, 1, h) repq(j, 1, w) cin >> board[i][j];
    string ans = "";
    repq(i, 1, h) repq(j, 1, w) {
      if ('A' <= board[i][j] && board[i][j] <= 'Z')
        dp[i][j] = "";
      else {
        string s1 = dp[i - 1][j], s2 = dp[i][j - 1];
        string ma = comp(s1, s2);
        // cout << "s1 = " << s1 << ", s2 = " << s2 << ", ma = " << ma << endl;
        if (ma == "") {
          if (board[i][j] != '0')
            dp[i][j] += board[i][j];
        } else
          dp[i][j] = ma + board[i][j];
      }
      ans = comp(ans, dp[i][j]);
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define repq(i, a, n) for (int(i) = (a); (i) <= (n); (i)++)
#define repr(i, a, n) for (int(i) = (a); (i) >= (n); (i)--)
#define all(v) begin(v), end(v)
#define pb(a) push_back(a)
#define fr first
#define sc second
#define INF 2000000000
#define int long long int

#define X real()
#define Y imag()
#define EPS (1e-10)
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).X, (b).X) && EQ((a).Y, (b).Y))
#define LE(n, m) ((n) < (m) + EPS)
#define LEQ(n, m) ((n) <= (m) + EPS)
#define GE(n, m) ((n) + EPS > (m))
#define GEQ(n, m) ((n) + EPS >= (m))

typedef vector<int> VI;
typedef vector<VI> MAT;
typedef pair<int, int> pii;
typedef long long ll;

typedef complex<double> P;
typedef pair<P, P> L;
typedef pair<P, double> C;

int dx[] = {1, 0};
int dy[] = {0, 1};
int const MOD = 1000000007;
ll mod_pow(ll x, ll n) {
  return (!n) ? 1 : (mod_pow((x * x) % MOD, n / 2) * ((n & 1) ? x : 1)) % MOD;
}
int madd(int a, int b) { return (a + b) % MOD; }
int msub(int a, int b) { return (a - b + MOD) % MOD; }
int mmul(int a, int b) { return (a * b) % MOD; }
int minv(int a) { return mod_pow(a, MOD - 2); }
int mdiv(int a, int b) { return mmul(a, minv(b)); }

namespace std {
bool operator<(const P &a, const P &b) {
  return a.X != b.X ? a.X < b.X : a.Y < b.Y;
}
} // namespace std

int w, h;
char board[110][110];
string ans;

void dfs(int x, int y, string cur, vector<vector<bool>> used) {
  bool ok = false;
  cur += board[x][y];
  // printf("x = %lld, y = %lld\\n", x, y);
  rep(i, 0, 2) {
    int nx = x + dx[i], ny = y + dy[i];
    if (nx < 0 || nx >= h || ny < 0 || ny >= w)
      continue;
    if ('A' <= board[nx][ny] && board[nx][ny] <= 'Z')
      continue;
    if (used[nx][ny])
      continue;
    used[nx][ny] = true;
    ok = true;
    dfs(nx, ny, cur, used);
    used[nx][ny] = false;
  }
  if (!ok) {
    if (ans.length() < cur.length())
      ans = cur;
    else if (ans.length() == cur.length()) {
      if (ans < cur)
        ans = cur;
    }
  }
}

signed main() {
  while (cin >> w >> h, w || h) {
    rep(i, 0, h) rep(j, 0, w) cin >> board[i][j];
    ans = "";
    rep(i, 0, h) rep(j, 0, w) {
      vector<vector<bool>> a(h, vector<bool>(w, false));
      if ('0' <= board[i][j] && board[i][j] <= '9')
        dfs(i, j, "", a);
    }
    cout << ans << endl;
  }
  return 0;
}