Problem p00121 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
using namespace std;

struct dat {
  string s;
  int i;
};

int main() {
  string str;
  int a[8];
  dat d, d2;

  map<string, int> cost;
  map<string, bool> checked;

  queue<dat> que;

  cost["01234567"] = 0;
  checked["01234567"] = true;

  d.s = "01234567";
  d.i = 0;
  que.push(d);
  while (!que.empty()) {
    d = que.front();
    que.pop();
    // cout << d.s << " " << d.i << endl;
    for (int i = 0; i < 8; i++) {
      if (d.s[i] == '0') {
        if (i % 4 > 0) {
          d2 = d;
          swap(d2.s[i], d2.s[i - 1]);
          if (!checked[d2.s]) {
            checked[d2.s] = true;
            cost[d2.s] = d.i + 1;
            d2.i += 1;
            que.push(d2);
          }
        }
        if ((i + 1) % 4 > 0) {
          d2 = d;
          swap(d2.s[i], d2.s[i + 1]);
          if (!checked[d2.s]) {
            checked[d2.s] = true;
            cost[d2.s] = d.i + 1;
            d2.i += 1;
            que.push(d2);
          }
        }

        break;
      }
    }
    for (int i = 0; i < 8; i++) {
      if (d.s[i] == '0') {
        if (i < 4) {
          d2 = d;
          swap(d2.s[i], d2.s[i + 4]);
          if (!checked[d2.s]) {
            checked[d2.s] = true;
            cost[d2.s] = d.i + 1;
            d2.i += 1;
            que.push(d2);
          }
        } else {
          d2 = d;
          swap(d2.s[i], d2.s[i - 4]);
          if (!checked[d2.s]) {
            checked[d2.s] = true;
            cost[d2.s] = d.i + 1;
            d2.i += 1;
            que.push(d2);
          }
        }

        break;
      }
    }
  }

  while (cin >> a[0] >> a[1] >> a[2] >> a[3] >> a[4] >> a[5] >> a[6] >> a[7]) {
    for (int i = 0; i < 8; i++) {
      str += a[i] + 48;
    }
    cout << cost[str] << endl;
    str.erase();
  }

  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <queue>
using namespace std;

typedef pair<int *, int> p;
queue<p> que;
#define rep(a) for (int a = 0; a < 8; a++)

int main() {
  int dat[8];
  bool checked[8][8][8][8][8][8][8][8];
  int *con;
  p P;
  int cou, zero;
  int a, b, c, d, e, f, g, h;

  // printf("a");

  while ((scanf("%d%d%d%d%d%d%d%d", &dat[0], &dat[1], &dat[2], &dat[3], &dat[4],
                &dat[5], &dat[6], &dat[7])) != EOF) {
    P.first = dat;
    P.second = 0;
    que.push(P);

    rep(a) rep(b) rep(c) rep(d) rep(e) rep(f) rep(g) rep(h)
        checked[a][b][c][d][e][f][g][h] = false;

    while (!que.empty()) {
      P = que.front();
      que.pop();

      con = P.first;
      cou = P.second;

      for (int i = 0; i < 8; i++) {
        if (con[i] != i)
          break;
        if (i + 1 == 8) {
          printf("%d\\n", cou);
          while (!que.empty())
            que.pop();
        }
      }

      if (que.empty())
        break;

      for (int i = 0; i < 8; i++) {
        if (con[i] == 0) {
          zero = i;
          break;
        }
      }

      checked[con[0]][con[1]][con[2]][con[3]][con[4]][con[5]][con[6]][con[7]] =
          true;
      switch (zero % 4) {
      case 0:
        if (!checked[con[4]][con[1]][con[2]][con[3]][con[0]][con[5]][con[6]]
                    [con[7]]) {
          swap(con[0], con[4]);
          P.first = con;
          P.second = cou + 1;
          que.push(P);
          swap(con[0], con[4]);
        }
        break;
      case 1:
        if (!checked[con[0]][con[5]][con[2]][con[3]][con[4]][con[1]][con[6]]
                    [con[7]]) {
          swap(con[1], con[5]);
          P.first = con;
          P.second = cou + 1;
          que.push(P);
          swap(con[1], con[5]);
        }
        break;
      case 2:
        if (!checked[con[0]][con[1]][con[6]][con[3]][con[4]][con[5]][con[2]]
                    [con[7]]) {
          swap(con[2], con[6]);
          P.first = con;
          P.second = cou + 1;
          que.push(P);
          swap(con[2], con[6]);
        }
        break;
      case 3:
        if (!checked[con[0]][con[1]][con[2]][con[7]][con[4]][con[5]][con[6]]
                    [con[3]]) {
          swap(con[3], con[7]);
          P.first = con;
          P.second = cou + 1;
          que.push(P);
          swap(con[3], con[7]);
        }
        break;
      default:
        break;
      }
      if (zero == 0 || zero == 1) {
        if (!checked[con[1]][con[0]][con[2]][con[3]][con[4]][con[5]][con[6]]
                    [con[7]]) {
          swap(con[0], con[1]);
          P.first = con;
          P.second = cou + 1;
          que.push(P);
          swap(con[0], con[1]);
        }
      }
      if (zero == 1 || zero == 2) {
        if (!checked[con[0]][con[2]][con[1]][con[3]][con[4]][con[5]][con[6]]
                    [con[7]]) {
          swap(con[1], con[2]);
          P.first = con;
          P.second = cou + 1;
          que.push(P);
          swap(con[1], con[2]);
        }
      }
      if (zero == 2 || zero == 3) {
        if (!checked[con[0]][con[1]][con[3]][con[2]][con[4]][con[5]][con[6]]
                    [con[7]]) {
          swap(con[2], con[3]);
          P.first = con;
          P.second = cou + 1;
          que.push(P);
          swap(con[2], con[3]);
        }
      }
      if (zero == 4 || zero == 5) {
        if (!checked[con[0]][con[1]][con[2]][con[3]][con[5]][con[4]][con[6]]
                    [con[7]]) {
          swap(con[4], con[5]);
          P.first = con;
          P.second = cou + 1;
          que.push(P);
          swap(con[4], con[5]);
        }
      }
      if (zero == 5 || zero == 6) {
        if (!checked[con[0]][con[1]][con[2]][con[3]][con[4]][con[6]][con[5]]
                    [con[7]]) {
          swap(con[5], con[6]);
          P.first = con;
          P.second = cou + 1;
          que.push(P);
          swap(con[5], con[6]);
        }
      }
      if (zero == 6 || zero == 7) {
        if (!checked[con[0]][con[1]][con[2]][con[3]][con[4]][con[5]][con[7]]
                    [con[6]]) {
          swap(con[6], con[7]);
          P.first = con;
          P.second = cou + 1;
          que.push(P);
          swap(con[6], con[7]);
        }
      }
    }
  }

  return 0;
}