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

#include <algorithm>
#include <iostream>
using namespace std;

const int INF = 1e+8;
int n, X[5][5][5][5] = {0};
char dp[(1 << 25) / 2];

void init() {
  for (int i = 0; i < (1 << 25) / 2; i++) {
    dp[i] = -1;
  }
  for (int ly = 0; ly < 5; ly++) {
    for (int lx = 0; lx < 5; lx++) {
      for (int ry = ly; ry < 5; ry++) {
        for (int rx = lx; rx < 5; rx++) {
          for (int y = ly; y <= ry; y++) {
            for (int x = lx; x <= rx; x++) {
              X[ly][lx][ry][rx] |= (1 << (y * 5 + x));
            }
          }
        }
      }
    }
  }
}

void set(int bits, char k) {
  if (bits % 2 == 0) {
    dp[bits / 2] = ((0xf << 4) & dp[bits / 2]) | k;
  } else {
    dp[bits / 2] = (0xf & dp[bits / 2]) | (k << 4);
  }
}

int get(int bits) {
  if (bits % 2 == 0) {
    return dp[bits / 2] & 0xf;
  } else {
    return (dp[bits / 2] >> 4) & 0xf;
  }
}

int solve(int bits) {
  if (bits == 0)
    return 0;
  if (get(bits) != 0xf)
    return get(bits);

  int sx, sy;
  for (int i = 0; i < 25; i++) {
    if (bits & (1 << i)) {
      sx = i % 5;
      sy = i / 5;
      break;
    }
  }
  int res = INF;
  for (int y = sy; y < 5; y++) {
    for (int x = sx; x < 5; x++) {
      int next = bits ^ X[sy][sx][y][x];
      res = min(res, solve(next) + 1);
    }
  }
  set(bits, res);
  return res;
}

int main() {
  init();
  while (cin >> n, n) {
    int bits = 0;
    for (int i = 0; i < 25; i++) {
      int e = 0;
      if (i % 5 < n && i / 5 < n) {
        cin >> e;
      }
      bits = (bits << 1) + e;
    }
    int ans = solve(bits);
    for (int i = 0; i < ans; i++)
      cout << "myon";
    cout << endl;
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;

const int INF = 1e+8;
// X[sy][sx][gy][gx] := 5*5 テ」ツ?ォテ」ツ?、テ」ツ??」ツ?ヲ (sx,sy) -
// (gx,gy)テ」ツ?ォmyonテ」ツ?凖」ツつ凝」ツ?ィテ」ツ?催」ツ?ォテ、ツスツソテ」ツ??bit
// int X[5][5][5][5] = {0};
// dp[bits] := 5*5 テ」ツ?ォテ」ツ?、テ」ツ??」ツ?ヲ テァツ環カテヲツ??bits
// テ」ツ?ョテヲツ慊?ァツ淞ュテヲツ可凝ヲツ閉ーテ」ツつ津ィツソツ氾」ツ??
char dp[1 << 25];

// "myon" ** n テ」ツつ津ィツソツ氾」ツ??
/*string ans_myon(int k){
        string res;
        for(int i=0 ; i < k ; i++ ){
                res += "myon";
        }
        return res;
}*/

/*void init(){
        for(int i=0 ; i < (1<<25) ; i++ ){
                dp[i] = -1;
        }
        for(int ly = 0 ; ly < 5 ; ly++ ){
                for(int lx = 0 ; lx < 5 ; lx++ ){
                        for(int ry = ly ; ry < 5 ; ry++ ){
                                for(int rx = lx ; rx < 5 ; rx++ ){
                                        for(int y = ly ; y <= ry ; y++ ){
                                                for(int x = lx ; x <= rx ; x++
){ X[ly][lx][ry][rx] |= (1 << (y * 5 + x));
                                                }
                                        }
                                }
                        }
                }
        }
}*/

int get_xor(int lx, int ly, int rx, int ry) {
  int res = 0;
  for (int y = ly; y <= ry; y++) {
    for (int x = lx; x <= rx; x++) {
      res |= (1 << (y * 5 + x));
    }
  }
  return res;
}

int solve(int bits) {
  if (dp[bits] != -1)
    return dp[bits];

  int sx, sy;
  for (int i = 0; i < 25; i++) {
    if (bits & (1 << i)) {
      sx = i % 5;
      sy = i / 5;
      break;
    }
  }
  int res = INF;
  for (int y = sy; y < 5; y++) {
    for (int x = sx; x < 5; x++) {
      // int next = bits ^ X[sy][sx][y][x];
      int next = bits ^ get_xor(sx, sy, x, y);
      res = min(res, solve(next) + 1);
    }
  }
  return dp[bits] = res;
}

int main() {
  int n;
  // init();
  for (int i = 0; i < (1 << 25); i++)
    dp[i] = -1;
  while (cin >> n, n) {
    int bits = 0;
    for (int i = 0; i < 25; i++) {
      int e = 0;
      if (i % 5 < n && i / 5 < n) {
        cin >> e;
      }
      bits = (bits << 1) + e;
    }
    int ans = solve(bits);
    for (int i = 0; i < ans; i++)
      cout << "myon";
    cout << endl;
    // cout << ans_myon(ans) << endl;
  }
}