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

#include <bits/stdc++.h>
using namespace std;

#define int long long
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<pint> vpint;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, f, n) for (int i = (f); i < (n); i++)
#define all(v) (v).begin(), (v).end()
#define each(it, v)                                                            \\
  for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
#define pb push_back
#define fi first
#define se second
template <typename A, typename B> inline void chmin(A &a, B b) {
  if (a > b)
    a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
  if (a < b)
    a = b;
}

int H, W;
vector<pair<pint, pint>> lis;
string fld[55];

bool vis[55][55][55][55];
int dp[3000][55][55];
int memo[3000]; // 26*10*10
int dfs(int v) {
  int &ret = memo[v];
  if (ret != -1)
    return ret;

  ret = 0;
  fill_n(*dp[v], 55 * 55, -1001001001);
  dp[v][lis[v].fi.fi][lis[v].fi.se] = 0;
  int k = 0;
  reps(i, lis[v].fi.fi, lis[v].se.fi + 1) {
    reps(j, lis[v].fi.se, lis[v].se.se + 1) {
      if (fld[i][j] == '#')
        continue;
      if (i + 1 <= lis[v].se.fi && fld[i + 1][j] != '#')
        chmax(dp[v][i + 1][j], dp[v][i][j]);
      if (j + 1 <= lis[v].se.se && fld[i][j + 1] != '#')
        chmax(dp[v][i][j + 1], dp[v][i][j]);

      while (k < lis.size()) {
        bool ok = true;
        if (lis[k].fi < pint(i, j))
          ok = false;
        if (lis[k].fi == pint(i, j) &&
            (lis[k].se.fi > lis[v].se.fi || lis[k].se.se > lis[v].se.se))
          ok = false;
        if (lis[k].fi == lis[v].fi || lis[k].se == lis[v].se)
          ok = false;
        if (!ok) {
          k++;
          continue;
        }
        if (lis[k].fi == pint(i, j)) {
          int tmp = dp[v][i][j] + dfs(k) + 1;
          if (lis[k].se.fi + 1 <= lis[v].se.fi)
            chmax(dp[v][lis[k].se.fi + 1][lis[k].se.se], tmp);
          if (lis[k].se.se + 1 <= lis[v].se.se)
            chmax(dp[v][lis[k].se.fi][lis[k].se.se + 1], tmp);
          k++;
        } else
          break;
      }
    }
  }

  return ret = dp[v][lis[v].se.fi][lis[v].se.se];
}

signed main() {
  while (cin >> H >> W, H || W) {
    rep(i, H) cin >> fld[i + 1];
    fld[0] = fld[H + 1] = string(W, '#');
    fld[0][0] = fld[H + 1][W - 1] = '.';
    H += 2;

    memset(vis, 0, sizeof(vis));
    rep(a, H) rep(b, W) {
      if (fld[a][b] == '#')
        continue;
      vis[a][b][a][b] = true;
      rep(c, H) rep(d, W) {
        if (!vis[a][b][c][d])
          continue;
        if (c + 1 < H && fld[c + 1][d] != '#')
          vis[a][b][c + 1][d] = true;
        if (d + 1 < W && fld[c][d + 1] != '#')
          vis[a][b][c][d + 1] = true;
      }
    }

    if (!vis[0][0][H - 1][W - 1]) {
      cout << -1 << endl;
      continue;
    }

    lis.clear();
    lis.pb(make_pair(pint(0, 0), pint(H - 1, W - 1)));
    rep(a, H) rep(b, W) rep(c, H) rep(d, W) {
      if (a > c || b > d)
        continue;
      if (!islower(fld[a][b]) || !isupper(fld[c][d]))
        continue;
      if (toupper(fld[a][b]) != fld[c][d])
        continue;
      if (vis[a][b][c][d]) {
        lis.pb(make_pair(pint(a, b), pint(c, d)));
      }
    }

    sort(all(lis));
    memset(memo, -1, sizeof(memo));
    cout << dfs(0) << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define int long long
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<pint> vpint;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, f, n) for (int i = (f); i < (n); i++)
#define all(v) (v).begin(), (v).end()
#define each(it, v)                                                            \\
  for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
#define pb push_back
#define fi first
#define se second
template <typename A, typename B> inline void chmin(A &a, B b) {
  if (a > b)
    a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
  if (a < b)
    a = b;
}

int H, W;
vector<pair<pint, pint>> lis;
string fld[55];

bool vis[50][50][50][50];

inline bool reach(const pint &s, const pint &t) {
  return vis[s.fi][s.se][t.fi][t.se];
}

int dp[3000][55][55];
int memo[3000]; // 26*10*10
int dfs(int v) {
  int &ret = memo[v];
  if (ret != -1)
    return ret;

  ret = 0;
  fill_n(*dp[v], 55 * 55, -1001001001);
  dp[v][lis[v].fi.fi][lis[v].fi.se] = 0;
  int k = 0;
  reps(i, lis[v].fi.fi, lis[v].se.fi + 1) {
    reps(j, lis[v].fi.se, lis[v].se.se + 1) {
      if (fld[i][j] == '#')
        continue;
      if (i + 1 <= lis[v].se.fi && fld[i + 1][j] != '#')
        chmax(dp[v][i + 1][j], dp[v][i][j]);
      if (j + 1 <= lis[v].se.se && fld[i][j + 1] != '#')
        chmax(dp[v][i][j + 1], dp[v][i][j]);

      while (k < lis.size()) {
        bool ok = true;
        if (lis[k].fi < pint(i, j))
          ok = false;
        if (lis[k].fi == pint(i, j) &&
            (lis[k].se.fi > lis[v].se.fi || lis[k].se.se > lis[v].se.se))
          ok = false;
        if (!ok) {
          k++;
          continue;
        }
        if (lis[k].fi == pint(i, j)) {
          int tmp = dp[v][i][j] + dfs(k) + 1;
          if (lis[k].se.fi + 1 <= lis[v].se.fi)
            chmax(dp[v][lis[k].se.fi + 1][lis[k].se.se], tmp);
          if (lis[k].se.se + 1 <= lis[v].se.se)
            chmax(dp[v][lis[k].se.fi][lis[k].se.se + 1], tmp);
          k++;
        } else
          break;
      }
    }
  }

  return ret = dp[v][lis[v].se.fi][lis[v].se.se];
}

signed main() {
  while (cin >> H >> W, H || W) {
    rep(i, H) cin >> fld[i + 1];
    fld[0] = fld[H + 1] = string(W, '#');
    fld[0][0] = fld[H + 1][W - 1] = '.';
    H += 2;

    memset(vis, 0, sizeof(vis));
    rep(a, H) rep(b, W) {
      if (fld[a][b] == '#')
        continue;
      vis[a][b][a][b] = true;
      rep(c, H) rep(d, W) {
        if (!vis[a][b][c][d])
          continue;
        if (c + 1 < H && fld[c + 1][d] != '#')
          vis[a][b][c + 1][d] = true;
        if (d + 1 < W && fld[c][d + 1] != '#')
          vis[a][b][c][d + 1] = true;
      }
    }

    if (!vis[0][0][H - 1][W - 1]) {
      cout << -1 << endl;
      continue;
    }

    lis.clear();
    lis.pb(make_pair(pint(0, 0), pint(H - 1, W - 1)));
    rep(a, H) rep(b, W) rep(c, H) rep(d, W) {
      if (a > c || b > d)
        continue;
      if (!islower(fld[a][b]) || !isupper(fld[c][d]))
        continue;
      if (toupper(fld[a][b]) != fld[c][d])
        continue;
      if (vis[a][b][c][d]) {
        lis.pb(make_pair(pint(a, b), pint(c, d)));
      }
    }

    sort(all(lis));

    memset(memo, -1, sizeof(memo));
    cout << dfs(0) << endl;
  }
  return 0;
}