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

#include <cassert>
#include <iostream>
#include <vector>
using namespace std;

vector<int> fall(vector<int> B, vector<int> A) {
  int hb = B.size(), ha = A.size();
  B.push_back(0);
  B.push_back(0);
  for (int i = hb; i >= 0; --i) {
    bool f = false;
    if (i > 0) {
      for (int j = 0; j < ha; ++j) {
        if (B[i - 1 + j] & A[j]) {
          f = true;
          break;
        }
      }
    } else {
      f = true;
    }
    if (f) {
      for (int j = 0; j < ha; ++j) {
        B[i + j] |= A[j];
      }
      while (B.back() == 0)
        B.pop_back();
      return B;
    }
  }
  assert(false);
  return B;
}

int erase(vector<int> &B) {
  vector<int> B_;
  int ret = 0;
  for (auto b : B) {
    if (b == 15) {
      ++ret;
      continue;
    }
    B_.push_back(b);
  }
  swap(B, B_);
  return ret;
}

// 0 1
// 2 3

int dfs(vector<int> B, const vector<vector<int>> &C, int idx) {
  if (idx == C.size())
    return 0;

  vector<int> c = C[idx];
  vector<vector<int>> c_(c.size());
  for (size_t i = 0; i < c.size(); ++i) {
    for (int j = 0; j < 4; ++j) {
      if ((c[i] >> j) & 1)
        c_[i].push_back(j);
    }
  }

  int ret = 0;
  int mask_x[] = {5, 0, 10}, mask_y[] = {3, 0, 12};
  int dx[] = {-1, 0, 1}, dy[] = {-2, 0, 2};
  for (int i = 0; i < 3; ++i) {
    {
      bool f = true;
      for (auto e : c) {
        if (e & mask_x[i]) {
          f = false;
          break;
        }
      }
      if (!f)
        continue;
    }
    for (int j = 0; j < 3; ++j) {
      {
        bool f = true;
        for (auto e : c) {
          if (e & mask_y[j]) {
            f = false;
            break;
          }
        }
        if (!f)
          continue;
      }
      vector<int> cc;
      for (auto e : c_) {
        int t = 0;
        for (auto ee : e) {
          ee += dx[i] + dy[j];
          t |= 1 << ee;
        }
        cc.push_back(t);
      }
      auto B_ = fall(B, cc);
      int ret_ = erase(B_);
      ret_ += dfs(B_, C, idx + 1);
      ret = max(ret, ret_);
    }
  }
  return ret;
}

int main() {
  int H, N;
  while (cin >> H >> N, H) {
    vector<int> B;
    for (int i = 0; i < H; ++i) {
      int t = 0;
      for (int j = 0; j < 2; ++j) {
        string s;
        cin >> s;
        for (int k = 0; k < 2; ++k) {
          if (s[k] == '#')
            t |= 1 << (j * 2 + k);
        }
      }
      B.push_back(t);
    }
    vector<vector<int>> C;
    for (int i = 0; i < N; ++i) {
      vector<int> b;
      for (int j = 0; j < 2; ++j) {
        int t = 0;
        for (int k = 0; k < 2; ++k) {
          string s;
          cin >> s;
          for (int l = 0; l < 2; ++l) {
            if (s[l] == '#')
              t |= 1 << (k * 2 + l);
          }
        }
        if (t)
          b.push_back(t);
      }
      C.push_back(b);
    }
    int ans = dfs(B, C, 0);
    cout << ans << endl;
  }
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <stack>
#include <vector>

using namespace std;

int main() {
  int N, H;
  const int l = 5, r = 10, u = 3, d = 12;
  while (cin >> H >> N, H) {
    stack<int> st;
    for (int i = 0; i < H; ++i) {
      int t = 0;
      for (int j = 0; j < 2; ++j) {
        for (int k = 0; k < 2; ++k) {
          char c;
          cin >> c;
          t |= (c == '#') << (2 * j + k);
        }
      }
      st.push(t);
    }
    vector<vector<int>> B(N, vector<int>(2, 0));
    for (int i = 0; i < N; ++i) {
      for (int j = 0; j < 2; ++j) {
        for (int k = 0; k < 2; ++k) {
          for (int l = 0; l < 2; ++l) {
            char c;
            cin >> c;
            B[i][j] |= (c == '#') << (2 * k + l);
          }
        }
      }
    }
    map<stack<int>, int> M;
    M[st] = 0;
    for (int i = 0; i < N; ++i) {
      int t = B[i][0] | B[i][1];
      vector<vector<int>> T;
      T.push_back(B[i]);
      if ((t & l) == 0) {
        vector<int> L = B[i];
        L[0] /= 2;
        L[1] /= 2;
        T.push_back(L);
      }
      if ((t & r) == 0) {
        vector<int> R = B[i];
        R[0] *= 2;
        R[1] *= 2;
        T.push_back(R);
      }
      int n = T.size();
      for (int j = 0; j < n; ++j) {
        t = T[j][0] | T[j][1];
        if ((t & u) == 0) {
          vector<int> U = T[j];
          U[0] /= 4;
          U[1] /= 4;
          T.push_back(U);
        }
        if ((t & d) == 0) {
          vector<int> D = T[j];
          D[0] *= 4;
          D[1] *= 4;
          T.push_back(D);
        }
      }
      map<stack<int>, int> M_;
      for (auto itr = M.begin(); itr != M.end(); ++itr) {
        for (int j = 0; j < (int)T.size(); ++j) {
          stack<int> st_ = itr->first;
          int t = itr->second;
          if ((st_.top() & T[j][0]) == 0) {
            st_.top() |= T[j][0];
            if (st_.top() == 15) {
              ++t;
              st_.pop();
              if ((st_.top() & T[j][1]) == 0) {
                st_.top() |= T[j][1];
                if (st_.top() == 15) {
                  ++t;
                  st_.pop();
                }
              } else {
                st_.push(T[j][1]);
                if (st_.top() == 15) {
                  ++t;
                  st_.pop();
                }
              }
            } else {
              if ((!T[j][0]) && (!(st_.top() & T[j][1])))
                st_.top() |= T[j][1];
              else
                st_.push(T[j][1]);
              if (st_.top() == 15) {
                st_.pop();
                ++t;
              }
            }
          } else {
            st_.push(T[j][0]);
            if (st_.top() == 15) {
              ++t;
              st_.pop();
              if ((st_.top() & T[j][1]) == 0) {
                st_.top() |= T[j][1];
                if (st_.top() == 15) {
                  ++t;
                  st_.pop();
                }
              } else {
                st_.push(T[j][1]);
                if (st_.top() == 15) {
                  ++t;
                  st_.pop();
                }
              }
            } else {
              st_.push(T[j][1]);
              if (st_.top() == 15) {
                st_.pop();
                ++t;
              }
            }
          }
          M_[st_] = max(M_[st_], t);
        }
      }
      M = M_;
    }
    int ans = 0;
    for (auto itr = M.begin(); itr != M.end(); ++itr) {
      ans = max(ans, itr->second);
    }
    cout << ans << endl;
  }
  return 0;
}