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

#define loop(i, j, k) for (long long i = j; i < k; i++)
#include <array>
#include <iostream>
#include <queue>
#include <set>
#include <utility>

using namespace std;

long mhash(array<array<int, 3>, 3> &e) {
  long res = 0;
  long cb = 1;
  loop(i, 0, 9) {
    res += cb * e[i / 3][i % 3];
    cb = cb * 9;
  }
  return res;
}

bool cp(array<array<int, 3>, 3> &e) {
  loop(i, 0, 3) loop(j, 0, 3) if (e[i][j] != (1 + j + i * 3) % 9) return false;
  return true;
}

int main() {
  set<long> ald;
  array<array<int, 3>, 3> pz;
  queue<pair<array<array<int, 3>, 3>, int>> q;
  loop(i, 0, 3) loop(j, 0, 3) cin >> pz[i][j];
  q.push(make_pair(pz, 0));
  if (cp(pz)) {
    cout << 0 << endl;
    return 0;
  }
  while (!q.empty()) {
    auto cur = q.front().first;
    int dep = q.front().second + 1;
    q.pop();
    int r = -1, c = -1;
    loop(i, 0, 3) loop(j, 0, 3) if (cur[i][j] == 0) {
      r = i;
      c = j;
    }
    if (r != 2) {
      swap(cur[r][c], cur[r + 1][c]);
      if (cp(cur)) {
        cout << dep << endl;
        return 0;
      } else {
        long hs = mhash(cur);
        if (ald.find(hs) == ald.end()) {
          q.push(make_pair(cur, dep));
          ald.insert(hs);
        }
      }
      swap(cur[r][c], cur[r + 1][c]);
    }
    if (r != 0) {
      swap(cur[r][c], cur[r - 1][c]);
      if (cp(cur)) {
        cout << dep << endl;
        return 0;
      } else {
        long hs = mhash(cur);
        if (ald.find(hs) == ald.end()) {
          q.push(make_pair(cur, dep));
          ald.insert(hs);
        }
      }
      swap(cur[r][c], cur[r - 1][c]);
    }
    if (c != 2) {
      swap(cur[r][c], cur[r][c + 1]);
      if (cp(cur)) {
        cout << dep << endl;
        return 0;
      } else {
        long hs = mhash(cur);
        if (ald.find(hs) == ald.end()) {
          q.push(make_pair(cur, dep));
          ald.insert(hs);
        }
      }
      swap(cur[r][c], cur[r][c + 1]);
    }
    if (c != 0) {
      swap(cur[r][c], cur[r][c - 1]);
      if (cp(cur)) {
        cout << dep << endl;
        return 0;
      } else {
        long hs = mhash(cur);
        if (ald.find(hs) == ald.end()) {
          q.push(make_pair(cur, dep));
          ald.insert(hs);
        }
      }
      swap(cur[r][c], cur[r][c - 1]);
    }
  }
}


Error: MLE

Fixed submission:

#define loop(i, j, k) for (long long i = j; i < k; i++)
#include <array>
#include <iostream>
#include <queue>
#include <stdio.h>
#include <utility>

using namespace std;

bool cp(array<array<int, 3>, 3> &e) {
  loop(i, 0, 3) loop(j, 0, 3) if (e[i][j] != (1 + j + i * 3) % 9) return false;
  return true;
}

int main() {
  array<array<int, 3>, 3> pz;
  queue<pair<array<array<int, 3>, 3>, int>> q;
  loop(i, 0, 3) loop(j, 0, 3) cin >> pz[i][j];
  q.push(make_pair(pz, 0));
  if (cp(pz)) {
    cout << 0 << endl;
    return 0;
  }
  while (!q.empty()) {
    auto cur = q.front().first;
    int dep = q.front().second + 1;
    q.pop();
    int r = -1, c = -1;
    loop(i, 0, 3) loop(j, 0, 3) if (cur[i][j] == 0) {
      r = i;
      c = j;
    }
    if (r != 2) {
      swap(cur[r][c], cur[r + 1][c]);
      if (cp(cur)) {
        cout << dep << endl;
        return 0;
      } else
        q.push(make_pair(cur, dep));
      swap(cur[r][c], cur[r + 1][c]);
    }
    if (r != 0) {
      swap(cur[r][c], cur[r - 1][c]);
      if (cp(cur)) {
        cout << dep << endl;
        return 0;
      } else
        q.push(make_pair(cur, dep));
      swap(cur[r][c], cur[r - 1][c]);
    }
    if (c != 2) {
      swap(cur[r][c], cur[r][c + 1]);
      if (cp(cur)) {
        cout << dep << endl;
        return 0;
      } else
        q.push(make_pair(cur, dep));
      swap(cur[r][c], cur[r][c + 1]);
    }
    if (c != 0) {
      swap(cur[r][c], cur[r][c - 1]);
      if (cp(cur)) {
        cout << dep << endl;
        return 0;
      } else
        q.push(make_pair(cur, dep));
      swap(cur[r][c], cur[r][c - 1]);
    }
  }
}