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

#include <algorithm>
#include <cctype>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>
using namespace std;

char F[70][70];

// s <= t ?
bool compare(string &s, string &t) {
  int ls, lt;
  for (ls = 0; s[ls] == '0'; ++ls)
    ;
  for (lt = 0; t[lt] == '0'; ++lt)
    ;
  s = s.substr(ls);
  t = t.substr(lt);
  if (s.length() < t.length())
    return true;
  if (s.length() > t.length())
    return false;
  return s <= t;
}

int main() {
  ios::sync_with_stdio(false);
  while (true) {
    int W, H;
    cin >> W >> H;
    if (W == 0 && H == 0)
      break;
    for (int i = 0; i < H; ++i)
      cin >> F[i];
    string ans = "0";
    queue<pair<string, pair<int, int>>> Q;
    for (int i = 0; i < H; ++i) {
      for (int j = 0; j < W; ++j) {
        if (isdigit(F[i][j])) {
          string s;
          s.push_back(F[i][j]);
          Q.emplace(make_pair(s, make_pair(i, j)));
        }
      }
    }
    string G[H][W];
    for (int i = 0; i < H; ++i) {
      for (int j = 0; j < W; ++j) {
        G[i][j] = "0";
      }
    }
    while (!Q.empty()) {
      auto p = Q.front();
      Q.pop();
      if (compare(ans, p.first))
        ans = p.first;
      int i = p.second.first;
      int j = p.second.second;
      if (compare(G[i][j], p.first) == false)
        continue;
      if (i + 1 < H && isdigit(F[i + 1][j])) {
        string s = p.first;
        s.push_back(F[i + 1][j]);
        if (compare(s, G[i + 1][j]) == false) {
          G[i + 1][j] = s;
          Q.emplace(make_pair(s, make_pair(i + 1, j)));
        }
      }
      if (j + 1 < W && isdigit(F[i][j + 1])) {
        string s = p.first;
        s.push_back(F[i][j + 1]);
        if (compare(s, G[i][j + 1]) == false) {
          G[i][j + 1] = s;
          Q.emplace(make_pair(s, make_pair(i, j + 1)));
        }
      }
    }
    int l = 0;
    while (ans[l] == '0')
      ++l;
    ans = ans.substr(l);
    if (ans == "")
      cout << "0" << endl;
    else
      cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>
using namespace std;

char F[70][70];

// s <= t ?
bool compare(string s, string t) {
  int ls, lt;
  for (ls = 0; s[ls] == '0'; ++ls)
    ;
  for (lt = 0; t[lt] == '0'; ++lt)
    ;
  s = s.substr(ls);
  t = t.substr(lt);
  if (s.length() < t.length())
    return true;
  if (s.length() > t.length())
    return false;
  for (int i = 0; i < s.length(); ++i) {
    if (s[i] > t[i])
      return false;
  }
  return true;
}

int main() {
  ios::sync_with_stdio(false);
  while (true) {
    int W, H;
    cin >> W >> H;
    if (W == 0 && H == 0)
      break;
    for (int i = 0; i < H; ++i)
      cin >> F[i];
    string ans = "0";
    queue<pair<string, pair<int, int>>> Q;
    for (int i = 0; i < H; ++i) {
      for (int j = 0; j < W; ++j) {
        if (isdigit(F[i][j])) {
          string s;
          s.push_back(F[i][j]);
          Q.emplace(make_pair(s, make_pair(i, j)));
        }
      }
    }
    string G[H][W];
    for (int i = 0; i < H; ++i) {
      for (int j = 0; j < W; ++j) {
        G[i][j] = "0";
      }
    }
    while (!Q.empty()) {
      auto p = Q.front();
      Q.pop();
      if (compare(ans, p.first))
        ans = p.first;
      int i = p.second.first;
      int j = p.second.second;
      if (compare(G[i][j], p.first) == false)
        continue;
      if (i + 1 < H && isdigit(F[i + 1][j])) {
        string s = p.first;
        if (s == "0" && F[i + 1][j] == '0') {
        } else
          s.push_back(F[i + 1][j]);
        if (compare(s, G[i + 1][j]) == false) {
          G[i + 1][j] = s;
          Q.emplace(make_pair(s, make_pair(i + 1, j)));
        }
      }
      if (j + 1 < W && isdigit(F[i][j + 1])) {
        string s = p.first;
        if (s == "0" && F[i][j + 1] == '0') {
        } else
          s.push_back(F[i][j + 1]);
        if (compare(s, G[i][j + 1]) == false) {
          G[i][j + 1] = s;
          Q.emplace(make_pair(s, make_pair(i, j + 1)));
        }
      }
    }
    int l = 0;
    while (ans[l] == '0')
      ++l;
    ans = ans.substr(l);
    cout << ans << endl;
  }
}