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

#define _USE_MATH_DEFINES
#define INF 100000000

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

static const double EPS = 1e-8;

int tx[] = {-1, 0, 1, 0};
int ty[] = {0, 1, 0, -1};

char stage[101][101];
int W, H;

string larger(const string &str1, const string &str2) {
  if (str1.size() > str2.size())
    return str1;
  if (str1.size() < str2.size())
    return str2;

  for (int i = 0; i < str1.size(); i++) {
    if (str1[i] > str2[i])
      return str1;
    if (str1[i] < str2[i])
      return str2;
  }

  return str1;
}

void dfs(int x, int y, string buf, string &res) {
  buf.push_back(stage[y][x]);

  if (y + 1 < H && isdigit(stage[y + 1][x])) {
    dfs(x, y + 1, buf, res);
  }

  if (x + 1 < W && isdigit(stage[y][x + 1])) {
    dfs(x + 1, y, buf, res);
  }

  res = larger(res, buf);
}

int main() {
  while (~scanf("%d %d", &W, &H)) {
    if (W == H && H == 0)
      break;
    for (int y = 0; y < H; y++) {
      char buf[128];
      scanf("%s", buf);
      for (int x = 0; x < W; x++) {
        stage[y][x] = buf[x];
      }
    }

    string dp[101][101];
    // for(int x=0;x<W;x++)
    //	if(isdigit(stage[0][x]) && stage[0][x] != '0'){
    //	dp[1][x+1] = dp[0][x] + stage[0][x];
    //	printf("dp[%d][%d]=%s\\n",0,x+1,dp[0][x+1].c_str());
    // }

    // for(int y=0;x<W;y++)
    //	if(isdigit(stage[0][x]) && stage[0][x] != '0'){
    //	dp[1][x+1] = dp[0][x] + stage[0][x];
    //	printf("dp[%d][%d]=%s\\n",0,x+1,dp[0][x+1].c_str());
    // }

    for (int y = 0; y < H; y++) {
      for (int x = 0; x < W; x++) {
        string left = "";
        if (isdigit(stage[y][x])) {
          if (!(dp[y + 1][x].size() == 0 && stage[y][x] == '0')) {
            left = dp[y + 1][x] + stage[y][x];
          }
        }

        string up = "";
        if (isdigit(stage[y][x])) {
          if (!(dp[y][x + 1].size() == 0 && stage[y][x] == '0')) {
            up = dp[y][x + 1] + stage[y][x];
          }
        }

        dp[y + 1][x + 1] = larger(left, up);
      }
    }

    string res = "0";
    for (int y = 0; y <= H; y++) {
      for (int x = 0; x <= W; x++) {
        res = larger(res, dp[y][x]);
      }
    }
    cout << res << endl;
  }
}

Error: TLE

Fixed submission:

#define _USE_MATH_DEFINES
#define INF 100000000

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

static const double EPS = 1e-8;

int tx[] = {-1, 0, 1, 0};
int ty[] = {0, 1, 0, -1};

char stage[101][101];
int W, H;

string larger(const string &str1, const string &str2) {
  if (str1.size() > str2.size())
    return str1;
  if (str1.size() < str2.size())
    return str2;

  for (int i = 0; i < str1.size(); i++) {
    if (str1[i] > str2[i])
      return str1;
    if (str1[i] < str2[i])
      return str2;
  }

  return str1;
}

void dfs(int x, int y, string buf, string &res) {
  buf.push_back(stage[y][x]);

  if (y + 1 < H && isdigit(stage[y + 1][x])) {
    dfs(x, y + 1, buf, res);
  }

  if (x + 1 < W && isdigit(stage[y][x + 1])) {
    dfs(x + 1, y, buf, res);
  }

  res = larger(res, buf);
}

int main() {
  while (~scanf("%d %d", &W, &H)) {
    if (W == H && H == 0)
      break;
    for (int y = 0; y < H; y++) {
      char buf[128];
      scanf("%s", buf);
      for (int x = 0; x < W; x++) {
        stage[y][x] = buf[x];
      }
    }

    string res = "0";
    for (int y = 0; y < H; y++) {
      for (int x = 0; x < W; x++) {
        if (!isdigit(stage[y][x]))
          continue;
        if (stage[y][x] == '0')
          continue;
        dfs(x, y, "", res);
      }
    }
    cout << res << endl;
  }
}