Problem p00822 (C++) — buggy submission, verdict: Memory Limit Exceeded

// include
//------------------------------------------
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);

bool dp[2][9][7 * 7 * 7 * 7];
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int N;
  while (cin >> N, N) {
    int crt = 0, nxt = 1;
    dp[crt][4][0] = true;
    FOR(i, 1, N + 1) {
      REP(j, 9) REP(k, 7 * 7 * 7 * 7) dp[nxt][j][k] = false;

      VI vi(16);
      REP(j, 16) cin >> vi[j];

      for (int idx = 0; idx < 9; ++idx) {
        for (int c = 0; c < 7 * 7 * 7 * 7; ++c) {
          if (!dp[crt][idx][c])
            continue;
          set<int> p;
          for (int x = idx / 3 * 3; x <= idx / 3 * 3 + 2; ++x)
            p.insert(x);
          for (int y = idx % 3; y < 9; y += 3)
            p.insert(y);
          VI ps_(4);
          int tmp = c;
          REP(j, 4) {
            ps_[j] = tmp % 7;
            tmp /= 7;
          }

          for (int idx_ : p) {
            if (i == 1 && idx_ != 4)
              continue;
            int orig;
            if (idx_ < 3)
              orig = idx_;
            else if (idx_ < 6)
              orig = 1 + idx_;
            else
              orig = 2 + idx_;
            if (vi[orig] || vi[orig + 1] || vi[orig + 4] || vi[orig + 5])
              continue;
            VI ps = ps_;

            if (idx_ != 0)
              ++ps[0];
            else
              ps[0] = 0;
            if (idx_ != 2)
              ++ps[1];
            else
              ps[1] = 0;
            if (idx_ != 6)
              ++ps[2];
            else
              ps[2] = 0;
            if (idx_ != 8)
              ++ps[3];
            else
              ps[3] = 0;

            if (ps[0] >= 7 || ps[1] >= 7 || ps[2] >= 7 || ps[3] >= 7)
              continue;
            tmp = 0;
            REP(j, 4) tmp = tmp * 7 + ps[3 - j];
            dp[nxt][idx_][tmp] = true;
          }
        }
      }
      swap(crt, nxt);
    }
    bool ok = false;
    for (int idx = 0; idx < 9; ++idx)
      for (int c = 0; c < 7 * 7 * 7 * 7; ++c)
        ok = ok || dp[crt][idx][c];

    cout << (ok ? 1 : 0) << endl;
  }

  return 0;
}

Error: MLE

Fixed submission:

// include
//------------------------------------------
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);

int di[5] = {1, -1, 4, -4, 0};
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int N;
  while (cin >> N, N) {
    queue<pair<int, VI>> q[2];
    VI tmp;
    tmp.PB(0);
    tmp.PB(0);
    tmp.PB(0);
    tmp.PB(0);
    int crt = 0, nxt = 1;
    q[crt].push(MP(5, tmp));
    REP(i, N) {
      //	  cout << endl<<i << ": " << endl;
      //	  cout << SZ(q[crt]) << endl;
      VI vi(16);
      REP(j, 16) cin >> vi[j];

      //	  REP(j,4){REP(k,4)cout<<vi[j*4+k]<<" ";cout<<endl;}cout<<endl;
      while (!q[crt].empty()) {
        auto res = q[crt].front();
        q[crt].pop();
        int idx = res.first;
        VI &buf = res.second;
        VI p;
        for (int x = idx / 4 * 4; x <= idx / 4 * 4 + 2; ++x)
          p.PB(x);
        for (int y = idx % 4; y <= idx % 4 + 8; y += 4)
          p.PB(y);

        for (int idx_ : p) {
          if (i == 0 && idx_ != 5)
            continue;
          if (idx_ < 0 || idx_ % 4 == 3 || idx_ >= 12)
            continue;
          if (vi[idx_] || vi[idx_ + 1] || vi[idx_ + 4] || vi[idx_ + 5])
            continue;
          VI xs = buf;
          if (idx_ != 0)
            ++xs[0];
          else
            xs[0] = 0;
          if (idx_ != 2)
            ++xs[1];
          else
            xs[1] = 0;
          if (idx_ != 8)
            ++xs[2];
          else
            xs[2] = 0;
          if (idx_ != 10)
            ++xs[3];
          else
            xs[3] = 0;

          if (xs[0] >= 7 || xs[1] >= 7 || xs[2] >= 7 || xs[3] >= 7)
            continue;
          q[nxt].push(MP(idx_, xs));
        }
      }
      swap(crt, nxt);
    }

    cout << (q[crt].empty() ? 0 : 1) << endl;
  }

  return 0;
}