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

#include <algorithm>
#include <cassert>
#include <iostream>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

vector<string> right_rot(vector<string> v) {
  vector<string> res;
  int h = v.size(), w = v[0].size();
  res.resize(w);
  rep(i, w) res[i].resize(h);
  rep(i, h) rep(j, w) res[j][h - 1 - i] = v[i][j];
  return res;
}

vector<int> vstovi(vector<string> v) {
  vector<int> res;
  rep(i, v.size()) {
    int tmp = 0;
    rep(j, v[i].size()) if (v[i][j] == '#') tmp |= 1 << j;
    res.push_back(tmp);
  }
  return res;
}

struct piece {
  int h[4], w[4], difx[4], dify[4];
  vector<int> p[4];
  int num, dir;

  piece(void) {}
  piece(vector<string> v) {
    int y = v.size(), x = v[0].size();

    num = 0;
    rep(i, y) rep(j, x) if (v[i][j] == '#') num++;

    dir = 0;
    rep(d, 4) {
      vector<int> vi = vstovi(v);
      bool f = true;
      rep(i, dir) {
        if (h[i] == v.size() && w[i] == v[0].size() && p[i] == vi) {
          f = false;
          break;
        }
      }

      if (f) {
        p[dir] = vi;
        h[dir] = v.size();
        w[dir] = v[0].size();
        rep(i, h[dir]) {
          rep(j, w[dir]) {
            if (v[i][j] == '#') {
              dify[dir] = i;
              difx[dir] = j;
              goto END;
            }
          }
        }
      END:;
        dir++;
      }
      v = right_rot(v);
    }
  }
};

int H, W;
vector<string> tmp;
vector<int> board;
int h, w, n, player;
piece p[10];
int k, id;
bool can[20][20][10][4];

void set(void) {
  rep(i, H) rep(j, W) {
    if ((board[i] >> j) & 1)
      continue;

    rep(z, n) rep(d, p[z].dir) {
      if (i + p[z].h[d] - 1 - p[z].dify[d] >= H)
        continue;
      if (i < p[z].dify[d])
        continue;
      if (j + p[z].w[d] - 1 - p[z].difx[d] >= W)
        continue;
      if (j < p[z].difx[d])
        continue;

      bool f = true;
      rep(ii, p[z].h[d]) {
        if ((board[i - p[z].dify[d] + ii] >> (j - p[z].difx[d])) &
            p[z].p[d][ii]) {
          f = false;
          goto END;
        }
      }
    END:;
      can[i][j][z][d] = f;
    }
  }
}

bool rec(int y, int x, int bit) {
  while (y < H && ((board[y] >> x) & 1)) {
    if (++x >= W) {
      x = 0;
      y++;
    }
  }
  if (y == H)
    return true;

  vector<int> hoge;

  rep(z, n) {
    if ((bit >> z) & 1) {
      rep(d, p[z].dir) {
        if (can[y][x][z][d]) {
          bool f = true;
          rep(i, p[z].h[d]) {
            if ((board[y + i - p[z].dify[d]] >> (x - p[z].difx[d])) &
                p[z].p[d][i]) {
              f = false;
              goto END;
            }
          }
        END:;
          if (f) {
            hoge = board;
            rep(i, p[z].h[d]) board[y + i - p[z].dify[d]] |=
                (p[z].p[d][i] << (x - p[z].difx[d]));
            f = rec(y, x, bit - (1 << z));
            board = hoge;
            if (f)
              return true;
          }
        }
      }
    }
  }
  return false;
}

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

  while (cin >> H >> W, H || W) {
    tmp.resize(H);
    int space = 0;
    rep(i, H) {
      cin >> tmp[i];
      rep(j, W) if (tmp[i][j] == '.') space++;
    }
    board = vstovi(tmp);

    cin >> n;
    rep(i, n) {
      cin >> h >> w;
      tmp.resize(h);
      rep(j, h) cin >> tmp[j];
      p[i] = piece(tmp);
    }

    rep(i, H) rep(j, W) { rep(z, n) rep(d, 4) can[i][j][z][d] = false; }
    set();

    cin >> player;
    rep(i, player) {
      cin >> k;
      int num = 0;

      int bit = 0;
      rep(j, k) {
        cin >> id;
        id--;
        bit |= (1 << id);
        num += p[id].num;
      }

      if (num != space) {
        cout << "NO\\n";
        continue;
      }

      if (rec(0, 0, bit))
        cout << "YES\\n";
      else
        cout << "NO\\n";
    }
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <iostream>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

void bin(int a) {
  rep(i, 20) {
    if ((a >> i) & 1)
      cerr << 1;
    else
      cerr << 0;
  }
  cerr << endl;
}

vector<string> right_rot(vector<string> v) {
  vector<string> res;
  int h = v.size(), w = v[0].size();
  res.resize(w);
  rep(i, w) res[i].resize(h);
  rep(i, h) rep(j, w) res[j][h - 1 - i] = v[i][j];
  return res;
}

vector<int> vstovi(vector<string> v) {
  vector<int> res;
  rep(i, v.size()) {
    int tmp = 0;
    rep(j, v[i].size()) if (v[i][j] == '#') tmp |= 1 << j;
    res.push_back(tmp);
  }
  return res;
}

struct piece {
  int h[4], w[4], difx[4], dify[4];
  vector<int> p[4];
  int num, dir;

  piece(void) {}
  piece(vector<string> v) {
    int y = v.size(), x = v[0].size();

    num = 0;
    rep(i, y) rep(j, x) if (v[i][j] == '#') num++;

    dir = 0;
    rep(d, 4) {
      vector<int> vi = vstovi(v);
      bool f = true;
      rep(i, dir) {
        if (h[i] == v.size() && w[i] == v[0].size() && p[i] == vi) {
          f = false;
          break;
        }
      }

      if (f) {
        p[dir] = vi;
        h[dir] = v.size();
        w[dir] = v[0].size();
        rep(i, h[dir]) {
          rep(j, w[dir]) {
            if (v[i][j] == '#') {
              dify[dir] = i;
              difx[dir] = j;
              goto END;
            }
          }
        }
      END:;
        if (dify[dir]) {
          rep(i, h[dir]) bin(p[dir][i]);
        }
        dir++;
      }
      v = right_rot(v);
    }
  }
};

int H, W;
vector<string> tmp;
vector<int> board;
int h, w, n, player;
piece p[10];
int k, id;
bool can[20][20][10][4];

void set(void) {
  rep(i, H) rep(j, W) {
    if ((board[i] >> j) & 1)
      continue;

    rep(z, n) rep(d, p[z].dir) {
      if (i + p[z].h[d] - 1 - p[z].dify[d] >= H)
        continue;
      if (i < p[z].dify[d])
        continue;
      if (j + p[z].w[d] - 1 - p[z].difx[d] >= W)
        continue;
      if (j < p[z].difx[d])
        continue;

      bool f = true;
      rep(ii, p[z].h[d]) {
        if ((board[i - p[z].dify[d] + ii] >> (j - p[z].difx[d])) &
            p[z].p[d][ii]) {
          f = false;
          goto END;
        }
      }
    END:;
      can[i][j][z][d] = f;
    }
  }
}

bool rec(int y, int x, int bit) {
  while (y < H && ((board[y] >> x) & 1)) {
    if (++x >= W) {
      x = 0;
      y++;
    }
  }
  if (y == H)
    return true;

  vector<int> hoge;

  rep(z, n) {
    if ((bit >> z) & 1) {
      rep(d, p[z].dir) {
        if (can[y][x][z][d]) {
          bool f = true;
          rep(i, p[z].h[d]) {
            if ((board[y + i - p[z].dify[d]] >> (x - p[z].difx[d])) &
                p[z].p[d][i]) {
              f = false;
              goto END;
            }
          }
        END:;
          if (f) {
            hoge = board;
            rep(i, p[z].h[d]) board[y + i - p[z].dify[d]] |=
                (p[z].p[d][i] << (x - p[z].difx[d]));
            f = rec(y, x, bit - (1 << z));
            board = hoge;
            if (f)
              return true;
          }
        }
      }
    }
  }
  return false;
}

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

  while (cin >> H >> W, H || W) {
    tmp.resize(H);
    int space = 0;
    rep(i, H) {
      cin >> tmp[i];
      rep(j, W) if (tmp[i][j] == '.') space++;
    }
    board = vstovi(tmp);

    cin >> n;
    rep(i, n) {
      cin >> h >> w;
      tmp.resize(h);
      rep(j, h) cin >> tmp[j];
      p[i] = piece(tmp);
    }

    rep(i, H) rep(j, W) { rep(z, n) rep(d, 4) can[i][j][z][d] = false; }
    set();
    /*
    rep(z,n)rep(d,4){
      cout << z << " " << d << endl;
      rep(i,H){
        rep(j,W){
          cout << can[i][j][z][d] << " ";
        }
        cout << endl;
      }
      cout << endl;
    }
    */

    cin >> player;
    rep(i, player) {
      cin >> k;
      int num = 0;

      int bit = 0;
      rep(j, k) {
        cin >> id;
        id--;
        bit |= (1 << id);
        num += p[id].num;
      }

      if (num != space) {
        cout << "NO\\n";
        continue;
      }

      if (rec(0, 0, bit))
        cout << "YES\\n";
      else
        cout << "NO\\n";
    }
  }
}