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

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

#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))

typedef long long LL;

int main() {
  int h, n;
  char c;
  while (1) {
    cin >> h >> n;
    if (h == 0)
      return 0;
    vector<vector<vector<int>>> field(
        h + 2 * n + 1, vector<vector<int>>(2, vector<int>(2, 0)));
    vector<vector<vector<vector<int>>>> block(
        n, vector<vector<vector<int>>>(
               2, vector<vector<int>>(2, vector<int>(2, 0))));
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < 2; j++) {
        for (int k = 0; k < 2; k++) {
          cin >> c;
          if (c == '#')
            field[i][j][k] = 1;
        }
      }
    }
    vector<int> x(2), y(2);
    vector<int> X(3, 1), Y(3, 1);
    for (int p = 0; p < n; p++) {
      x[0] = x[1] = y[0] = y[1] = 0;
      for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
          for (int k = 0; k < 2; k++) {
            cin >> c;
            if (c == '#') {
              block[p][i][j][k] = 1;
              x[j] = 1;
              y[k] = 1;
            }
          }
        }
      }
      if (x[0] == 0 && x[1] == 1) {
        for (int i = 0; i < 2; i++) {
          for (int j = 0; j < 2; j++) {
            block[p][i][0][j] = block[p][i][1][j];
            block[p][i][1][j] = 0;
          }
        }
      }
      if (y[0] == 0 && y[1] == 1) {
        for (int i = 0; i < 2; i++) {
          for (int j = 0; j < 2; j++) {
            block[p][i][j][0] = block[p][i][j][1];
            block[p][i][j][1] = 0;
          }
        }
      }
      X[p] = 3 - x[0] - x[1];
      Y[p] = 3 - y[0] - y[1];
      // cout << "block" << p << endl;
      // for(int i=0;i<2;i++){
      //   for(int j=0;j<2;j++){
      //     for(int k=0;k<2;k++){
      //       cout << block[p][i][j][k] << " ";
      //     }
      //     cout << endl;
      //   }
      // }
    }
    vector<vector<vector<int>>> f;
    bool ok;
    int tmp;
    int ans = 0;
    int now;
    int maxh;
    int fh;
    bool okflag;
    vector<int> fl(h + 2 * n + 1);
    vector<vector<vector<vector<int>>>> b(
        n, vector<vector<vector<int>>>(
               2, vector<vector<int>>(2, vector<int>(2, 0))));
    vector<int> xx(3, 0), yy(3, 0);
    // cout <<"XY" << X[0] << Y[0] << endl;
    // cout <<"XY" << X[1] << Y[1] << endl;
    for (int I = 0; I < (1 << n); I++) {
      for (int K = 0; K < (1 << n); K++) {
        xx[0] = I & 1;
        xx[1] = (I & 2) / 2;
        xx[2] = (I & 4) / 4;
        yy[0] = K & 1;
        yy[1] = (K & 2) / 2;
        yy[2] = (K & 4) / 4;
        okflag = true;
        // cout << "(" <<  I << " " << K << ")" << endl;
        for (int i = 0; i < 3; i++) {
          if (xx[i] >= X[i])
            okflag = false;
          if (yy[i] >= Y[i])
            okflag = false;
        }
        if (!okflag)
          continue;
        // cout << "XXYY" << xx[1] << yy[1] << endl;
        tmp = 0;
        f = field;
        b = block;
        // for(int i=0;i<2;i++){
        //   for(int j=0;j<2;j++){
        //     cout << f[0][i][j];
        //   }
        //   cout << endl;
        // }
        for (int B = 0; B < n; B++) {
          if (xx[B] == 1) {
            for (int i = 0; i < 2; i++) {
              for (int j = 0; j < 2; j++) {
                b[B][i][1][j] = b[B][i][0][j];
                b[B][i][0][j] = 0;
              }
            }
          }
          if (yy[B] == 1) {
            for (int i = 0; i < 2; i++) {
              for (int j = 0; j < 2; j++) {
                b[B][i][j][1] = b[B][i][j][0];
                b[B][i][j][0] = 0;
              }
            }
          }
          // cout << "block" << B << endl;
          // for(int i=0;i<2;i++){
          //   for(int j=0;j<2;j++){
          //     for(int k=0;k<2;k++){
          //       cout << b[B][i][j][k] << " ";
          //     }
          //     cout << endl;
          //   }
          // }
        }
        for (int i = 0; i < n; i++) {
          maxh = -1;
          for (int j = 0; j < 2; j++) {
            for (int k = 0; k < 2; k++) {
              fh = -1;
              for (int l = 0; l < h + 2 * n + 1; l++) {
                if (f[l][j][k] == 1)
                  fh = l;
              }
              // cout << fh << endl;
              if (b[i][0][j][k]) {
                maxh = max(maxh, fh + 1);
              } else if (b[i][1][j][k]) {
                maxh = max(maxh, fh);
              }
            }
          }
          // cout << "maxh:" <<  maxh << endl;
          // cout << i+1 << "kome" << endl;
          // cout << maxh << endl;
          for (int j = 0; j < 2; j++) {
            for (int k = 0; k < 2; k++) {
              for (int l = 0; l < 2; l++) {
                if (maxh + j >= 0)
                  f[maxh + j][k][l] = (f[maxh + j][k][l] | b[i][j][k][l]);
              }
            }
          }
          for (int j = h + 2 * n - 1; j >= 0; j--) {
            // for(int a=0;a<2;a++){
            //   for(int b=0;b<2;b++){
            //     cout << f[j][a][b] << " ";
            //   }
            //   cout << endl;
            // }
            // cout << endl;
            fl[j] = 0;
            if (f[j][0][0] & f[j][0][1] & f[j][1][0] & f[j][1][1]) {
              fl[j] = 1;
              tmp++;
            }
          }
          now = 0;
          for (int j = 0; j < h + 2 * n - 1; j++) {
            if (!fl[j]) {
              f[now][0][0] = f[j][0][0];
              f[now][0][1] = f[j][0][1];
              f[now][1][0] = f[j][1][0];
              f[now][1][1] = f[j][1][1];
              now++;
            }
          }
        }
        // if(ans<tmp)cout << "!!!!!!!!!" << endl;
        ans = max(ans, tmp);
      }
    }
    cout << ans << endl;
  }
  return 0;
}


Error: -6
free(): invalid pointer


Fixed submission:

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

#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))

typedef long long LL;

int main() {
  int h, n;
  char c;
  while (1) {
    cin >> h >> n;
    if (h == 0)
      return 0;
    vector<vector<vector<int>>> field(
        h + 2 * n, vector<vector<int>>(2, vector<int>(2, 0)));
    vector<vector<vector<vector<int>>>> block(
        n, vector<vector<vector<int>>>(
               2, vector<vector<int>>(2, vector<int>(2, 0))));
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < 2; j++) {
        for (int k = 0; k < 2; k++) {
          cin >> c;
          if (c == '#')
            field[i][j][k] = 1;
        }
      }
    }
    vector<int> x(2), y(2);
    vector<int> X(3, 1), Y(3, 1);
    for (int p = 0; p < n; p++) {
      x[0] = x[1] = y[0] = y[1] = 0;
      for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
          for (int k = 0; k < 2; k++) {
            cin >> c;
            if (c == '#') {
              block[p][i][j][k] = 1;
              x[j] = 1;
              y[k] = 1;
            }
          }
        }
      }
      if (x[0] == 0 && x[1] == 1) {
        for (int i = 0; i < 2; i++) {
          for (int j = 0; j < 2; j++) {
            block[p][i][0][j] = block[p][i][1][j];
            block[p][i][1][j] = 0;
          }
        }
      }
      if (y[0] == 0 && y[1] == 1) {
        for (int i = 0; i < 2; i++) {
          for (int j = 0; j < 2; j++) {
            block[p][i][j][0] = block[p][i][j][0];
            block[p][i][j][0] = 0;
          }
        }
      }
      X[p] = 3 - x[0] - x[1];
      Y[p] = 3 - y[0] - y[1];
      // cout << "block" << p << endl;
      // for(int i=0;i<2;i++){
      //   for(int j=0;j<2;j++){
      //     for(int k=0;k<2;k++){
      //       cout << block[p][i][j][k] << " ";
      //     }
      //     cout << endl;
      //   }
      // }
    }
    vector<vector<vector<int>>> f;
    bool ok;
    int tmp;
    int ans = 0;
    int now;
    int maxh;
    int fh;
    bool okflag;
    vector<int> fl(h + 2 * n - 1);
    vector<vector<vector<vector<int>>>> b(
        n, vector<vector<vector<int>>>(
               2, vector<vector<int>>(2, vector<int>(2, 0))));
    vector<int> xx(3, 0), yy(3, 0);
    // cout <<"XY" << X[0] << Y[0] << endl;
    // cout <<"XY" << X[1] << Y[1] << endl;
    for (int I = 0; I < (1 << n); I++) {
      for (int K = 0; K < (1 << n); K++) {
        xx[0] = I & 1;
        xx[1] = (I & 2) / 2;
        xx[2] = (I & 4) / 4;
        yy[0] = K & 1;
        yy[1] = (K & 2) / 2;
        yy[2] = (K & 4) / 4;
        okflag = true;
        // cout << "(" <<  I << " " << K << ")" << endl;
        for (int i = 0; i < 3; i++) {
          if (xx[i] >= X[i])
            okflag = false;
          if (yy[i] >= Y[i])
            okflag = false;
        }
        if (!okflag)
          continue;
        // cout << "XXYY" << xx[1] << yy[1] << endl;
        tmp = 0;
        f = field;
        b = block;
        // for(int i=0;i<2;i++){
        //   for(int j=0;j<2;j++){
        //     cout << f[0][i][j];
        //   }
        //   cout << endl;
        // }
        for (int B = 0; B < n; B++) {
          if (xx[B] == 1) {
            for (int i = 0; i < 2; i++) {
              for (int j = 0; j < 2; j++) {
                b[B][i][1][j] = b[B][i][0][j];
                b[B][i][0][j] = 0;
              }
            }
          }
          if (yy[B] == 1) {
            for (int i = 0; i < 2; i++) {
              for (int j = 0; j < 2; j++) {
                b[B][i][j][1] = b[B][i][j][0];
                b[B][i][j][0] = 0;
              }
            }
          }
          // cout << "block" << B << endl;
          // for(int i=0;i<2;i++){
          //   for(int j=0;j<2;j++){
          //     for(int k=0;k<2;k++){
          //       cout << b[B][i][j][k] << " ";
          //     }
          //     cout << endl;
          //   }
          // }
        }
        for (int i = 0; i < n; i++) {
          maxh = -1;
          for (int j = 0; j < 2; j++) {
            for (int k = 0; k < 2; k++) {
              fh = -1;
              for (int l = 0; l < h + 2 * n - 1; l++) {
                if (f[l][j][k] == 1)
                  fh = l;
              }
              // cout << fh << endl;
              if (b[i][0][j][k]) {
                maxh = max(maxh, fh + 1);
              } else if (b[i][1][j][k]) {
                maxh = max(maxh, fh);
              }
            }
          }
          // cout << "maxh:" <<  maxh << endl;
          for (int j = 0; j < 2; j++) {
            for (int k = 0; k < 2; k++) {
              for (int l = 0; l < 2; l++) {
                if (maxh + j >= 0)
                  f[maxh + j][k][l] = (f[maxh + j][k][l] | b[i][j][k][l]);
              }
            }
          }
          for (int j = h + 2 * n - 1; j >= 0; j--) {
            // for(int a=0;a<2;a++){
            //   for(int b=0;b<2;b++){
            //     cout << f[j][a][b] << " ";
            //   }
            //   cout << endl;
            // }
            // cout << endl;
            fl[j] = 0;
            if (f[j][0][0] & f[j][0][1] & f[j][1][0] & f[j][1][1]) {
              fl[j] = 1;
              tmp++;
            }
          }
          now = 0;
          for (int j = 0; j < h + 2 * n - 1; j++) {
            if (!fl[j]) {
              f[now][0][0] = f[j][0][0];
              f[now][0][1] = f[j][0][1];
              f[now][1][0] = f[j][1][0];
              f[now][1][1] = f[j][1][1];
              now++;
            }
          }
        }
        ans = max(ans, tmp);
      }
    }
    cout << ans << endl;
  }
  return 0;
}