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

#include <iostream>
#include <queue>
using namespace std;
int map[5][5];
int dx[5] = {0, 0, 1, -1}, dy[5] = {1, -1, 0, 0};
queue<int> a1, a2, a3, a4, a5, a6, a7, a8, a9, jx, jy;
queue<int> a;
bool pd[45000000];
int main() {
  for (int i = 1; i <= 3; i++) {
    for (int j = 1; j <= 3; j++) {
      cin >> map[i][j];
      if (i == 1 && j == 1)
        a1.push(map[i][j]);
      else if (i == 1 && j == 2)
        a2.push(map[i][j]);
      else if (i == 1 && j == 3)
        a3.push(map[i][j]);
      else if (i == 2 && j == 1)
        a4.push(map[i][j]);
      else if (i == 2 && j == 2)
        a5.push(map[i][j]);
      else if (i == 2 && j == 3)
        a6.push(map[i][j]);
      else if (i == 3 && j == 1)
        a7.push(map[i][j]);
      else if (i == 3 && j == 2)
        a8.push(map[i][j]);
      else if (i == 3 && j == 3)
        a9.push(map[i][j]);
      if (map[i][j] == 0) {
        jx.push(i);
        jy.push(j);
        a.push(0);
      }
    }
  }
  int jishu = 0;
  jishu = a1.front();
  jishu = jishu * 9 + a2.front();
  jishu = jishu * 9 + a3.front();
  jishu = jishu * 9 + a4.front();
  jishu = jishu * 9 + a5.front();
  jishu = jishu * 9 + a6.front();
  jishu = jishu * 9 + a7.front();
  jishu = jishu * 9 + a8.front();
  pd[jishu] = 1;
  jishu = 0;
  while (a1.front() != 1 || a2.front() != 2 || a3.front() != 3 ||
         a4.front() != 4 || a5.front() != 5 || a6.front() != 6 ||
         a7.front() != 7 || a8.front() != 8 || a9.front() != 0) {
    for (int i = 0; i <= 3; i++) {
      if (jx.front() + dx[i] >= 1 && jx.front() + dx[i] <= 3 &&
          jy.front() + dy[i] >= 1 && jy.front() + dy[i] <= 3) {
        map[1][1] = a1.front();
        map[1][2] = a2.front();
        map[1][3] = a3.front();
        map[2][1] = a4.front();
        map[2][2] = a5.front();
        map[2][3] = a6.front();
        map[3][1] = a7.front();
        map[3][2] = a8.front();
        map[3][3] = a9.front();

        map[jx.front()][jy.front()] =
            map[jx.front() + dx[i]][jy.front() + dy[i]];
        map[jx.front() + dx[i]][jy.front() + dy[i]] = 0;

        jishu = map[1][1];
        jishu = jishu * 9 + map[1][2];
        jishu = jishu * 9 + map[1][3];
        jishu = jishu * 9 + map[2][1];
        jishu = jishu * 9 + map[2][2];
        jishu = jishu * 9 + map[2][3];
        jishu = jishu * 9 + map[3][1];
        jishu = jishu * 9 + map[3][2];

        if (pd[jishu] == 1)
          continue;
        else
          pd[jishu] = 1;

        for (int i1 = 1; i1 <= 3; i1++) {
          for (int i2 = 1; i2 <= 3; i2++) {
            if (i1 == 1 && i2 == 1)
              a1.push(map[i1][i2]);
            else if (i1 == 1 && i2 == 2)
              a2.push(map[i1][i2]);
            else if (i1 == 1 && i2 == 3)
              a3.push(map[i1][i2]);
            else if (i1 == 2 && i2 == 1)
              a4.push(map[i1][i2]);
            else if (i1 == 2 && i2 == 2)
              a5.push(map[i1][i2]);
            else if (i1 == 2 && i2 == 3)
              a6.push(map[i1][i2]);
            else if (i1 == 3 && i2 == 1)
              a7.push(map[i1][i2]);
            else if (i1 == 3 && i2 == 2)
              a8.push(map[i1][i2]);
            else
              a9.push(map[i1][i2]);
          }
        }
        jx.push(jx.front() + dx[i]);
        jy.push(jy.front() + dy[i]);
        a.push(a.front() + 1);
      }
    }
    a1.pop();
    a2.pop();
    a3.pop();
    a4.pop();
    a5.pop();
    a6.pop();
    a7.pop();
    a8.pop();
    a9.pop();
    jx.pop();
    jy.pop();
    a.pop();
  }
  cout << a.front() << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
#include <set>
using namespace std;
set<long long> s;
int map1[10][10];
int k[10];
int t;
queue<long long> x, y;
void input() {
  int g = 0;
  for (int i = 0; i < 3; ++i) {
    for (int j = 0; j < 3; ++j) {
      cin >> map1[i][j];
      g = g * 10 + map1[i][j];
    }
  }
  s.insert(g);
  x.push(g);
  y.push(0);
}
void syf() {
  while (!x.empty()) {
    if (x.front() == 123456780) {
      cout << y.front() << endl;
      return;
    }
    int g = x.front(), tot = 0, kl = -1;
    while (g != 0) {
      if (g % 10 == 0)
        kl = tot;
      k[tot++] = g % 10;
      g /= 10;
    }
    if (kl == -1) {
      kl = tot;
      tot++;
    }
    if (kl % 3 > 0) {
      t = k[kl];
      k[kl] = k[kl - 1];
      k[kl - 1] = t;
      long long ss = 0;
      for (int i = tot - 1; i >= 0; --i) {
        ss = ss * 10 + k[i];
      }
      if (s.count(ss) == 0) {
        x.push(ss);
        y.push(y.front() + 1);
        s.insert(ss);
      }
      t = k[kl];
      k[kl] = k[kl - 1];
      k[kl - 1] = t;
    }
    if (kl % 3 < 2) {
      t = k[kl];
      k[kl] = k[kl + 1];
      k[kl + 1] = t;
      long long ss = 0;
      for (int i = tot - 1; i >= 0; --i) {
        ss = ss * 10 + k[i];
      }
      if (s.count(ss) == 0) {
        x.push(ss);
        y.push(y.front() + 1);
        s.insert(ss);
      }
      t = k[kl];
      k[kl] = k[kl + 1];
      k[kl + 1] = t;
    }
    if (kl + 3 <= 8) {
      t = k[kl];
      k[kl] = k[kl + 3];
      k[kl + 3] = t;
      long long ss = 0;
      for (int i = tot - 1; i >= 0; --i) {
        ss = ss * 10 + k[i];
      }
      if (s.count(ss) == 0) {
        x.push(ss);
        y.push(y.front() + 1);
        s.insert(ss);
      }
      t = k[kl];
      k[kl] = k[kl + 3];
      k[kl + 3] = t;
    }
    if (kl - 3 >= 0) {
      t = k[kl];
      k[kl] = k[kl - 3];
      k[kl - 3] = t;
      long long ss = 0;
      for (int i = tot - 1; i >= 0; --i) {
        ss = ss * 10 + k[i];
      }
      if (s.count(ss) == 0) {
        x.push(ss);
        y.push(y.front() + 1);
        s.insert(ss);
      }
      t = k[kl];
      k[kl] = k[kl - 3];
      k[kl - 3] = t;
    }
    x.pop();
    y.pop();
  }
}
int main() {
  input();
  syf();
  return 0;
}