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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

inline bool cheak(int x, int y, int xMax, int yMax) {
  return x >= 0 && y >= 0 && xMax > x && yMax > y;
}
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
template <class T> inline T sqr(T x) { return x * x; }

typedef pair<int, int> P;
typedef tuple<int, int, int> T;
typedef long long ll;
typedef unsigned long long ull;

#define For(i, a, b) for (int(i) = (a); i < (b); (i)++)
#define rep(i, n) For(i, 0, n)
#define clr(a) memset((a), 0, sizeof(a))
#define mclr(a) memset((a), -1, sizeof(a))
#define all(a) (a).begin(), (a).end()
#define sz(a) (sizeof(a))
#define Fill(a, v) fill((int *)a, (int *)(a + (sz(a) / sz(*(a)))), v)

const int dx[8] = {-1, 0, -1, 0, 1, 1, -1, -1},
          dy[8] = {0, -1, 0, 1, -1, 1, -1, 1};
const int mod = 1000000007;
const int INF = 1e9;

string c[71];
int w, h;

string d[71][71];

int str_compare(string s1, string s2) {

  if (s1.size() > s2.size())
    return 1;
  if (s1.size() < s2.size())
    return 0;

  while (s1.size() != 1 && s1[0] == '0') {
    s1 = s1.substr(1, s1.size() - 1);
  }
  while (s2.size() != 1 && s2[0] == '0') {
    s2 = s2.substr(1, s2.size() - 1);
  }

  if (s1.size() > s2.size())
    return 1;
  if (s1.size() < s2.size())
    return 0;

  if (s1.size() == s2.size()) {
    if (s1 > s2)
      return 1;
    if (s1 < s2)
      return 0;
    if (s1 == s2)
      return -1;
  }
}

void func(int y, int x) {

  rep(i, 2) {
    int ty = y + dy[i], tx = x + dx[i];
    if (cheak(tx, ty, w, h) && (c[ty][tx] >= '0' && c[ty][tx] <= '9')) {

      string t = (c[ty][tx]) + d[y][x];
      if (str_compare(d[ty][tx], t) == 0) {
        d[ty][tx] = t;
        func(ty, tx);
      }
    }
  }
}

int main() {
  while (cin >> w >> h && (w || h)) {
    rep(y, h) {
      cin >> c[y];
      rep(x, w) if (c[y][x] >= '0' && c[y][x] <= '9') d[y][x] = c[y][x];
    }
    for (int y = h - 1; y >= 0; y--)
      for (int x = w - 1; x >= 0; x--)
        if (c[y][x] >= '0' && c[y][x] <= '9') {
          func(y, x);
        }

    string ans = "0";

    rep(y, h) rep(x, w) if (c[y][x] >= '0' && c[y][x] <= '9') {

      while (d[y][x].size() != 1 && d[y][x][0] == '0') {
        d[y][x] = d[y][x].substr(1, d[y][x].size() - 1);
      }

      if (str_compare(ans, d[y][x]) == 0) {
        ans = d[y][x];
      }
    }

    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

inline bool cheak(int x, int y, int xMax, int yMax) {
  return x >= 0 && y >= 0 && xMax > x && yMax > y;
}
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
template <class T> inline T sqr(T x) { return x * x; }

typedef pair<int, int> P;
typedef tuple<int, int, int> T;
typedef long long ll;
typedef unsigned long long ull;

#define For(i, a, b) for (int(i) = (a); i < (b); (i)++)
#define rep(i, n) For(i, 0, n)
#define clr(a) memset((a), 0, sizeof(a))
#define mclr(a) memset((a), -1, sizeof(a))
#define all(a) (a).begin(), (a).end()
#define sz(a) (sizeof(a))
#define Fill(a, v) fill((int *)a, (int *)(a + (sz(a) / sz(*(a)))), v)

const int dx[8] = {-1, 0, -1, 0, 1, 1, -1, -1},
          dy[8] = {0, -1, 0, 1, -1, 1, -1, 1};
const int mod = 1000000007;
const int INF = 1e9;

string c[71];
int w, h;

string d[71][71];

int str_compare(string s1, string s2) {

  while (s1[0] == '0') {
    s1 = s1.substr(1, s1.size() - 1);
  }
  while (s2[0] == '0') {
    s2 = s2.substr(1, s2.size() - 1);
  }

  if (s1.size() > s2.size())
    return 1;
  if (s1.size() < s2.size())
    return 0;
  if (s1.size() == s2.size()) {
    if (s1 > s2)
      return 1;
    if (s1 < s2)
      return 0;
    if (s1 == s2)
      return -1;
  }
}

void func(int y, int x) {

  rep(i, 2) {
    int ty = y + dy[i], tx = x + dx[i];
    if (cheak(tx, ty, w, h) && (c[ty][tx] >= '0' && c[ty][tx] <= '9')) {

      string t = (c[ty][tx]) + d[y][x];
      if (str_compare(d[ty][tx], t) != 1) {
        d[ty][tx] = t;
        func(ty, tx);
      }
    }
  }
}

int main() {
  while (cin >> w >> h && (w || h)) {
    rep(y, h) {
      cin >> c[y];
      rep(x, w) if (c[y][x] >= '0' && c[y][x] <= '9') d[y][x] = c[y][x];
    }
    for (int y = h - 1; y >= 0; y--)
      for (int x = w - 1; x >= 0; x--)
        if (c[y][x] >= '0' && c[y][x] <= '9') {
          func(y, x);
        }

    string ans = "0";

    rep(y, h) rep(x, w) if (c[y][x] >= '0' && c[y][x] <= '9') {
      if (str_compare(ans, d[y][x]) == 0) {
        ans = d[y][x];
      }
    }
    while (ans[0] == '0') {
      ans = ans.substr(1, ans.size() - 1);
    }

    cout << ans << endl;
  }
  return 0;
}