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

#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

string norm(string str) {
  int ind = 0;
  for (int i = 0; i < str.length() && str[i] == '0'; ++i) {
    ++ind;
  }
  if (ind > 0) {
    return str.substr(ind, str.length() - ind);
  }
  return str;
}

string str_max(string s1, string s2) {
  s1 = norm(s1);
  s2 = norm(s2);
  if (s1.length() > s2.length()) {
    return s1;
  }
  if (s2.length() > s1.length()) {
    return s2;
  }

  if (s1 > s2) {
    return s1;
  }
  return s2;
}

int main(void) {
  int W, H;
  while (cin >> W >> H, W) {
    vector<vector<string>> mat(H, vector<string>(W));
    for (int i = 0; i < H; ++i) {
      for (int j = 0; j < W; ++j) {
        string temp = "";
        char c;
        cin >> c;
        if (c >= '0' && c <= '9') {
          temp += c;
        } else {
          temp = " ";
        }
        mat[i][j] = temp;
      }
    }

    string res = "0";
    vector<vector<string>> dp(H, vector<string>(W, ""));
    for (int i = 0; i < H; ++i) {
      for (int j = 0; j < W; ++j) {
        if (mat[i][j] != " ") {
          if (dp[i][j] == "") {
            dp[i][j] = mat[i][j];
            res = str_max(res, dp[i][j]);
          }
          if (j + 1 < W && mat[i][j + 1] != " ") {
            dp[i][j + 1] = str_max(dp[i][j] + mat[i][j + 1], dp[i][j + 1]);
            res = str_max(dp[i][j + 1], res);
          }
          if (i + 1 < H && mat[i + 1][j] != " ") {
            dp[i + 1][j] = str_max(dp[i][j] + mat[i + 1][j], dp[i + 1][j]);
            res = str_max(dp[i + 1][j], res);
          }
        }
      }
    }
    cout << res << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

int W, H;
string res, comp;

void dfs(string sn, vector<pair<int, int>> ps,
         map<pair<int, int>, string> &memo, vector<vector<string>> &mat) {
  pair<int, int> np = ps[ps.size() - 1];
  bool next = false;
  if (np.first + 1 < H) {
    string ns = mat[np.first + 1][np.second];
    if (ns != " ") {
      vector<pair<int, int>> temp = ps;
      temp.push_back({np.first + 1, np.second});
      next = true;
      dfs(sn + ns, temp, memo, mat);
    }
  }

  if (np.second + 1 < W) {
    string ns = mat[np.first][np.second + 1];
    if (ns != " ") {
      vector<pair<int, int>> temp = ps;
      temp.push_back({np.first, np.second + 1});
      next = true;
      dfs(sn + ns, temp, memo, mat);
    }
  }

  if (!next) {
    for (int i = 0; i < ps.size(); ++i) {
      if (sn > memo[ps[i]]) {
        memo[ps[i]] = sn;
      }
    }
    if (sn.length() >= res.length()) {
      if (sn.length() == res.length()) {
        if (sn > res) {
          res = sn;
        }
      } else {
        res = sn;
      }
    }
  }
  return;
}

int main(void) {
  while (cin >> W >> H, W) {
    res = "0";
    vector<vector<string>> mat(H, vector<string>(W));
    vector<pair<int, int>> start;
    for (int i = 0; i < H; ++i) {
      for (int j = 0; j < W; ++j) {
        string temp = "";
        char c;
        cin >> c;
        if (c >= '0' && c <= '9') {
          start.push_back({i, j});
          temp += c;
        } else {
          temp = " ";
        }
        mat[i][j] = temp;
      }
    }
    map<pair<int, int>, string> memo;
    for (int i = 0; i < start.size(); ++i) {
      if (memo[start[i]] == "" && mat[start[i].first][start[i].second] != "0") {
        comp = "";
        vector<pair<int, int>> temp;
        temp.push_back(start[i]);
        dfs(mat[start[i].first][start[i].second], temp, memo, mat);
      }
    }
    cout << res << endl;
  }
  return 0;
}