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

#include <algorithm>
#include <bitset>
#include <cctype>
#include <complex>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

#define VARIABLE(x) cerr << #x << "=" << x << endl
#define BINARY(x) static_cast<bitset<16>>(x);
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, m, n) for (int i = m; i < (int)(n); i++)
#define if_range(x, y, w, h)                                                   \\
  if (0 <= (int)(x) && (int)(x) < (int)(w) && 0 <= (int)(y) &&                 \\
      (int)(y) < (int)(h))

const int INF = 1e9;
const double EPS = 1e-8;
const double PI = 3.14159;
int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
using namespace std;
typedef long long ll;
// typedef pair<int, int> P;
struct P {
  int x, y, n;
  P(int n, int x, int y) : n(n), x(x), y(y) {}
  P() {}
};

/** Problem1126 : The Secret Number **/
const int MAX_W = 70;
int W, H;
char field[MAX_W][MAX_W];
string dp[MAX_W][MAX_W];

string big(string s1, string s2) {
  if (s1.length() > s2.length()) {
    return s1;
  } else if (s2.length() > s1.length()) {
    return s2;
  } else {
    return (s1 > s2 ? s1 : s2);
  }
}

string big2(string s1, string s2) {
  while (s1[0] == '0')
    s1.erase(s1.begin());
  while (s2[0] == '0')
    s2.erase(s2.begin());
  return big(s1, s2);
}

string rec(int x, int y) {
  if (!('0' <= field[y][x] && field[y][x] <= '9'))
    return dp[y][x] = "";

  string s1 = dp[y][x + 1];
  string s2 = dp[y + 1][x];

  return dp[y][x] = field[y][x] + big(s1, s2);
}

int main() {
  while (cin >> W >> H, W || H) {
    fill(dp[0], dp[0] + MAX_W * MAX_W, "");

    rep(i, H) {
      rep(j, W) { cin >> field[i][j]; }
    }

    string ans = "";
    rep(y, H) {
      rep(x, W) { ans = big2(ans, rec(W - x - 1, H - y - 1)); }
    }

    cout << (ans != "" ? ans : "0") << endl;
  }
}

Error: WA

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

#define VARIABLE(x) cerr << #x << "=" << x << endl
#define BINARY(x) static_cast<bitset<16>>(x);
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, m, n) for (int i = m; i < (int)(n); i++)
#define if_range(x, y, w, h)                                                   \\
  if (0 <= (int)(x) && (int)(x) < (int)(w) && 0 <= (int)(y) &&                 \\
      (int)(y) < (int)(h))

const int INF = 100000000;
typedef double D;
const double EPS = 1e-8;
const double PI = 3.14159;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, -1, 0, 1};
using namespace std;
typedef pair<int, int> P;

/** Problem1126 : The Secret Number **/
const int MAX_W = 40;
int W, H;
char field[MAX_W][MAX_W];
string dp[MAX_W][MAX_W];
bool vis[MAX_W][MAX_W];

string big(string s1, string s2) {
  if (s1.length() > s2.length()) {
    return s1;
  } else if (s2.length() > s1.length()) {
    return s2;
  } else {
    return (s1 > s2 ? s1 : s2);
  }
}

string rec(int x, int y, string n) {
  if_range(x, y, W, H) {
    if ('0' <= field[y][x] && field[y][x] <= '9')
      n += field[y][x];
    else
      return dp[y][x] = n;
  }
  else return dp[y][x] = n;

  if (dp[y][x] != "")
    return dp[y][x];

  string s1 = rec(x + 1, y, n);
  string s2 = rec(x, y + 1, n);

  return dp[y][x] = big(s1, s2);
}

int main() {
  while (cin >> W >> H, W || H) {
    fill(dp[0], dp[0] + W * H, "");
    fill(vis[0], vis[0] + W * H, false);

    rep(i, H) {
      rep(j, W) { cin >> field[i][j]; }
    }

    string ans = "0";
    rep(y, H) {
      rep(x, W) {
        if (!vis[y][x]) {
          ans = big(ans, rec(x, y, ""));
          vis[y][x] = true;
        }
      }
    }

    cout << ans << endl;
  }
}