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

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <string>
using namespace std;
typedef pair<string, int> Pa;
map<string, int> MP;
queue<Pa> Q;
Pa temp, t;
int bz[4] = {4, -4, 1, -1};
void bfs() {
  while (!Q.empty()) {
    temp = Q.front();
    Q.pop();
    for (int i = 0; i < 4; i++) {
      int next = bz[i] + temp.second;
      string str = temp.first;
      swap(str[next], str[temp.second]);
      map<string, int>::iterator it = MP.find(str);
      if (next >= 0 && next < 8 && !(next == 3 && temp.second == 4) &&
          !(next == 4 && temp.second == 3) && it == MP.end()) {
        MP[str] = MP[temp.first] + 1;
        t.first = str;
        t.second = next;
        Q.push(t);
      }
    }
  }
}
int main() {
  string ans;
  Pa t("01234567", 0);
  MP["01234567"] = 0;
  Q.push(t);
  bfs();
  while (getline(cin, ans)) {
    ans.erase(remove(ans.begin(), ans.end(), ' '), ans.end());
    printf("%d\\n", MP[ans]);
  }
}

Error: MLE

Fixed submission:

#include <queue>
#include <stdio.h>
using namespace std;
struct node {
  int sz[8];
  int step;
  int order;
  int place;
};
typedef struct node Node;
queue<Node> Q;
// int bz[][2]={{1,0},{-1,0},{0,1},{0,-1}};
int bfs(Node vn) {
  Node copy;
  while (!Q.empty()) {

    int P = Q.front().place;
    int der = Q.front().order;

    if (P < 4) {
      for (int i = -1; i <= 1; i++) {
        if (i != 0 && P + i >= 0 && P + i < 4 && der != i) {
          copy = Q.front();
          copy.order = -i;
          copy.place = P + i;
          copy.step += 1;
          copy.sz[P] = copy.sz[P + i];
          copy.sz[P + i] = 0;
          for (int j = 0; j < 8; j++) {
            if (copy.sz[j] != j)
              break;
            if (j == 7)
              return copy.step;
          }
          Q.push(copy);
        }
      }
      if (der != 2) {
        copy = Q.front();
        copy.order = 2;
        copy.place = P + 4;
        copy.sz[P] = copy.sz[P + 4];
        copy.sz[P + 4] = 0;
        copy.step += 1;
        for (int j = 0; j < 8; j++) {
          if (copy.sz[j] != j)
            break;
          if (j == 7)
            return copy.step;
        }
        Q.push(copy);
      }
    }

    else {
      for (int i = -1; i <= 1; i++) {
        if (i != 0 && P + i >= 4 && P + i < 8 && der != i) {
          copy = Q.front();
          copy.order = -i;
          copy.place = P + i;
          copy.step += 1;
          copy.sz[P] = copy.sz[P + i];
          copy.sz[P + i] = 0;
          for (int j = 0; j < 8; j++) {
            if (copy.sz[j] != j)
              break;
            if (j == 7)
              return copy.step;
          }
          Q.push(copy);
        }
      }
      if (der != 2) {
        copy = Q.front();
        copy.order = 2;
        copy.place = P - 4;
        copy.sz[P] = copy.sz[P - 4];
        copy.sz[P - 4] = 0;
        copy.step += 1;
        for (int j = 0; j < 8; j++) {
          if (copy.sz[j] != j)
            break;
          if (j == 7)
            return copy.step;
        }
        Q.push(copy);
      }
    }
    Q.pop();
  }
  return 0;
}
int main() {
  Node vb;
  while (scanf("%d%d%d%d%d%d%d%d", &vb.sz[0], &vb.sz[1], &vb.sz[2], &vb.sz[3],
               &vb.sz[4], &vb.sz[5], &vb.sz[6], &vb.sz[7]) != EOF) {
    // getchar();
    while (!Q.empty())
      Q.pop();
    for (int i = 0; i < 8; i++) {
      if (vb.sz[i] == 0) {
        vb.place = i;
        vb.step = 0;
        vb.order = 0;
        Q.push(vb);
        printf("%d\\n", bfs(vb));
        break;
      }
    }
  }
}