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

// ?????¬???????????¬??????

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define repq(i, a, n) for (int(i) = (a); (i) <= (n); (i)++)
#define repr(i, a, n) for (int(i) = (a); (i) >= (n); (i)--)
#define int long long int

template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chadd(T &a, T b) { a = a + b; }

typedef pair<int, int> pii;
typedef long long ll;

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
constexpr ll INF = 1001001001001001LL;
constexpr ll MOD = 1000000007LL;

int N;
int H[20], W[20];
int dp[1 << 16];
vector<string> board;
const string alph = "RGB";

int solve() {
  queue<int> q;
  q.push(0);
  while (!q.empty()) {
    int bit = q.front();
    q.pop();
    if (bit == (1 << 16) - 1)
      return dp[bit];
    // i ????????????????????????????????? (j, k) ?????????????????? s ??§??????
    rep(i, 0, N) rep(j, -H[i] + 1, 4) rep(k, -W[i] + 1, 4) rep(s, 0, 3) {
      int nbit = bit;
      rep(x, max(j, 0LL), min(j + H[i], 4LL))
          rep(y, max(k, 0LL), min(k + W[i], 4LL)) {
        int idx = x * 4 + y;
        if (board[x][y] == alph[s])
          nbit |= (1 << idx);
        else if (nbit >> idx & 1)
          nbit ^= (1 << idx);
      }
      if (dp[nbit] < 0) {
        dp[nbit] = dp[bit] + 1;
        q.push(nbit);
      }
    }
  }
  return 0;
}

signed main() {
  memset(dp, -1, sizeof(dp));
  dp[0] = 0;

  cin >> N;
  rep(i, 0, N) cin >> H[i] >> W[i];
  rep(i, 0, 4) {
    string s;
    cin >> s;
    board.push_back(s);
  }
  cout << solve() << endl;
  return 0;
}

Error: TLE

Fixed submission:

// ?????¬???????????¬??????

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define repq(i, a, n) for (int(i) = (a); (i) <= (n); (i)++)
#define repr(i, a, n) for (int(i) = (a); (i) >= (n); (i)--)
#define int long long int

template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chadd(T &a, T b) { a = a + b; }

typedef pair<int, int> pii;
typedef long long ll;

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
constexpr ll INF = 1001001001001001LL;
constexpr ll MOD = 1000000007LL;

int N;
int H[20], W[20];
int dp[1 << 16];
vector<string> board;
const string alph = "RGB";

int solve() {
  queue<int> q;
  q.push(0);
  while (!q.empty()) {
    int bit = q.front();
    q.pop();
    if (bit == (1 << 16) - 1)
      return dp[bit];
    // i ????????????????????????????????? (j, k) ?????????????????? s ??§??????
    rep(i, 0, N) rep(j, -4, 4) rep(k, -4, 4) rep(s, 0, 3) {
      int nbit = bit;
      rep(x, 0, H[i]) rep(y, 0, W[i]) {
        if (j + x < 0 || j + x >= 4 || k + y < 0 || k + y >= 4)
          continue;
        int idx = (j + x) * 4 + k + y;
        if (board[j + x][k + y] == alph[s])
          nbit |= (1 << idx);
        else if (nbit >> idx & 1)
          nbit ^= (1 << idx);
      }
      if (dp[nbit] < 0) {
        dp[nbit] = dp[bit] + 1;
        q.push(nbit);
      }
    }
  }
  return 0;
}

signed main() {
  memset(dp, -1, sizeof(dp));
  dp[0] = 0;

  cin >> N;
  rep(i, 0, N) cin >> H[i] >> W[i];
  rep(i, 0, 4) {
    string s;
    cin >> s;
    board.push_back(s);
  }
  cout << solve() << endl;
  return 0;
}