Problem p00707 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>

using namespace std;

#define INF 999999999
#define REP(i, n) for (int i = 0; i < (int)(n); i++)

vector<string> in(72);
string dp[72][72];
string ans;

bool com(string a, string b) {
  if (a.size() > b.size())
    return true;
  else if (a.size() == b.size()) {
    REP(i, a.size()) {
      if (a[i] > b[i])
        return true;
      else if (a[i] < b[i])
        return false;
    }
    return true;
  } else
    return false;
}

int main() {
  int i, j, w, h;
  string p;
  while (1) {
    cin >> w >> h;
    if (!w)
      break;
    REP(a, h) {
      cin >> in[a + 1];
      in[a + 1].insert(in[a + 1].begin(), 'A');
      in[a + 1].insert(in[a + 1].end(), 'A');
    }
    in[h + 1] = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
                "AAAAAAAAAA";
    in[0] = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
            "AAAAAA";
    REP(a, h + 2) dp[a][0] = "";
    REP(a, w + 2) dp[0][a] = "";
    dp[1][1] = "";
    ans = "";
    for (i = 1; i <= h; i++) {
      for (j = 1; j <= w; j++) {
        if (in[i][j] - '0' >= 0 && in[i][j] - '0' <= 9) {
          dp[i][j] = "";
          if (dp[i - 1][j] != "") {
            dp[i][j] = dp[i - 1][j];
          }
          if (dp[i][j - 1] != "") {
            if (com(dp[i][j - 1], dp[i][j]))
              dp[i][j] = dp[i][j - 1];
          }
          if (!(dp[i][j] == "" && in[i][j] == '0'))
            dp[i][j].insert(dp[i][j].end(), in[i][j]);
        } else
          dp[i][j] = "";
      }
    }
    ans = "";
    for (i = 1; i <= h; i++) {
      for (j = 1; j <= w; j++) {
        if (com(dp[i][j], ans))
          ans = dp[i][j];
      }
    }
    cout << ans << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

#define INF 999999999
#define REP(i, n) for (int i = 0; i < (int)(n); i++)

struct nu {
  int x, y;
  string s;
};

vector<string> in(72);
string ans;
queue<nu> q;

void search(nu v) {
  // cout << v.x <<"~"<< v.y << "~" << v.s << endl;
  nu ins;
  int fla = 0;
  if (in[v.x + 1][v.y] - '0' >= 0 && in[v.x + 1][v.y] - '0' <= 9) {
    fla++;
    ins.s = v.s + in[v.x + 1][v.y];
    ins.x = v.x + 1;
    ins.y = v.y;
    q.push(ins);
  }
  if (in[v.x][v.y + 1] - '0' >= 0 && in[v.x][v.y + 1] - '0' <= 9) {
    fla++;
    ins.s = v.s + in[v.x][v.y + 1];
    ins.x = v.x;
    ins.y = v.y + 1;
    q.push(ins);
  }
  if (!fla) {
    if (v.s.size() > ans.size())
      ans = v.s;
    else if (v.s.size() == ans.size()) {
      REP(a, v.s.size()) {
        if (v.s[a] > ans[a])
          ans = v.s;
        else if (v.s[a] < ans[a])
          break;
      }
    }
  }
  q.pop();
  if (!q.empty())
    search(q.front());
}

int main() {
  int i, j, w, h;
  while (1) {
    cin >> w >> h;
    if (!w)
      break;
    REP(a, h) {
      cin >> in[a + 1];
      in[a + 1] = "A" + in[a + 1] + "A";
    }
    in[h + 1] = "";
    in[0] = "";
    REP(a, w) in[h + 1] += "A";
    REP(a, w) in[0] += "A";
    ans = "";
    for (i = 1; i <= h; i++) {
      for (j = 1; j <= w; j++) {
        if ((in[i - 1][j] - '0' < 1 || in[i - 1][j] - '0' > 9) &&
            (in[i][j - 1] - '0' < 1 || in[i][j - 1] - '0' > 9) &&
            (in[i][j] - '0' >= 1 && in[i][j] - '0' <= 9)) {
          nu v;
          v.x = i;
          v.y = j;
          v.s = "";
          v.s += in[i][j];
          q.push(v);
          search(v);
        }
      }
    }
    cout << ans << endl;
  }

  return 0;
}