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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef pair<int, int> P;
queue<P> q;
map<int, int> m;
map<int, int>::iterator ite;
int A, B, a[8], Q, n0, kaisu, dummy[9];
int *tens;
int main() {
  tens = &dummy[1];
  tens[7] = 1;
  for (int i = 6; i >= -1; i--)
    tens[i] = tens[i + 1] * 10;
  m.insert(make_pair(1234567, 0));
  q.push(P(1234567, 0));
  while (!q.empty()) {
    A = q.front().first;
    kaisu = q.front().second;
    q.pop();
    for (int i = 0; i < 8; i++)
      if ((A / tens[i]) % 10 == 0)
        n0 = i;
    if (n0 < 4) {
      B = A - tens[n0 + 4] * ((A / tens[n0 + 4]) % 10) +
          tens[n0] * ((A / tens[n0 + 4]) % 10);
      ite = m.find(B);
      if (ite == m.end()) {
        m.insert(make_pair(B, kaisu + 1));
        q.push(P(B, kaisu + 1));
      }
    }

    if (4 <= n0) {
      B = A - tens[n0 - 4] * ((A / tens[n0 - 4]) % 10) +
          tens[n0] * ((A / tens[n0 - 4]) % 10);
      ite = m.find(B);
      if (ite == m.end()) {
        m.insert(make_pair(B, kaisu + 1));
        q.push(P(B, kaisu + 1));
      }
    }

    if (n0 != 0 && n0 != 4) {
      B = A - tens[n0 - 1] * ((A / tens[n0 - 1]) % 10) +
          tens[n0] * ((A / tens[n0 - 1]) % 10);
      ite = m.find(B);
      if (ite == m.end()) {
        m.insert(make_pair(B, kaisu + 1));
        q.push(P(B, kaisu + 1));
      }
    }

    if (n0 != 3 && n0 != 7) {
      B = A - tens[n0 + 1] * ((A / tens[n0 + 1]) % 10) +
          tens[n0] * ((A / tens[n0 + 1]) % 10);
      ite = m.find(B);
      if (ite == m.end()) {
        m.insert(make_pair(B, kaisu + 1));
        q.push(P(B, kaisu + 1));
      }
    }
  }
  while (1) {
    Q = 0;
    for (int i = 0; i < 8; i++) {
      if (scanf("%d", &a[i]) == EOF)
        return 0;
      Q += tens[i] * a[i];
    }
    printf("%d\\n", m[Q]);
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
struct pu {
  int a[8];
  int kaisu;
  int s;
}; // 0?????????????????????????????? ?§???????????????????
queue<pu> q;
int an, wze, s2;
pu p;
int main() {
  for (;;) {
    pu orig;
    for (int i = 0; i < 8; i++) {
      if (scanf("%d", &orig.a[i]) == EOF)
        return 0;
    }
    orig.s = -1;
    orig.kaisu = 0;
    q.push(orig);
    while (1) {
      p = q.front();
      s2 = p.s;
      q.pop();
      for (int i = 0; i < 8; i++) {
        if (p.a[i] != i)
          break;
        if (i == 7) {
          an = p.kaisu;
          goto ans;
        }
      }
      for (int i = 0; i < 8; i++)
        if (p.a[i] == 0)
          wze = i;
      p.kaisu++;
      if (s2 != 0 && wze >= 4) { // 0?????????
        swap(p.a[wze], p.a[wze - 4]);
        p.s = 1;
        q.push(p);
        swap(p.a[wze], p.a[wze - 4]);
      }
      if (s2 != 1 && wze < 4) { // 0?????????
        swap(p.a[wze], p.a[wze + 4]);
        p.s = 0;
        q.push(p);
        swap(p.a[wze], p.a[wze + 4]);
      }
      if (s2 != 2 && wze != 3 && wze != 7) { // 0?????????
        swap(p.a[wze], p.a[wze + 1]);
        p.s = 3;
        q.push(p);
        swap(p.a[wze], p.a[wze + 1]);
      }
      if (s2 != 3 && wze != 0 && wze != 4) { // 0?????????
        swap(p.a[wze], p.a[wze - 1]);
        p.s = 2;
        q.push(p);
        swap(p.a[wze], p.a[wze - 1]);
      }
    }
  ans:
    printf("%d\\n", an);
    while (!q.empty())
      q.pop();
  }
}