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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;

#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = (1LL << 31) - 1;
const ll MOD = 1e9 + 7;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, N) for (int i = 0; i < (N); i++)
#define ALL(s) (s).begin(), (s).end()
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()))
#define fi first
#define se second
#define N_SIZE (1LL << 20)
#define NIL -1
#define MAX_N 100100 * 3

int w, h;
string s[100];
string dp[100][100];

int dx[2] = {1, 0};
int dy[2] = {0, 1};

bool c(int y, int x) { return s[y][x] >= '0' && s[y][x] <= '9'; }

// void solve(int y, int x, string num) {
//	if (c(y, x)) {
//		if (num.size() == 0) {
//			if(s[y][x] != '0')num += s[y][x];
//		}
//		else num += s[y][x];
//	}
//	else return;
//	if (dp[y][x].size() > num.size())return;
//	else {
//		if (dp[y][x] >= num)return;
//	}
//	dp[y][x] = num;
//	REP(i, 2) {
//		int ny = y + dy[i], nx = x + dx[i];
//		if (ny >= 0 && ny < h&&nx >= 0 && nx < w) {
//			solve(ny, nx, num);
//		}
//	}
// }
//

int main() {
  while (cin >> w >> h && w + h) {
    REP(i, h) cin >> s[i];
    REP(i, 100) REP(j, 100) dp[i][j] = "";
    REP(i, h) REP(j, w) {
      if (c(i, j))
        dp[i][j] = s[i][j];
      else
        dp[i][j] = "";
    }
    REP(i, h) {
      REP(j, w) {
        REP(k, 2) {
          int ny = i + dy[k], nx = j + dx[k];
          if (ny < 0 || ny >= h || nx < 0 || nx >= w)
            continue;
          if (dp[i][j] != "0" && c(ny, nx)) {
            string t = dp[i][j] + s[ny][nx];
            if (dp[ny][nx].size() < t.size())
              dp[ny][nx] = t;
            else if (dp[ny][nx].size() == t.size()) {
              if (dp[ny][nx] < t)
                dp[ny][nx] = t;
            }
          }
        }
      }
    }
    string ans[1000];
    REP(i, 1000) ans[i] = "";
    REP(i, 100) {
      REP(j, 100) {
        // if (!dp[i][j].empty())cout << dp[i][j] << " ";
        ans[dp[i][j].size()] = max(ans[dp[i][j].size()], dp[i][j]);
      }
      // cout << endl;
    }
    for (int i = 1000 - 1; i >= 0; i--) {
      if (!ans[i].empty()) {
        cout << ans[i] << endl;
        break;
      }
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;

#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = (1LL << 31) - 1;
const ll MOD = 1e9 + 7;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, N) for (int i = 0; i < (N); i++)
#define ALL(s) (s).begin(), (s).end()
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()))
#define fi first
#define se second
#define N_SIZE (1LL << 20)
#define NIL -1
#define MAX_N 100100 * 3

int w, h;
string s[100];
string dp[100][100];

int dx[2] = {1, 0};
int dy[2] = {0, 1};

bool c(int y, int x) {
  if (y >= 0 && y < h && x >= 0 && x < w) {
    return s[y][x] >= '0' && s[y][x] <= '9';
  }
  return false;
}

void solve(int y, int x, string num) {
  num += s[y][x];
  if (dp[y][x].size() > num.size())
    return;
  else {
    if (dp[y][x] >= num)
      return;
  }
  dp[y][x] = num;
  REP(i, 2) {
    if (c(y + dy[i], x + dx[i])) {
      solve(y + dy[i], x + dx[i], num);
    }
  }
}

int main() {
  while (cin >> w >> h && w + h) {
    REP(i, 100) REP(j, 100) dp[i][j] = "";
    REP(i, h) cin >> s[i];
    REP(i, h) {
      REP(j, w) {
        if (s[i][j] == '0')
          continue;
        if (c(i, j) && dp[i][j] == "")
          solve(i, j, "");
      }
    }
    string ans[1000];
    REP(i, 1000) ans[i] = "";
    REP(i, 100) {
      REP(j, 100) {
        ans[dp[i][j].size()] = max(ans[dp[i][j].size()], dp[i][j]);
      }
    }
    for (int i = 1000 - 1; i >= 0; i--) {
      if (!ans[i].empty()) {
        cout << ans[i] << endl;
        break;
      }
    }
  }
}