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

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define foreach(it, c)                                                         \\
  for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define rforeach(it, c)                                                        \\
  for (__typeof__((c).rbegin()) it = (c).rbegin(); it != (c).rend(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define CL(arr, val) memset(arr, val, sizeof(arr))
#define COPY(dest, src) memcpy(dest, src, sizeof(dest))

template <class T> void max_swap(T &a, const T &b) { a = max(a, b); }
template <class T> void min_swap(T &a, const T &b) { a = min(a, b); }

template <class T> void sort(vector<T> &c) { sort(c.begin(), c.end()); }
template <class T> void uniq(vector<T> &c) {
  sort(c.begin(), c.end());
  c.erase(unique(c.begin(), c.end()), c.end());
}

template <class T> string to_s(const T &a) {
  ostringstream os;
  os << a;
  return os.str();
}
template <class T> T to_T(const string &s) {
  istringstream is(s);
  T res;
  is >> res;
  return res;
}

bool valid(int x, int y, int w, int h) {
  return 0 <= x && x < w && 0 <= y && y < h;
}

template <class T, class U> ostream &operator<<(ostream &os, pair<T, U> &p) {
  os << "( " << p.first << ", " << p.second << " )";
  return os;
}

template <class T>
void print(T a, int n, const string &deli = " ", int br = 1) {
  for (int i = 0; i < n; ++i) {
    cout << a[i];
    if (i + 1 != n)
      cout << deli;
  }
  while (br--)
    cout << endl;
}
template <class T>
void print(const T &c, const string &deli = " ", int br = 1) {
  foreach (it, c) {
    cout << *it;
    if (++it != c.end())
      cout << deli;
    --it;
  }
  while (br--)
    cout << endl;
}
template <class T> void print2d(T a, int w, int h, int width = -1, int br = 1) {
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      if (width != -1)
        cout.width(width);
      cout << a[i][j] << ' ';
    }
    cout << endl;
  }
  while (br--)
    cout << endl;
}

template <class T> void input(T &a, int n) {
  for (int i = 0; i < n; ++i)
    cin >> a[i];
}
template <class T> void input(T *a, int n) {
  for (int i = 0; i < n; ++i)
    cin >> a[i];
}
template <class T> void input2d(T &a, int w, int h) {
  for (int i = 0; i < h; ++i)
    for (int j = 0; j < w; ++j)
      cin >> a[i][j];
}

void fast_io() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}
#define trace(x) (cout << #x << ": " << (x) << endl)

typedef long long ll;
typedef pair<int, int> pint;

const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};

const double PI = acos(-1.0);
const int mod = 1000000007;

void p(int e) {
  for (int y = 0; y < 5; ++y) {
    for (int x = 0; x < 5; ++x)
      printf("%d ", e >> (5 * y + x) & 1);
    puts("");
  }
  puts("");
}

bool enemy(int e, int x, int y) { return e >> (5 * y + x) & 1; }
int xor_mask[5][5][5][5];
int cost[1 << 20]; /// 4 rows * 5 cols
void pre() {
  // myon mask
  for (int y1 = 0; y1 < 5; ++y1) {
    for (int x1 = 0; x1 < 5; ++x1) {
      for (int y2 = y1; y2 < 5; ++y2) {
        for (int x2 = x1; x2 < 5; ++x2) {
          int mask = 0;
          for (int y = y1; y <= y2; ++y)
            for (int x = x1; x <= x2; ++x)
              mask |= 1 << (5 * y + x);
          xor_mask[y1][x1][y2][x2] = mask;
        }
      }
    }
  }

  // bfs
  CL(cost, -1);
  queue<int> q;
  cost[0] = 0;
  q.push(0);
  while (!q.empty()) {
    int cur = q.front();
    int ncost = cost[cur] + 1;
    q.pop();

    for (int y1 = 0; y1 < 4; ++y1) {
      for (int x1 = 0; x1 < 5; ++x1) {
        for (int y2 = y1; y2 < 4; ++y2) {
          for (int x2 = x1; x2 < 5; ++x2) {
            int next = cur ^ xor_mask[y1][x1][y2][x2];
            if (cost[next] == -1) {
              cost[next] = ncost;
              q.push(next);
            }
          }
        }
      }
    }
  }
}
void dfs(int e, int x, int c, int &res) {
  if (x >= 5 || c >= res)
    return;
  else if (e < (1 << 20)) {
    min_swap(res, c + cost[e]);
    return;
  }

  for (int yy = 0; yy < 5; ++yy) {
    for (int x1 = x; x1 < 5; ++x1) {
      for (int x2 = x1; x2 < 5; ++x2) {
        int ne = e ^ xor_mask[yy][x1][4][x2];
        dfs(ne, x + 1, c + 1, res);
      }
    }
  }
  dfs(e, x + 1, c, res);
}
int main() {
  fast_io();
  pre();

  string myon[33];
  myon[0] = "";
  for (int i = 0; i < 30; ++i)
    myon[i + 1] = myon[i] + "myon";

  int n;
  while (cin >> n, n) {
    int e = 0;
    for (int y = 0; y < n; ++y) {
      for (int x = 0; x < n; ++x) {
        int t;
        cin >> t;
        e |= t << (y * 5 + x);
      }
    }

    // shift upward if no enemies in 1st row
    while (!(e & ~((1 << 5) - 1)))
      e <<= 5;

    int times = 25;
    dfs(e, 0, 0, times);
    cout << myon[times] << endl;
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define foreach(it, c)                                                         \\
  for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define rforeach(it, c)                                                        \\
  for (__typeof__((c).rbegin()) it = (c).rbegin(); it != (c).rend(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define CL(arr, val) memset(arr, val, sizeof(arr))
#define COPY(dest, src) memcpy(dest, src, sizeof(dest))

template <class T> void max_swap(T &a, const T &b) { a = max(a, b); }
template <class T> void min_swap(T &a, const T &b) { a = min(a, b); }

template <class T> void sort(vector<T> &c) { sort(c.begin(), c.end()); }
template <class T> void uniq(vector<T> &c) {
  sort(c.begin(), c.end());
  c.erase(unique(c.begin(), c.end()), c.end());
}

template <class T> string to_s(const T &a) {
  ostringstream os;
  os << a;
  return os.str();
}
template <class T> T to_T(const string &s) {
  istringstream is(s);
  T res;
  is >> res;
  return res;
}

bool valid(int x, int y, int w, int h) {
  return 0 <= x && x < w && 0 <= y && y < h;
}

template <class T, class U> ostream &operator<<(ostream &os, pair<T, U> &p) {
  os << "( " << p.first << ", " << p.second << " )";
  return os;
}

template <class T>
void print(T a, int n, const string &deli = " ", int br = 1) {
  for (int i = 0; i < n; ++i) {
    cout << a[i];
    if (i + 1 != n)
      cout << deli;
  }
  while (br--)
    cout << endl;
}
template <class T>
void print(const T &c, const string &deli = " ", int br = 1) {
  foreach (it, c) {
    cout << *it;
    if (++it != c.end())
      cout << deli;
    --it;
  }
  while (br--)
    cout << endl;
}
template <class T> void print2d(T a, int w, int h, int width = -1, int br = 1) {
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      if (width != -1)
        cout.width(width);
      cout << a[i][j] << ' ';
    }
    cout << endl;
  }
  while (br--)
    cout << endl;
}

template <class T> void input(T &a, int n) {
  for (int i = 0; i < n; ++i)
    cin >> a[i];
}
template <class T> void input(T *a, int n) {
  for (int i = 0; i < n; ++i)
    cin >> a[i];
}
template <class T> void input2d(T &a, int w, int h) {
  for (int i = 0; i < h; ++i)
    for (int j = 0; j < w; ++j)
      cin >> a[i][j];
}

void fast_io() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}
#define trace(x) (cout << #x << ": " << (x) << endl)

typedef long long ll;
typedef pair<int, int> pint;

const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};

const double PI = acos(-1.0);
const int mod = 1000000007;

typedef vector<vector<bool>> tab;
const int size = 5;

int n;
int xor_mask[5][5][5][5];
#if __GNUC__
#include <tr1/unordered_map>
#include <tr1/unordered_set>
using namespace tr1;
#else
#include <unordered_map>
#include <unordered_set>
#endif
// char dp[1 << 25];
unordered_map<int, char> dp;
int recur(int e) {
  if (dp.count(e))
    return dp[e];

  for (int y = 0; y < n; ++y) {
    for (int x = 0; x < n; ++x) {
      if (e >> (y * size + x) & 1) {
        int res = 1 << 29;
        for (int yy = y; yy < n; ++yy)
          for (int xx = x; xx < n; ++xx)
            min_swap(res, recur(e ^ xor_mask[y][x][yy][xx]) + 1);
        return dp[e] = res;
      }
    }
  }

  puts("akan");
  exit(1);
}
int main() {
  for (int y1 = 0; y1 < size; ++y1) {
    for (int x1 = 0; x1 < size; ++x1) {
      for (int y2 = y1; y2 < size; ++y2) {
        for (int x2 = x1; x2 < size; ++x2) {
          int mask = 0;
          for (int y = size - 1; y >= 0; --y)
            for (int x = size - 1; x >= 0; --x)
              mask = (mask << 1) | (x1 <= x && x <= x2 && y1 <= y && y <= y2);
          xor_mask[y1][x1][y2][x2] = mask;
        }
      }
    }
  }

  string myon[33];
  myon[0] = "";
  for (int i = 0; i < 25; ++i)
    myon[i + 1] = myon[i] + "myon";

  dp[0] = 0;

  while (cin >> n, n) {
    bool f[5][5] = {};
    for (int y = 0; y < n; ++y) {
      for (int x = 0; x < n; ++x) {
        int t;
        cin >> t;
        f[y][x] = t == 1;
      }
    }
    int e = 0;
    for (int y = size - 1; y >= 0; --y)
      for (int x = size - 1; x >= 0; --x)
        e = (e << 1) | f[y][x];

    int times = recur(e);
    cout << myon[times] << endl;
  }
}