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

#include <bits/stdc++.h>

#define LL long long
#define VI vector<int>
#define VVI vector<vector<int>>
#define VVVI vector<vector<vector<int>>>
#define VVVVI vector<vector<vector<vector<int>>>>
#define VL vector<LL>
#define VVL vector<vector<LL>>
#define VVVL vector<vector<vector<LL>>>
#define VVVVL vector<vector<vector<vector<LL>>>>
#define VB vector<bool>
#define FOR(i, a, b) for (int i = (a); i < ((int)b); ++i)
#define RFOR(i, a) for (int i = (a); i >= 0; --i)
#define FOE(i, a) for (auto i : a)
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define DUMP(x) cerr << #x << " = " << (x) << endl;
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v, x) (std::find(v.begin(), v.end(), x) != v.end())
#define BIT(n) (1LL << (n))
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
#define EPS 1e-14

template <class T> T ceil(T a, T b) { return (a + b - 1) / b; }
template <class T> void print(T x) { std::cout << x << std::endl; }
inline bool inside(int y, int x, int H, int W) {
  return 0 <= y and y < H and 0 <= x and x < W;
}

const std::string YES = "YES", Yes = "Yes", NO = "NO", No = "No";
const std::vector<int> dy = {0, 1, 0, -1},
                       dx = {1, 0, -1, 0}; // 4近傍（右, 下, 左, 上）

using namespace std;

bool big(string a, string b) {
  if (a.size() != b.size()) {
    return a.size() > b.size();
  }
  FOR(i, 0, a.size()) {
    if ((a[i] - '0') > (b[i] - '0')) {
      return true;
    } else if ((a[i] - '0') < (b[i] - '0')) {
      return false;
    }
  }

  return false;
}

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

  while (true) {
    int W, H;
    cin >> W >> H;
    if (W == 0 and H == 0) {
      break;
    }

    vector<vector<int>> field(H, vector<int>(W, -1));
    FOR(i, 0, H) {
      string s;
      cin >> s;
      FOR(j, 0, s.size()) {
        if (s[j] < 'A') {
          field[i][j] = (s[j] - '0');
        }
      }
    }

    vector<vector<string>> dp(H, vector<string>(W));
    FOR(y, 0, H) {
      FOR(x, 0, W) {
        if (field[y][x] == -1) {
          continue;
        }

        if (dp[y][x].empty() and field[y][x] == 0) {
          continue;
        }

        // start
        if (dp[y][x].empty()) {
          dp[y][x] = to_string(field[y][x]);
        }

        if (y + 1 < field.size() and field[y + 1][x] != -1) {
          string new_c = dp[y][x] + to_string(field[y + 1][x]);
          if (big(new_c, dp[y + 1][x])) {
            dp[y + 1][x] = new_c;
          }
        }
        if (x + 1 < field[0].size() and field[y][x + 1] != -1) {
          string new_c = dp[y][x] + to_string(field[y][x + 1]);
          if (big(new_c, dp[y][x + 1])) {
            dp[y][x + 1] = new_c;
          }
        }
      }
    }

    string ans = "";
    FOR(y, 0, dp.size()) {
      FOR(x, 0, dp[0].size()) {
        if (big(dp[y][x], ans)) {
          ans = dp[y][x];
        }
      }
    }

    print(ans);
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define LL long long
#define VI vector<int>
#define VVI vector<vector<int>>
#define VVVI vector<vector<vector<int>>>
#define VVVVI vector<vector<vector<vector<int>>>>
#define VL vector<LL>
#define VVL vector<vector<LL>>
#define VVVL vector<vector<vector<LL>>>
#define VVVVL vector<vector<vector<vector<LL>>>>
#define VB vector<bool>
#define FOR(i, a, b) for (int i = (a); i < ((int)b); ++i)
#define RFOR(i, a) for (int i = (a); i >= 0; --i)
#define FOE(i, a) for (auto i : a)
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define DUMP(x) cerr << #x << " = " << (x) << endl;
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v, x) (std::find(v.begin(), v.end(), x) != v.end())
#define BIT(n) (1LL << (n))
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
#define EPS 1e-14

template <class T> T ceil(T a, T b) { return (a + b - 1) / b; }
template <class T> void print(T x) { std::cout << x << std::endl; }
inline bool inside(int y, int x, int H, int W) {
  return 0 <= y and y < H and 0 <= x and x < W;
}

const std::string YES = "YES", Yes = "Yes", NO = "NO", No = "No";
const std::vector<int> dy = {0, 1, 0, -1},
                       dx = {1, 0, -1, 0}; // 4近傍（右, 下, 左, 上）

using namespace std;

bool big(string a, string b) {
  if (a.size() != b.size()) {
    return a.size() > b.size();
  }
  FOR(i, 0, a.size()) {
    if ((a[i] - '0') > (b[i] - '0')) {
      return true;
    } else if ((a[i] - '0') < (b[i] - '0')) {
      return false;
    }
  }

  return false;
}

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

  while (true) {
    int W, H;
    cin >> W >> H;
    if (W == 0 and H == 0) {
      break;
    }

    vector<vector<int>> field(H, vector<int>(W, -1));
    FOR(i, 0, H) {
      string s;
      cin >> s;
      FOR(j, 0, s.size()) {
        if (s[j] < 'A') {
          field[i][j] = (s[j] - '0');
        }
      }
    }

    string ans = "";
    FOR(sy, 0, H) {
      FOR(sx, 0, W) {
        if (field[sy][sx] == -1 or field[sy][sx] == 0) {
          continue;
        }

        vector<vector<string>> dp(H, vector<string>(W));
        dp[sy][sx] = "" + to_string(field[sy][sx]);

        stack<pair<int, int>> s;
        s.push(make_pair(sy, sx));
        while (not s.empty()) {
          int y, x;
          tie(y, x) = s.top();
          s.pop();
          string now = dp[y][x];

          FOR(i, 0, 2) {
            int ny = y + dy[i];
            int nx = x + dx[i];
            if (inside(ny, nx, H, W) and field[ny][nx] != -1) {
              string ns = now + to_string(field[ny][nx]);
              if (big(ns, dp[ny][nx])) {
                dp[ny][nx] = ns;
                if (big(ns, ans)) {
                  ans = ns;
                }
                s.push(make_pair(ny, nx));
              }
            }
          }
        }
      }
    }

    print(ans);
  }

  return 0;
}