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

#include <cstring>
#include <iostream>
#include <queue>
#include <set>
#include <vector>

using namespace std;

// bool passed[1<<25];
set<int> passed;

int field[10][10];

int main() {
  int n;
  while (cin >> n && n) {
    int ss = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        cin >> field[i][j];
        if (field[i][j])
          ss |= (1 << (i * n + j));
      }
    }
    passed.clear();
    // memset(passed,0,sizeof(passed));
    queue<int> q[2];
    int cur = 0;
    int nxt = 1;
    q[cur].push(ss);
    int cnt = 0;
    // passed[ss]=true;
    passed.insert(ss);
    bool fin = false;
    while (q[cur].size()) {
      while (q[cur].size()) {
        int cs = q[cur].front();
        if (cs == 0) {
          fin = true;
          break;
        }
        q[cur].pop();
        // csÌóÔ©çÚ®Å«éóÔðñ
        int minx, miny;
        minx = miny = -1;
        for (int i = 0; i < n; i++) {
          for (int j = 0; j < n; j++) {
            if ((cs >> (i * n + j)) & 1) {
              minx = j;
              miny = i;
              break;
            }
          }
          if (minx != -1)
            break;
        }
        // EºÌÀWðè
        for (int i = miny; i < n; i++) {
          for (int j = minx; j < n; j++) {
            // ½]
            int ns = cs;
            for (int k = miny; k <= i; k++) {
              for (int l = minx; l <= j; l++) {
                if ((ns >> (k * n + l)) & 1)
                  ns &= ~(1 << (k * n + l));
                else
                  ns |= (1 << (k * n + l));
              }
            }
            if (passed.find(ns) == passed.end()) {
              q[nxt].push(ns);
              // passed[ns]=true;
              passed.insert(ns);
            }
          }
        }
      }
      if (fin)
        break;
      cnt++;
      swap(cur, nxt);
    }
    for (int i = 0; i < cnt; i++)
      cout << "myon";
    cout << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <cstring>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

bool passed[1 << 25];
int field[10][10];

int main() {

  int n;
  while (cin >> n && n) {
    int ss = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        cin >> field[i][j];
        if (field[i][j])
          ss |= (1 << (i * n + j));
      }
    }
    memset(passed, 0, sizeof(passed));
    queue<int> q[2];
    q[0].push(ss);
    int cur = 0;
    int nxt = 1;
    int cnt = 0;
    passed[ss] = true;
    bool fin = false;
    while (q[cur].size()) {
      while (q[cur].size()) {
        int cs = q[cur].front();
        if (cs == 0) {
          fin = true;
          break;
        }
        q[cur].pop();
        // csÌóÔ©çÚ®Å«éóÔðñ
        int minx, miny;
        minx = miny = -1;
        for (int i = 0; i < n; i++) {
          for (int j = 0; j < n; j++) {
            if ((cs >> (i * n + j)) & 1) {
              minx = j;
              miny = i;
              break;
            }
          }
          if (minx != -1)
            break;
        }
        // EºÌÀWðè
        for (int i = miny; i < n; i++) {
          for (int j = minx; j < n; j++) {
            // ½]
            int ns = cs;
            for (int k = miny; k <= i; k++) {
              for (int l = minx; l <= j; l++) {
                if ((ns >> (k * n + l)) & 1)
                  ns &= ~(1 << (k * n + l));
                else
                  ns |= (1 << (k * n + l));
              }
            }
            if (!passed[ns]) {
              q[nxt].push(ns);
              passed[ns] = true;
            }
          }
        }
      }
      if (fin)
        break;
      cnt++;
      swap(cur, nxt);
    }
    for (int i = 0; i < cnt; i++)
      cout << "myon";
    cout << endl;
  }

  return 0;
}