Problem p00645 (C++) — buggy submission, verdict: Time 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;
    FOREQ(ny, y + 1, n) {
      FOREQ(nx, x + 1, n) {
        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[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: TLE

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[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) {
    if (y == 0 && x == 0) {
      continue;
    }
    ret |= field[y][x] << ((y * n) + x);
  }
  return ret;
}

void inv(int y, int x, int ey, int ex) {
  FOREQ(ny, y, ey) FOREQ(nx, x, ex) { field[ny][nx] ^= 1; }
}

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 {
    FOR(ny, y, n) {
      FOR(nx, x, n) {
        inv(y, x, ny, nx);
        ret = min(ret, calc(y, x + 1) + 1);
        inv(y, x, ny, nx);
      }
    }
  }
  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]); }
    int ans = calc(0, 0);
    while (ans--) {
      printf("myon");
    }
    puts("");
  }
}