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

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

using VI = vector<int>;
using VVI = vector<VI>;
using PII = pair<int, int>;
using LL = long long;
using VL = vector<LL>;
using VVL = vector<VL>;
using PLL = pair<LL, LL>;
using VS = vector<string>;

#define ALL(a) begin((a)), end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

#define FF first
#define SS second
template <class S, class T> istream &operator>>(istream &is, pair<S, T> &p) {
  return is >> p.FF >> p.SS;
}
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> &p) {
  return os << p.FF << " " << p.SS;
}
template <class T> void maxi(T &x, T y) {
  if (x < y)
    x = y;
}
template <class T> void mini(T &x, T y) {
  if (x > y)
    x = y;
}

const double EPS = 1e-10;
const double PI = acos(-1.0);
const LL MOD = 1e9 + 7;

int H, N;
int init[20][2][2];
int blk[8][2][2];

void fall(vector<VVI> &fd, vector<VVI> &bl) {
  int y;
  for (y = H + 6; y > 0; --y) {
    bool done = false;
    REP(dy, 2) REP(d, 2) REP(x, 2) {
      if (bl[dy][d][x] && fd[y + dy - 1][d][x])
        done = true;
    }
    if (done)
      break;
  }
  bool f = true;
  REP(d, 2) REP(x, 2) if (bl[0][d][x]) f = false;
  if (f && y == 0) {
    --y;
  }

  REP(dy, 2) REP(d, 2) REP(x, 2) {
    if (bl[dy][d][x])
      fd[y + dy][d][x] = 1;
  }
}
int del(vector<VVI> &fd) {
  int res = 0;
  for (int y = 19; y >= 0; --y) {
    bool fill = true;
    REP(d, 2) REP(x, 2) if (!fd[y][d][x]) fill = false;
    if (fill) {
      ++res;
      FOR(ty, y, 19) REP(d, 2) REP(x, 2) fd[ty][d][x] = fd[ty + 1][d][x];
      REP(d, 2) REP(x, 2) fd[19][d][x] = 0;
    }
  }
  return res;
}
int check(const vector<PII> &ps) {
  vector<VVI> fd(20, VVI(2, VI(2)));
  REP(y, 20) REP(d, 2) REP(x, 2) fd[y][d][x] = init[y][d][x];
  int res = 0;
  REP(i, N) {
    vector<VVI> vblk(2, VVI(2, VI(2)));
    REP(y, 2) REP(d, 2) REP(x, 2) if (blk[i * 2 + y][d][x]) {
      vblk[y][d + ps[i].FF][x + ps[i].SS] = 1;
    }
    fall(fd, vblk);
    res += del(fd);
  }
  return res;
}

int dfs(int i, vector<PII> &ps) {
  if (i == N)
    return check(ps);
  int res = 0;
  FOR(dd, -1, 2) FOR(dx, -1, 2) {
    bool ok = true;
    REP(y, 2) REP(d, 2) REP(x, 2) {
      if (blk[i * 2 + y][d][x]) {
        int td = d + dd, tx = x + dx;
        if (td < 0 || 2 <= td || tx < 0 || 2 <= tx)
          ok = false;
      }
    }
    if (!ok)
      continue;
    ps.EB(dd, dx);
    maxi(res, dfs(i + 1, ps));
    ps.pop_back();
  }
  return res;
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  while (cin >> H >> N, H) {
    fill((int *)init, (int *)init + 20 * 2 * 2, 0);
    fill((int *)blk, (int *)blk + 8 * 2 * 2, 0);
    REP(y, H) REP(d, 2) REP(x, 2) {
      char c;
      cin >> c;
      init[y][d][x] = c == '#';
    }
    REP(i, N * 2) REP(d, 2) REP(x, 2) {
      char c;
      cin >> c;
      blk[i][d][x] = c == '#';
    }
    vector<PII> ps;
    cout << dfs(0, ps) << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

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

using VI = vector<int>;
using VVI = vector<VI>;
using PII = pair<int, int>;
using LL = long long;
using VL = vector<LL>;
using VVL = vector<VL>;
using PLL = pair<LL, LL>;
using VS = vector<string>;

#define ALL(a) begin((a)), end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

#define FF first
#define SS second
template <class S, class T> istream &operator>>(istream &is, pair<S, T> &p) {
  return is >> p.FF >> p.SS;
}
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> &p) {
  return os << p.FF << " " << p.SS;
}
template <class T> void maxi(T &x, T y) {
  if (x < y)
    x = y;
}
template <class T> void mini(T &x, T y) {
  if (x > y)
    x = y;
}

const double EPS = 1e-10;
const double PI = acos(-1.0);
const LL MOD = 1e9 + 7;

int H, N;
int init[20][2][2];
int blk[8][2][2];

void fall(vector<VVI> &fd, vector<VVI> &bl) {
  int y;
  for (y = H + 6; y > 0; --y) {
    bool done = false;
    REP(dy, 2) REP(d, 2) REP(x, 2) {
      if (bl[dy][d][x] && fd[y + dy - 1][d][x])
        done = true;
    }
    if (done)
      break;
  }
  bool f = true;
  REP(d, 2) REP(x, 2) if (bl[0][d][x]) f = false;
  if (f && y == 0) {
    --y;
  }

  REP(dy, 2) REP(d, 2) REP(x, 2) {
    if (bl[dy][d][x])
      fd[y + dy][d][x] = 1;
  }
}
int del(vector<VVI> &fd) {
  int res = 0;
  for (int y = 19; y >= 0; --y) {
    bool fill = true;
    REP(d, 2) REP(x, 2) if (!fd[y][d][x]) fill = false;
    if (fill) {
      ++res;
      FOR(ty, y, 19) REP(d, 2) REP(x, 2) fd[ty][d][x] = fd[ty + 1][d][x];
      REP(d, 2) REP(x, 2) fd[19][d][x] = 0;
    }
  }
  return res;
}
int check(const vector<PII> &ps) {
  vector<VVI> fd(20, VVI(2, VI(2)));
  REP(y, 20) REP(d, 2) REP(x, 2) fd[y][d][x] = init[y][d][x];
  int res = 0;
  REP(i, N) {
    vector<VVI> vblk(2, VVI(2, VI(2)));
    REP(y, 2)
    REP(d, 2) REP(x, 2) if (blk[i * 2 + y][d][x])
        vblk[y][d + ps[i].FF][x + ps[i].SS] = 1;
    fall(fd, vblk);
    res += del(fd);
  }
  return res;
}

int dfs(int i, vector<PII> &ps) {
  if (i == N)
    return check(ps);
  int res = 0;
  FOR(dd, -1, 2) FOR(dx, -1, 2) {
    bool ok = true;
    REP(y, 2) REP(d, 2) REP(x, 2) {
      if (blk[i * 2 + y][d][x]) {
        int td = d + dd, tx = d + dx;
        if (td < 0 || 2 <= td || tx < 0 || 2 <= tx)
          ok = false;
      }
    }
    if (!ok)
      continue;
    ps.EB(dd, dx);
    maxi(res, dfs(i + 1, ps));
    ps.pop_back();
  }
  return res;
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  while (cin >> H >> N, H) {
    fill((int *)init, (int *)init + 20 * 2 * 2, 0);
    fill((int *)blk, (int *)blk + 8 * 2 * 2, 0);
    REP(y, H) REP(d, 2) REP(x, 2) {
      char c;
      cin >> c;
      init[y][d][x] = c == '#';
    }
    REP(i, N * 2) REP(d, 2) REP(x, 2) {
      char c;
      cin >> c;
      blk[i][d][x] = c == '#';
    }
    vector<PII> ps;
    cout << dfs(0, ps) << endl;
  }

  return 0;
}