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

#include <bits/stdc++.h>
using namespace std;
typedef vector<vector<int>> vii;
//

int h(const vector<vector<int>> &x) {
  const int C = 997; // ???f??????
  size_t t = 0;
  for (int i = 0; i != x.size(); ++i) {
    for (int j = 0; j < x[i].size(); ++j) {
      t = t * 2 + x[i][j];
    }
  }
  return t;
}

vector<map<int, int>> memo(6);

int N;
int solve(const vii &field) {
  if (memo[field.size()].find(h(field)) == memo[field.size()].end()) {
    for (int y = 0; y < field.size(); ++y) {
      for (int x = 0; x < field[y].size(); ++x) {
        if (field[y][x]) {
          int ans = 100;
          for (int xsz = 1; xsz <= N - x; ++xsz) {
            for (int ysz = 1; ysz <= N - y; ++ysz) {
              vii nextfield(field);
              for (int dy = 0; dy < ysz; ++dy) {
                for (int dx = 0; dx < xsz; ++dx) {
                  int nexty = y + dy;
                  int nextx = x + dx;
                  nextfield[nexty][nextx] = !nextfield[nexty][nextx];
                }
              }
              ans = min(ans, 1 + solve(nextfield));
            }
          }
          return memo[field.size()][h(field)] = ans;
        }
      }
    }
    return memo[field.size()][h(field)] = 0;
  }
  return memo[field.size()][h(field)];
}
int main() {
  while (true) {
    cin >> N;
    if (!N)
      break;
    vii field(N, vector<int>(N));
    for (int i = 0; i < N; ++i) {
      for (int j = 0; j < N; ++j) {
        cin >> field[i][j];
      }
    }
    int ans = solve(field);
    for (int i = 0; i < ans; ++i) {
      cout << "myon";
    }
    cout << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef vector<vector<bool>> vii;
//
#include <unordered_map>
class HashVI { // ???n???b???V?????????????????I???u???W???F???N???g
public:
  size_t operator()(const vector<vector<bool>> &x) const {
    const int C = 997; // ???f??????
    size_t t = 0;
    for (int i = 0; i != x.size(); ++i) {
      for (int j = 0; j < x[i].size(); ++j) {
        t = t * C + x[i][j];
      }
    }
    return t;
  }
};
unordered_map<vector<vector<bool>>, int, HashVI> memo;

int N;
int solve(const vii &field) {
  if (memo.find(field) == memo.end()) {
    for (int y = 0; y < field.size(); ++y) {
      for (int x = 0; x < field[y].size(); ++x) {
        if (field[y][x]) {
          int ans = 100;
          for (int xsz = 1; xsz <= N - x; ++xsz) {
            for (int ysz = 1; ysz <= N - y; ++ysz) {
              vii nextfield(field);
              for (int dy = 0; dy < ysz; ++dy) {
                for (int dx = 0; dx < xsz; ++dx) {
                  int nexty = y + dy;
                  int nextx = x + dx;
                  nextfield[nexty][nextx] = !nextfield[nexty][nextx];
                }
              }
              ans = min(ans, 1 + solve(nextfield));
            }
          }
          return memo[field] = ans;
        }
      }
    }
    return memo[field] = 0;
  }
  return memo[field];
}
int main() {
  while (true) {
    cin >> N;
    if (!N)
      break;
    memo.clear();
    vii field(N, vector<bool>(N));
    for (int i = 0; i < N; ++i) {
      for (int j = 0; j < N; ++j) {
        int a;
        cin >> a;
        field[i][j] = a;
      }
    }
    int ans = solve(field);
    for (int i = 0; i < ans; ++i) {
      cout << "myon";
    }
    cout << endl;
  }
  return 0;
}