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

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define LOG(...) printf(__VA_ARGS__)
// #define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define RSORT(c) sort((c).rbegin(), (c).rend())
#define CLR(a) memset((a), 0, sizeof(a))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};

int main() {
  int w, h;
  while (cin >> w >> h, w) {
    vector<string> codes(h);
    REP(i, h)
    cin >> codes[i];
    vector<vector<string>> memo(h, vector<string>(w));
    REP(i, h)
    REP(j, w)
    memo[i][j] = codes[i][j];
    for (int i = h - 1; i >= 0; i--) {
      for (int j = w - 1; j >= 0; j--) {
        if (isdigit(codes[i][j])) {
          if (i != h - 1 && isdigit(codes[i + 1][j]))
            if (memo[i][j].length() < memo[i + 1][j].length() + 1)
              memo[i][j] = codes[i][j] + memo[i + 1][j];
            else if (memo[i][j].length() == memo[i + 1][j].length() + 1)
              memo[i][j] = max(memo[i][j], codes[i][j] + memo[i + 1][j]);
          if (j != w - 1 && isdigit(codes[i][j + 1]))
            if (memo[i][j].length() < memo[i][j + 1].length() + 1)
              memo[i][j] = codes[i][j] + memo[i][j + 1];
            else if (memo[i][j].length() == memo[i][j + 1].length() + 1)
              memo[i][j] = max(memo[i][j], codes[i][j] + memo[i][j + 1]);
        }
      }
    }
    string ans = "0";
    REP(i, h)
    REP(j, w) {
      if ('1' <= memo[i][j][0] && memo[i][j][0] <= '9') {
        if (ans.length() < memo[i][j].length())
          ans = memo[i][j];
        else if (ans.length() == memo[i][j].length())
          ans = max(ans, memo[i][j]);
      }
    }
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define LOG(...) printf(__VA_ARGS__)
// #define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define RSORT(c) sort((c).rbegin(), (c).rend())
#define CLR(a) memset((a), 0, sizeof(a))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};

int main() {
  int w, h;
  while (cin >> w >> h, w) {
    vector<string> vs(h);
    vector<vector<bool>> vvb(h);
    REP(i, h) {
      cin >> vs[i];
      REP(j, w) { vvb[i].push_back('0' <= vs[i][j] && vs[i][j] <= '9'); }
    }

    vector<vector<int>> vvi(h);
    int max2 = 1;
    int x, y;
    REP(i, h) {
      REP(j, w) {
        if (vvb[i][j]) {
          int max = 1;
          if (i > 0 && vvb[i - 1][j])
            max = vvi[i - 1][j] + 1;
          if (j > 0 && vvb[i][j - 1])
            if (max < vvi[i][j - 1] + 1)
              max = vvi[i][j - 1] + 1;
          vvi[i].push_back(max);
          if (max > max2) {
            max2 = max;
            x = j;
            y = i;
          }
        } else {
          vvi[i].push_back(-1);
        }
      }
    }
    vector<vector<int>> vvi2(h);
    REP(i, h) {
      REP(j, w) { vvi2[i].push_back(-1); }
    }
    vvi2[y][x] = max2;
    queue<int> qi;
    qi.push(max2 * 10000 + x * 100 + y);
    while (!qi.empty()) {
      int pos = qi.front();
      qi.pop();
      int max3 = pos / 10000;
      int x2 = (pos / 100) % 100;
      int y2 = pos % 100;
      if (x2 > 0 && vvb[y2][x2 - 1] && vvi2[y2][x2 - 1] == -1) {
        vvi2[y2][x2 - 1] = max3 - 1;
        qi.push((max3 - 1) * 10000 + (x2 - 1) * 100 + y2);
      }
      if (y2 > 0 && vvb[y2 - 1][x2] && vvi2[y2 - 1][x2] == -1) {
        vvi2[y2 - 1][x2] = max3 - 1;
        qi.push((max3 - 1) * 10000 + x2 * 100 + (y2 - 1));
      }
    }
    char max4 = '0' - 1;
    int x3, y3;
    REP(i, h) {
      REP(j, w) {
        if (vvi2[i][j] == 1 && max4 < vs[i][j]) {
          max4 = vs[i][j];
          x3 = j;
          y3 = i;
        }
      }
    }

    string ans;
    if (max4 != '0')
      ans += max4;
    REP(i, max2 - 1) {
      bool abb = false;
      max4 = '0' - 1;
      if (y3 + 1 != h && vvi2[y3 + 1][x3] > 0 && max4 < vs[y3 + 1][x3]) {
        max4 = vs[y3 + 1][x3];
        y3++;
        abb = true;
      }
      if (abb) {
        if (x3 + 1 != w && vvi2[y3 - 1][x3 + 1] > 0 &&
            max4 < vs[y3 - 1][x3 + 1]) {
          max4 = vs[y3 - 1][x3 + 1];
          x3++;
          y3--;
        }
      } else {
        if (x3 + 1 != w && vvi2[y3][x3 + 1] > 0 && max4 < vs[y3][x3 + 1]) {
          max4 = vs[y3][x3 + 1];
          x3++;
        }
      }
      if (!(ans.empty() && max4 == '0'))
        ans += max4;
    }
    cout << ans << endl;
  }
}