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

#include "bits/stdc++.h"
using namespace std;
#define mod 1000000007
#define all(c) begin(c), end(c)
template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }
using ll = long long;
using vi = vector<int>;
using vs = vector<string>;
int dd[] = {0, 1, 0, -1, 0}; // →↓←↑

int W, H;
string ans;
int m;
vector<string> C;
vector<vector<bool>> B;
vector<vs> dp;

// void dfs(int y, int x, int depth, string s)
//{
//	if (('A' <= C[y][x] && C[y][x] <= 'Z'))
//	{
//		if (m < depth)
//		{
//			m = depth;
//			ans = s;
//		}
//		else if (m == depth)
//		{
//			chmax(ans, s);
//		}
//		return;
//	}
//	B[y][x] = true;
//	if(y < H - 1)dfs(y + 1, x, depth + 1, s + C[y][x]);
//	if(x < W - 1)dfs(y, x + 1, depth + 1, s + C[y][x]);
// }
void solve() {
  while (true) {
    cin >> W >> H;
    if (W == 0 && H == 0)
      break;
    ans = "";
    m = 0;
    C = vs(H);
    dp = vector<vs>(H, vs(W, ""));
    for (int i = 0; i < H; i++) {
      cin >> C[i];
    }
    string ans = "";
    for (int y = 0; y < H; y++) {
      for (int x = 0; x < W; x++) {
        if ('A' <= C[y][x] && C[y][x] <= 'Z')
          continue;

        dp[y][x] = C[y][x];
        if (0 <= y - 1 && dp[y - 1][x] != "0") {
          dp[y][x] = dp[y - 1][x] + C[y][x];
        }
        if (0 <= x - 1 && dp[y][x - 1] != "0") {
          if (dp[y][x].size() < dp[y][x - 1].size() + 1) {
            dp[y][x] = dp[y][x - 1] + C[y][x];
          } else if (dp[y][x].size() == dp[y][x - 1].size() + 1) {
            chmax(dp[y][x], dp[y][x - 1] + C[y][x]);
          }
        }

        if (ans.size() < dp[y][x].size()) {
          ans = dp[y][x];
        } else if (ans.size() == dp[y][x].size()) {
          chmax(ans, dp[y][x]);
        }
      }
    }
    cout << ans << endl;
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  solve();
  return 0;
}


Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
#define mod 1000000007
#define all(c) begin(c), end(c)
template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }
using ll = long long;
using vi = vector<int>;
int dd[] = {0, 1, 0, -1, 0}; // →↓←↑

int W, H;
string ans;
int m;
vector<string> C;
vector<vector<bool>> B;

void dfs(int y, int x, int depth, string s) {
  if (('A' <= C[y][x] && C[y][x] <= 'Z')) {
    if (m < depth) {
      m = depth;
      ans = s;
    } else if (m == depth) {
      chmax(ans, s);
    }
    return;
  }
  B[y][x] = true;
  if (y < H - 1)
    dfs(y + 1, x, depth + 1, s + C[y][x]);
  if (x < W - 1)
    dfs(y, x + 1, depth + 1, s + C[y][x]);
}
void solve() {
  while (true) {
    cin >> W >> H;
    if (W == 0 && H == 0)
      break;
    ans = "";
    m = 0;
    C = vector<string>(H);
    B = vector<vector<bool>>(H, vector<bool>(W, false));
    for (int i = 0; i < H; i++) {
      cin >> C[i];
    }

    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        if (B[i][j] || C[i][j] == '0')
          continue;
        dfs(i, j, 0, "");
      }
    }
    cout << ans << endl;
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  solve();
  return 0;
}