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

#include <algorithm>
#include <assert.h>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <vector>

using namespace std;
typedef long long ll;
static const double EPS = 1e-9;
static const double PI = acos(-1.0);

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define FORIT(it, c)                                                           \\
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define DEC(i, s) for (int i = (s); i >= 0; i--)

#define SIZE(v) (int)((v).size())
#define MEMSET(v, h) memset((v), h, sizeof(v))
#define FIND(m, w) ((m).find(w) != (m).end())

int n;
int field[6][6];
char memo[1 << 24];

void printField() {
  REP(y, n) {
    REP(x, n) { cout << field[y][x] << " "; }
    cout << endl;
  }
}

int encode() {
  int ret = 0;
  REP(y, n) REP(x, n) {
    if (y == 0 && x == 0) {
      continue;
    }
    ret |= field[y][x] << ((y * n) + x - 1);
  }
  return ret;
}

int calc(int y, int x) {
  int state = encode();
  if (memo[state] != -1) {
    return memo[state];
  }
  int ret = 100;
  if (x == n) {
    ret = calc(y + 1, 0);
  } else if (y == n) {
    ret = 0;
  } else if (field[y][x] == 0) {
    ret = calc(y, x + 1);
  } else {
    field[y][x] = 0;
    field[y][n] = 1;
    field[n][x] = 1;
    FOREQ(ny, y + 1, n) {
      FOREQ(nx, x + 1, n) {
        field[ny][n] = 1;
        field[n][nx] = 1;
        // if (field[ny][x] == 1 && field[y][nx] == 1 && field[ny][nx] == 1) {
        // field[ny][x] = field[y][nx] = field[ny][nx] = 0;
        field[ny][x] ^= 1;
        field[y][nx] ^= 1;
        field[ny][nx] ^= 1;
        ret = min(ret, calc(y, x + 1) + 1);
        field[ny][x] ^= 1;
        field[y][nx] ^= 1;
        field[ny][nx] ^= 1;
        // field[ny][x] = field[y][nx] = field[ny][nx] = 1;
        //}
      }
    }
    field[y][x] = 1;
  }
  if (y == 0 && x == 0) {
    return ret;
  }
  return memo[state] = ret;
}

int main() {
  while (scanf("%d", &n), n) {
    MEMSET(memo, -1);
    MEMSET(field, 0);
    REP(y, n) REP(x, n) { scanf("%d", &field[y][x]); }
    REP(y, n) REP(x, n) {
      if (field[y][x] == 1) {
        FOR(ny, y, n) FOR(nx, x, n) { field[ny][nx] ^= 1; }
        field[y][x] = 1;
      }
    }
    int ans = calc(0, 0);
    while (ans--) {
      printf("myon");
    }
    puts("");
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <vector>

using namespace std;
typedef long long ll;
static const double EPS = 1e-9;
static const double PI = acos(-1.0);

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define FORIT(it, c)                                                           \\
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define DEC(i, s) for (int i = (s); i >= 0; i--)

#define SIZE(v) (int)((v).size())
#define MEMSET(v, h) memset((v), h, sizeof(v))
#define FIND(m, w) ((m).find(w) != (m).end())

int n;
int field[6][6];
char memo[6][1 << 25];

void printField() {
  REP(y, n) {
    REP(x, n) { cout << field[y][x] << " "; }
    cout << endl;
  }
}

int encode() {
  int ret = 0;
  REP(y, n) REP(x, n) { ret |= field[y][x] << ((y * n) + x); }
  return ret;
}

int calc(int y, int x) {
  int state = encode();
  if (memo[n][state] != -1) {
    return memo[n][state];
  }
  int ret = 100;
  if (x == n) {
    ret = calc(y + 1, 0);
  } else if (y == n) {
    ret = 0;
  } else if (field[y][x] == 0) {
    ret = calc(y, x + 1);
  } else {
    field[y][x] = 0;
    field[y][n] = 1;
    field[n][x] = 1;
    field[n][n] = 1;
    FOR(ny, y + 1, n + 1) {
      FOR(nx, x + 1, n + 1) {
        if (field[ny][x] == 1 && field[y][nx] == 1 && field[ny][nx] == 1) {
          field[ny][x] = field[y][nx] = field[ny][nx] = 0;
          ret = min(ret, calc(y, x + 1) + 1);
          field[ny][x] = field[y][nx] = field[ny][nx] = 1;
        }
      }
    }
    field[y][x] = 1;
  }
  return memo[n][state] = ret;
}

int main() {
  MEMSET(memo, -1);
  while (scanf("%d", &n), n) {
    MEMSET(field, 0);
    REP(y, n) REP(x, n) { scanf("%d", &field[y][x]); }
    REP(y, n) REP(x, n) {
      if (field[y][x] == 1) {
        FOR(ny, y, n) FOR(nx, x, n) { field[ny][nx] ^= 1; }
        field[y][x] = 1;
      }
    }
    int ans = calc(0, 0);
    while (ans--) {
      printf("myon");
    }
    puts("");
  }
}