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

#include <bitset>
#include <cmath>
#include <iostream>
using namespace std;

#define N 10

class CMap {
public:
  int m[N + 2][N + 2];
  int ans[N + 2][N + 2];

  CMap() {
    Reset();
    ResetAnswer();
  }

  void Reset() {
    for (int i = 0; i < N + 2; ++i) {
      for (int j = 0; j < N + 2; ++j) {
        m[i][j] = 0;
      }
    }
  }
  void ResetAnswer() {
    for (int i = 0; i < N + 2; ++i) {
      for (int j = 0; j < N + 2; ++j) {
        ans[i][j] = 0;
      }
    }
  }
  bool inline static inRange(int t) { return (1 <= t && t < N + 1); }

  bool PassParticle(int i, int j, bool bConsiderEfficiency = false) {
    int cnt0 = 0;
    int cnt1 = 0;
    const int di[] = {0, -1, 0, 1, 0};
    const int dj[] = {0, 0, 1, 0, -1};

    if (bConsiderEfficiency) {
      for (int k = 0; k < sizeof(di) / sizeof(*di); ++k)
        if (inRange(i + di[k]) && inRange(j + dj[k]))
          if (m[i + di[k]][j + dj[k]] == 1)
            cnt1++;
          else
            cnt0++;
    }

    if (cnt1 < cnt0)
      return false;

    for (int k = 0; k < sizeof(di) / sizeof(*di); ++k)
      if (inRange(i + di[k]) && inRange(j + dj[k]))
        m[i + di[k]][j + dj[k]] ^= 1;

    ans[i][j] = 1;
    return true;
  }
  bool NoLight() const {
    for (int i = 1; i < N + 1; ++i)
      for (int j = 1; j < N + 1; ++j)
        if (m[i][j] == 1)
          return false;
    return true;
  }
  void Print() const {
    for (int i = 1; i < N + 1; ++i) {
      cout << m[i][1];
      for (int j = 2; j < N + 1; ++j)
        cout << ' ' << m[i][j];
      cout << '\\n';
    }
  }
  void PrintAnswer() const {
    for (int i = 1; i < N + 1; ++i) {
      cout << ans[i][1];
      for (int j = 2; j < N + 1; ++j)
        cout << ' ' << ans[i][j];
      cout << '\\n';
    }
  }
};

bool DetermineShootingPoints(CMap &M) {
  for (unsigned int i = 2; i < N + 1; ++i) {
    for (unsigned int j = 1; j < N + 1; ++j) {
      if (M.m[i - 1][j] == 1) {
        M.PassParticle(i, j);
      }
    }
  }
  if (M.NoLight())
    return true;
  else
    return false;
}

int main(void) {
  int n;

  cin >> n;
  for (int D = 0; D < n; ++D) {
    bool bCont = true;
    CMap M;
    bitset<N> B;

    for (int i = 1; i < N + 1; ++i)
      for (int j = 1; j < N + 1; ++j)
        cin >> M.m[i][j];

    for (unsigned long L = 0; L < (unsigned long)pow(2.0, (double)N) && bCont;
         ++L) {
      B = L;
      // cout << L << ": " << B.to_string() << endl;
      CMap Tmp = M;
      for (unsigned int bits = 0; bits < B.size() && bCont; ++bits) {
        if (B[bits])
          M.PassParticle(1, N - bits); // break;
      }
      if (DetermineShootingPoints(M)) {
        bCont = false;
        break;
      }
      M = Tmp;
    }
    // cout << "END.\\n";
    // cout << "CURRENT MAP:\\n";
    // M.Print();
    // cout << "ANSWER:\\n";
    M.PrintAnswer();
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <vector>

#define MAX 12
#define NIL (-1)

using namespace std;

inline bool inRange(int t) { return (1 <= t && t < MAX - 1); }

class CMap {
public:
  int m[MAX][MAX];
  vector<pair<int, int>> vHist;

  CMap() { Reset(); }
  void Reset() {
    for (int i = 0; i < MAX; ++i)
      for (int j = 0; j < MAX; ++j)
        m[i][j] = 0;
  }
  bool PassParticle(int i, int j) {
    int cnt0 = 0;
    int cnt1 = 0;
    const int di[] = {0, -1, 0, 1, 0};
    const int dj[] = {0, 0, 1, 0, -1};

    if (!(inRange(i) && inRange(j)))
      return false;

    if (find(vHist.begin(), vHist.end(), make_pair(i, j)) != vHist.end())
      return false;

    for (int k = 0; k < sizeof(di) / sizeof(*di); ++k)
      if (inRange(i + di[k]) && inRange(j + dj[k]))
        if (m[i + di[k]][j + dj[k]] == 1)
          cnt1++;
        else
          cnt0++;

    if (cnt1 < cnt0)
      return false;

    for (int k = 0; k < sizeof(di) / sizeof(*di); ++k)
      if (inRange(i + di[k]) && inRange(j + dj[k]))
        m[i + di[k]][j + dj[k]] ^= 1;

    vHist.push_back(make_pair(i, j));
    return true;
  }
  bool NoLight(int range = MAX - 1) const {
    for (int i = 1; i < range; ++i)
      for (int j = 1; j < range; ++j)
        if (m[i][j] == 1)
          return false;
    return true;
  }
  void Print() const {
    for (unsigned int i = 1; i < MAX - 1; ++i) {
      for (unsigned int j = 1; j < MAX - 1; ++j) {
        cout << m[i][j] << ' ';
      }
      cout << '\\n';
    }
  }
  void PrintPassHist() {
    Reset();

    for (unsigned int i = 0; i < vHist.size(); ++i)
      m[vHist[i].first][vHist[i].second] = 1;

    for (int i = 1; i < MAX - 1; ++i) {
      cout << m[i][1];
      for (int j = 2; j < MAX - 1; ++j)
        cout << ' ' << m[i][j];
      cout << '\\n';
    }
  }
};

void EraseBlocks(CMap &M) {
  // cout << "Entered EraseBlocks Function.\\n";
  bool bCont;
  do {
    bCont = false;
    for (unsigned int i = 1; i < MAX - 1; ++i) {
      for (unsigned int j = 1; j < MAX - 1; ++j) {
        if (M.m[i][j] == 1) {
          int ci = NIL, cj = NIL, cntCand = 0;
          const int di[] = {-1, -1, 0, 1, 1, 1, 0, -1};
          const int dj[] = {0, 1, 1, 1, 0, -1, -1, -1};
          const int sz = sizeof(di) / sizeof(*di);

          for (int k = 0; k < sz; k += 2) {
            if (M.m[i + di[k]][j + dj[k]] == 0 && inRange(i + di[k]) &&
                inRange(j + dj[k]) &&
                M.m[i + di[(k + 1) % sz]][j + dj[(k + 1) % sz]] == 0 &&
                inRange(i + di[(k + 1) % sz]) &&
                inRange(j + dj[(k + 1) % sz]) &&
                M.m[i + di[(k + 2) % sz]][j + dj[(k + 2) % sz]] == 0 &&
                inRange(i + di[(k + 2) % sz]) &&
                inRange(j + dj[(k + 2) % sz]) &&
                M.m[i + di[(k + 3) % sz]][j + dj[(k + 3) % sz]] == 0 &&
                inRange(i + di[(k + 3) % sz]) &&
                inRange(j + dj[(k + 3) % sz]) &&
                M.m[i + di[(k + 4) % sz]][j + dj[(k + 4) % sz]] == 0 &&
                inRange(i + di[(k + 4) % sz]) &&
                inRange(j + dj[(k + 4) % sz]) &&

                M.m[i + di[(k + 5) % sz]][j + dj[(k + 5) % sz]] == 1 &&
                inRange(i + di[(k + 5) % sz]) &&
                inRange(j + dj[(k + 5) % sz]) &&
                M.m[i + di[(k + 7) % sz]][j + dj[(k + 7) % sz]] == 1 &&
                inRange(i + di[(k + 7) % sz]) &&
                inRange(j + dj[(k + 7) % sz])) {
              CMap Tmp = M;
              if (M.PassParticle(i + di[(k + 6) % sz], j + dj[(k + 6) % sz])) {
                cntCand++;
                ci = i + di[(k + 6) % sz];
                cj = j + dj[(k + 6) % sz];
                M = Tmp;
              }
            }
          }
          if (cntCand == 1) {
            if (M.PassParticle(ci, cj)) {
              bCont = true;
            }
          }
        }
      }
    }
  } while (bCont);
}

void BFS(CMap &stm, CMap &ret) {
  queue<CMap> q;

  q.push(stm);
  while (!q.empty()) {
    bool bCont = true;
    CMap Tmp, M = q.front();

    q.pop();

    EraseBlocks(M);
    /*cout << "Erase End.\\n";
    cout << endl;
    M.Print();*/
    if (M.NoLight()) {
      ret = M;
      break;
    }
    Tmp = M;

    for (unsigned int i = 1; i < MAX - 1 && bCont; ++i) {
      for (unsigned int j = 1; j < MAX - 1 && bCont; ++j) {
        if (M.m[i][j] == 1) {
          int max1 = 0;
          int ci = NIL, cj = NIL;
          const int di[] = {-1, 0, 1, 0};
          const int dj[] = {0, 1, 0, -1};
          const int sz = sizeof(di) / sizeof(*di);

          for (int k = 0; k < sz; ++k) {
            if (M.m[i + di[k]][j + dj[k]] == 0 && inRange(i + di[k]) &&
                inRange(j + dj[k]) &&
                M.m[i + di[(k + 1) % sz]][j + dj[(k + 1) % sz]] == 0 &&
                inRange(i + di[(k + 1) % sz]) &&
                inRange(j + dj[(k + 1) % sz])) {
              if (M.PassParticle(i + di[(k + 2) % sz], j + dj[(k + 2) % sz])) {
                q.push(M);
                M = Tmp;
                bCont = false;
              }
              if (M.PassParticle(i + di[(k + 3) % sz], j + dj[(k + 3) % sz])) {
                q.push(M);
                M = Tmp;
                bCont = false;
              }
            }
          }
        }
      }
    }
  }
}
int main(void) {
  int n;

  cin >> n;
  for (int D = 0; D < n; ++D) {
    CMap M;
    CMap Answer;

    for (int i = 1; i < MAX - 1; ++i)
      for (int j = 1; j < MAX - 1; ++j)
        cin >> M.m[i][j];

    BFS(M, Answer);
    Answer.PrintPassHist();
  }
  return 0;
}