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

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)

int W, H;
char grid[70][70];
vector<char> dp[70][70];

vector<char> max_str(vector<char> str1, vector<char> str2) {
  if (str1.size() > str2.size()) {
    return str1;
  } else if (str1.size() < str2.size()) {
    return str2;
  } else {
    bool ret1flag = false;

    rep(i, str1.size()) {
      if (str1[i] > str2[i]) {
        ret1flag = true;
        break;
      } else if (str2[i] > str1[i]) {
        break;
      }
    }

    if (ret1flag == true) {
      return str1;
    } else {
      return str2;
    }
  }
}

bool isranged(int y, int x) {
  if (y >= 0 && y < H && x >= 0 && x < W) {
    return true;
  } else {
    return false;
  }
}

int main() {
  while (true) {
    cin >> W >> H;

    if (W == 0 && H == 0) {
      break;
    }

    rep(i, 70) {
      rep(j, 70) { dp[i][j].clear(); }
    }

    rep(y, H) {
      rep(x, W) { cin >> grid[y][x]; }
    }

    rep(y, H) {
      rep(x, W) {
        if (grid[y][x] >= 'A' && grid[y][x] <= 'Z') {
          continue;
        }

        if (grid[y][x] >= '0' && grid[y][x] <= '9') {
          if (isranged(y - 1, x) == true && isranged(y, x - 1) == true) {
            dp[y][x] = max_str(dp[y - 1][x], dp[y][x - 1]);
            if (grid[y][x] != '0' || dp[y][x].empty() != true) {
              dp[y][x].push_back(grid[y][x]);
            }
          } else if (isranged(y - 1, x) == true &&
                     isranged(y, x - 1) == false) {
            dp[y][x] = dp[y - 1][x];
            if (grid[y][x] != '0' || dp[y][x].empty() != true) {
              dp[y][x].push_back(grid[y][x]);
            }
          } else if (isranged(y - 1, x) == false &&
                     isranged(y, x - 1) == true) {
            dp[y][x] = dp[y][x - 1];
            if (grid[y][x] != '0' || dp[y][x].empty() != true) {
              dp[y][x].push_back(grid[y][x]);
            }
          } else {
            if (grid[y][x] != '0' || dp[y][x].empty() != true) {
              dp[y][x].push_back(grid[y][x]);
            }
          }
        }
      }
    }

    vector<char> answer;

    rep(y, H) {
      rep(x, W) { answer = max_str(dp[y][x], answer); }
    }

    rep(i, answer.size()) { cout << answer[i]; }

    cout << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)

int W, H;
char grid[70][70];
int max_depth;
vector<char> answer;

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

void dfs(int y, int x, int depth, vector<char> now) {
  now.push_back(grid[y][x]);

  if (depth > max_depth) {
    max_depth = depth;
    answer = now;
  } else if (depth == max_depth) {
    bool flag = false;

    rep(i, max_depth) {
      if (now[i] > answer[i]) {
        flag = true;
        break;
      } else if (now[i] < answer[i]) {
        break;
      }
    }

    if (flag == true) {
      answer = now;
    }
  }

  rep(i, 2) {
    int ny = y + dy[i];
    int nx = x + dx[i];

    if (ny < 0 || ny >= H || nx < 0 || nx >= W) {
      continue;
    }

    if (grid[ny][nx] >= 'A' && grid[ny][nx] <= 'Z') {
      continue;
    }

    dfs(ny, nx, depth + 1, now);
  }
}

int main() {
  while (true) {
    cin >> W >> H;

    if (W == 0 && H == 0) {
      break;
    }

    max_depth = 0;
    answer.clear();

    rep(y, H) {
      rep(x, W) { cin >> grid[y][x]; }
    }

    vector<char> start;

    rep(y, H) {
      rep(x, W) {
        if (grid[y][x] >= 'A' && grid[y][x] <= 'Z') {
          continue;
        }

        if (grid[y][x] == '0') {
          continue;
        }

        dfs(y, x, 1, start);
      }
    }

    rep(i, max_depth) { cout << answer[i]; }

    cout << endl;
  }

  return 0;
}