Problem p00132 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const int INF = 1 << 28;

class Piece {
private:
  int h, w;
  bool g[20][20];
  vector<int> bit;

public:
  Piece(){};

  bool readFromStdin(bool cutflag = true) {
    if (cin >> h >> w && (h | w))
      ;
    else
      return false;
    for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
        char c;
        cin >> c;
        g[i][j] = (c == '#');
      }
    }
    if (cutflag)
      cutSpace();
    makeBitVector();
    return true;
  }

  void cutSpace() {
    int minI, minJ, maxI, maxJ;
    minI = minJ = INF;
    maxI = maxJ = -INF;
    for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
        if (g[i][j]) {
          minI = min(minI, i);
          minJ = min(minJ, j);
          maxI = max(maxI, i);
          maxJ = max(maxJ, j);
        }
      }
    }
    h = maxI - minI + 1;
    w = maxJ - minJ + 1;
    for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
        g[i][j] = g[minI + i][minJ + j];
      }
    }
  }

  void makeBitVector() {
    bit.clear();
    for (int i = 0; i < h; ++i) {
      int b = 0;
      for (int j = 0; j < w; ++j) {
        b |= g[i][j] << j;
      }
      bit.push_back(b);
    }
  }

  Piece rotate() const {
    Piece p;
    p.h = w;
    p.w = h;
    for (int i = 0; i < p.h; ++i) {
      for (int j = 0; j < p.w; ++j) {
        p.g[i][j] = g[h - j - 1][i];
      }
    }
    p.makeBitVector();
    return p;
  }

  bool canPut(const Piece &p, int pi, int pj) const {
    if (pi < 0 || pi + p.h > h)
      return false;
    if (pj < 0 || pj + p.w > w)
      return false;
    for (int i = 0; i < p.h; ++i) {
      if (bit[pi + i] & (p.bit[i] << pj))
        return false;
    }
    return true;
  }

  void put(const Piece &p, int pi, int pj) {
    for (int i = 0; i < p.h; ++i) {
      bit[pi + i] |= p.bit[i] << pj;
    }
  }

  void erase(const Piece &p, int pi, int pj) {
    for (int i = 0; i < p.h; ++i) {
      bit[pi + i] &= ~(p.bit[i] << pj);
    }
  }

  int getH() const { return h; }
  int getW() const { return w; }
  int getSize() const { return h * w; }
  bool getDot(int i, int j) const { return (bit[i] >> j) & 1; }
  int getCount() const {
    int res = 0;
    for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
        res += g[i][j];
      }
    }
    return res;
  }

  void show() const {
    for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
        cout << g[i][j] << " ";
      }
      cout << endl;
    }
  }
};

const int MAXN = 10;
int h, w;
Piece base;
int N;
Piece ps[MAXN][4];

bool rec(vector<int>::const_iterator it,
         const vector<int>::const_iterator end) {
  if (it == end)
    return true;
  {
    Piece b = base;
    for (int ni = 0; ni < h; ++ni) {
      for (int nj = 0; nj < w; ++nj) {
        if (!b.getDot(ni, nj)) {
          for (vector<int>::const_iterator a = it; a != end; ++a) {
            for (int k = 0; k < 4; ++k) {
              const Piece &p = ps[*a][k];
              int ph = p.getH();
              int pw = p.getW();
              for (int i = max(0, ni - ph + 1); i <= ni && i + ph <= h; ++i) {
                for (int j = max(0, nj - pw + 1); j <= nj && j + pw <= w; ++j) {
                  if (base.canPut(p, i, j)) {
                    b.put(p, i, j);
                  }
                }
              }
            }
          }
        }
        if (!b.getDot(ni, nj))
          return false;
      }
    }
  }

  for (int k = 0; k < 4; ++k) {
    const Piece &p = ps[*it][k];
    int ph = p.getH();
    int pw = p.getW();
    for (int i = 0; i + ph <= h; ++i) {
      for (int j = 0; j + pw <= w; ++j) {
        if (base.canPut(p, i, j)) {
          base.put(p, i, j);
          bool res = rec(it + 1, end);
          base.erase(p, i, j);
          if (res)
            return true;
        }
      }
    }
  }
  return false;
}

int main() {
  while (base.readFromStdin(false)) {
    h = base.getH();
    w = base.getW();
    cin >> N;
    for (int i = 0; i < N; ++i) {
      ps[i][0].readFromStdin();
      for (int j = 1; j < 4; ++j) {
        ps[i][j] = ps[i][j - 1].rotate();
      }
    }
    int M;
    cin >> M;
    while (M--) {
      vector<int> v;
      {
        int k;
        cin >> k;
        vector<pair<int, int>> tmp(k);
        for (int i = 0; i < k; ++i) {
          cin >> tmp[i].second;
          --tmp[i].second;
          tmp[i].first = -ps[tmp[i].second][0].getSize();
        }
        sort(tmp.begin(), tmp.end());
        v.resize(k);
        for (int i = 0; i < k; ++i) {
          v[i] = tmp[i].second;
        }
      }
      int sum = 0;
      for (int i = 0; i < v.size(); ++i) {
        sum += ps[v[i]][0].getCount();
      }
      if (sum == base.getSize() - base.getCount() && rec(v.begin(), v.end())) {
        cout << "YES" << endl;
      } else {
        cout << "NO" << endl;
      }
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

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

const int INF = 1 << 28;

class Piece {
private:
  int h, w;
  bool g[20][20];
  vector<int> bit;

public:
  Piece(){};

  bool readFromStdin(bool cutflag = true) {
    if (cin >> h >> w && (h | w))
      ;
    else
      return false;
    for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
        char c;
        cin >> c;
        g[i][j] = (c == '#');
      }
    }
    if (cutflag)
      cutSpace();
    makeBitVector();
    return true;
  }

  void cutSpace() {
    int minI, minJ, maxI, maxJ;
    minI = minJ = INF;
    maxI = maxJ = -INF;
    for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
        if (g[i][j]) {
          minI = min(minI, i);
          minJ = min(minJ, j);
          maxI = max(maxI, i);
          maxJ = max(maxJ, j);
        }
      }
    }
    h = maxI - minI + 1;
    w = maxJ - minJ + 1;
    for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
        g[i][j] = g[minI + i][minJ + j];
      }
    }
  }

  void makeBitVector() {
    bit.clear();
    for (int i = 0; i < h; ++i) {
      int b = 0;
      for (int j = 0; j < w; ++j) {
        b |= g[i][j] << j;
      }
      bit.push_back(b);
    }
  }

  Piece rotate() const {
    Piece p;
    p.h = w;
    p.w = h;
    for (int i = 0; i < p.h; ++i) {
      for (int j = 0; j < p.w; ++j) {
        p.g[i][j] = g[h - j - 1][i];
      }
    }
    p.makeBitVector();
    return p;
  }

  bool canPut(const Piece &p, int pi, int pj) const {
    if (pi < 0 || pi + p.h > h)
      return false;
    if (pj < 0 || pj + p.w > w)
      return false;
    for (int i = 0; i < p.h; ++i) {
      if (bit[pi + i] & (p.bit[i] << pj))
        return false;
    }
    return true;
  }

  void put(const Piece &p, int pi, int pj) {
    for (int i = 0; i < p.h; ++i) {
      bit[pi + i] ^= p.bit[i] << pj;
    }
  }

  int getH() const { return h; }
  int getW() const { return w; }
  int getSize() const { return h * w; }
  int getCount() const {
    int res = 0;
    for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
        res += g[i][j];
      }
    }
    return res;
  }

  void show() const {
    for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
        cout << g[i][j] << " ";
      }
      cout << endl;
    }
  }
};

const int MAXN = 10;
int h, w;
Piece base;
int N;
Piece ps[MAXN][4];

bool rec(vector<int>::const_iterator it,
         const vector<int>::const_iterator end) {
  if (it == end)
    return true;
  for (int k = 0; k < 4; ++k) {
    const Piece &p = ps[*it][k];
    int ph = p.getH();
    int pw = p.getW();
    for (int i = 0; i + ph <= h; ++i) {
      for (int j = 0; j + pw <= w; ++j) {
        if (base.canPut(p, i, j)) {
          base.put(p, i, j);
          bool res = rec(it + 1, end);
          base.put(p, i, j);
          if (res)
            return true;
        }
      }
    }
  }
  return false;
}

int main() {
  while (base.readFromStdin(false)) {
    h = base.getH();
    w = base.getW();
    cin >> N;
    for (int i = 0; i < N; ++i) {
      ps[i][0].readFromStdin();
      for (int j = 1; j < 4; ++j) {
        ps[i][j] = ps[i][j - 1].rotate();
      }
    }
    int M;
    cin >> M;
    while (M--) {
      vector<int> v;
      {
        int k;
        cin >> k;
        vector<pair<int, int>> tmp(k);
        for (int i = 0; i < k; ++i) {
          cin >> tmp[i].second;
          --tmp[i].second;
          tmp[i].first = -ps[tmp[i].second][0].getSize();
        }
        sort(tmp.begin(), tmp.end());
        v.resize(k);
        for (int i = 0; i < k; ++i) {
          v[i] = tmp[i].second;
        }
      }
      int sum = 0;
      for (int i = 0; i < v.size(); ++i) {
        sum += ps[v[i]][0].getCount();
      }
      if (sum == base.getSize() - base.getCount() && rec(v.begin(), v.end())) {
        cout << "YES" << endl;
      } else {
        cout << "NO" << endl;
      }
    }
  }
  return 0;
}