alexjercan/bugnet huggingface dataset:
Problem p00121 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <map>
#include <queue>
#include <sstream>

using namespace std;

vector<string> next(string s) {
  vector<string> v;
  int i = s.find('0', 0);
  if (i < 4) {
    if (i > 0) {
      v.push_back(s.substr(0, i - 1) + s[i] + s[i - 1] + s.substr(i + 1));
    }
    if (i < 3) {
      v.push_back(s.substr(0, i) + s[i + 1] + s[i] + s.substr(i + 2));
    }
    v.push_back(s.substr(0, i) + s[i + 4] + s.substr(i + 1, 3) + s[i] +
                s.substr(i + 5));
  } else {
    if (i < 7) {
      v.push_back(s.substr(0, i) + s[i + 1] + s[i] + s.substr(i + 2));
    }
    if (i > 4) {
      v.push_back(s.substr(0, i - 1) + s[i] + s[i - 1] + s.substr(i + 1));
    }
    v.push_back(s.substr(0, i - 4) + s[i] + s.substr(i - 3, 3) + s[i - 4] +
                s.substr(i + 1));
  }
  return v;
}

int main() {
  int n[8];
  priority_queue<pair<int, string>, vector<pair<int, string>>,
                 greater<pair<int, string>>>
      q;
  q.push(pair<int, string>(0, "01234567"));
  map<string, int> mintime;
  mintime["01234567"] = 0;
  while (!q.empty()) {
    auto p = q.top();
    q.pop();
    auto v = next(p.second);
    for (auto s : v) {
      if (mintime.find(s) == mintime.end()) {
        mintime[s] = p.first + 1;
        q.push(pair<int, string>(p.first + 1, s));
      }
    }
  }
  while (cin >> n[0]) {
    for (int i = 1; i < 8; ++i) {
      cin >> n[i];
    }
    string s;
    stringstream ss;
    for (int i = 0; i < 8; ++i) {
      ss << n[i];
    }
    cout << mintime[ss.str()] << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <queue>
#include <set>
#include <sstream>

using namespace std;

vector<string> next(string s) {
  vector<string> v;
  int i = s.find('0', 0);
  if (i < 4) {
    if (i > 0) {
      v.push_back(s.substr(0, i - 1) + s[i] + s[i - 1] + s.substr(i + 1));
    }
    if (i < 3) {
      v.push_back(s.substr(0, i) + s[i + 1] + s[i] + s.substr(i + 2));
    }
    v.push_back(s.substr(0, i) + s[i + 4] + s.substr(i + 1, 3) + s[i] +
                s.substr(i + 5));
  } else {
    if (i < 7) {
      v.push_back(s.substr(0, i) + s[i + 1] + s[i] + s.substr(i + 2));
    }
    if (i > 5) {
      v.push_back(s.substr(0, i - 1) + s[i] + s[i - 1] + s.substr(i + 1));
    }
    v.push_back(s.substr(0, i - 4) + s[i] + s.substr(i - 3, 3) + s[i - 4] +
                s.substr(i + 1));
  }
  return v;
}

int main() {
  int n[8];
  while (cin >> n[0]) {
    for (int i = 1; i < 8; ++i) {
      cin >> n[i];
    }
    string s;
    stringstream ss;
    for (int i = 0; i < 8; ++i) {
      ss << n[i];
    }
    set<string> used;
    priority_queue<pair<int, string>, vector<pair<int, string>>,
                   greater<pair<int, string>>>
        q;
    q.push(pair<int, string>(0, ss.str()));
    used.insert(ss.str());
    while (q.top().second != "01234567") {
      auto p = q.top();
      q.pop();
      auto v = next(p.second);
      for (auto s : v) {
        if (used.find(s) == used.end()) {
          used.insert(s);
          q.push(pair<int, string>(p.first + 1, s));
        }
      }
    }
    cout << q.top().first << endl;
  }
}
Problem p00121 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <map>
#include <queue>
#include <string>

using namespace std;

int d[] = {1, -1, 4, -4};

bool check(int space) { return (space >= 0 && space < 8); }

int main() {
  map<string, int> m;
  queue<string> que;

  que.push("01234567");
  while (!que.empty()) {
    string now = que.front();
    que.pop();

    int space = now.find("0");
    for (int i = 0; i < 4; i++) {
      if (!check(space + d[i]))
        continue;
      if (space % 4 == 0 && i == 1)
        continue;
      if (space % 4 == 3 && i == 0)
        continue;
      string tmp = now;
      swap(tmp[space], tmp[space + d[i]]);
      if (m.count(tmp) == 0) {
        que.push(tmp);
        m[tmp] = m[now] + 1;
      }
    }
  }

  char c;
  while (cin >> c) {
    string panel;

    panel.push_back(c);
    for (int i = 1; i < 8; i++) {
      cin >> c;
      panel.push_back(c);
    }
    cout << m[panel] << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <map>
#include <queue>
#include <string>

using namespace std;

int d[] = {1, -1, 4, -4};

struct Puzzle {
  string panel;
  int space, cnt;

  Puzzle(string panel_, int space_, int cnt_) {
    panel = panel_;
    space = space_;
    cnt = cnt_;
  }
  Puzzle() {}
};

bool check(string s) {
  if (s == "01234567")
    return true;
  else
    return false;
}

int bfs(Puzzle s) {
  map<string, bool> m;
  queue<Puzzle> que;

  que.push(s);
  m[s.panel] = true;

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

    if (check(now.panel))
      return now.cnt;

    int idx1 = now.space;
    for (int i = 0; i < 4; i++) {
      int idx2 = idx1 + d[i];

      if (idx2 < 0 || idx2 >= 8)
        continue;
      string next_panel = now.panel;

      next_panel[idx1] = now.panel[idx2];
      next_panel[idx2] = now.panel[idx1];

      Puzzle next;
      next.panel = next_panel;
      next.space = idx2;
      next.cnt = now.cnt + 1;

      if (m.count(next.panel) == 0) {
        m[next.panel] = true;
        que.push(next);
      }
    }
  }
  return -1;
}

int main() {
  int space;
  char c;

  while (cin >> c) {
    string panel;

    if (c == '0')
      space = 0;
    panel.push_back(c);
    for (int i = 1; i < 8; i++) {
      cin >> c;
      panel.push_back(c);
      if (c == '0')
        space = i;
    }
    Puzzle s(panel, space, 0);
    cout << bfs(s) << endl;
  }

  return 0;
}
Problem p00121 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

const int d[] = {-1, -4, 1, 4};

typedef pair<string, int> P;

int main() {
  queue<P> que;
  que.push({"01234567", 0});
  map<string, int> done;
  while (!que.empty()) {
    P st = que.front();
    que.pop();
    // if (st.first == g){
    //	res = st.second;
    //	break;
    // }
    if (done.count(st.first))
      continue;
    done.insert(st);

    int p = 0;
    while (st.first[p] != '0')
      p++;

    for (int dir = 0; dir < 4; dir++) {
      P tmp = st;
      tmp.second++;
      int to = p + d[dir];
      if (to < 0 || to >= 8)
        continue;
      if (p + to == 7 && dir % 2 == 0)
        continue;
      swap(tmp.first[p], tmp.first[to]);
      que.push(tmp);
    }
  }

  while (true) {
    string g;
    for (int i = 0; i < 8; i++) {
      string a;
      if (!(cin >> a))
        return 0;
      g += a;
    }

    printf("%d\\n", done[g]);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

const int d[] = {-1, -4, 1, 4};

typedef pair<string, int> P;

int main() {
  while (true) {
    string g;
    for (int i = 0; i < 8; i++) {
      string a;
      if (!(cin >> a))
        return 0;
      g += a;
    }

    int res;
    queue<P> que;
    que.push({"01234567", 0});
    set<string> done;
    while (!que.empty()) {
      P st = que.front();
      que.pop();
      if (st.first == g) {
        res = st.second;
        break;
      }
      if (done.count(st.first))
        continue;
      done.insert(st.first);

      int p = 0;
      while (st.first[p] != '0')
        p++;

      for (int dir = 0; dir < 4; dir++) {
        P tmp = st;
        tmp.second++;
        int to = p + d[dir];
        if (to < 0 || to >= 8)
          continue;
        if (p + to == 7 && dir % 2 == 0)
          continue;
        swap(tmp.first[p], tmp.first[to]);
        que.push(tmp);
      }
    }
    printf("%d\\n", res);
  }

  return 0;
}
Problem p00121 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
using namespace std;

typedef pair<string, int> P;
string data;
int dx[] = {4, 1, -4, -1};
set<string> s;
map<string, int> cost;

void bfs() {
  string str = "01234567";
  cost[str] = 0;
  queue<P> que;
  que.push(P(str, 0));
  s.insert(str);
  while (!que.empty()) {
    P tmp = que.front();
    que.pop();

    int n_0;
    for (int i = 0; i < 8; i++)
      if (tmp.first[i] == '0')
        n_0 = i;
    for (int i = 0; i < 4; i++) {
      if ((n_0 + dx[i]) < 8 && (n_0 + dx[i]) >= 0) {
        if (n_0 == 3) {
          if (dx[i] == 1)
            continue;
        }
        if (n_0 == 4) {
          if (dx[i] == -1)
            continue;
        }
        P t = tmp;
        t.second++;
        swap(t.first[n_0], t.first[n_0 + dx[i]]);
        if (s.find(t.first) == s.end()) {
          que.push(t);
          s.insert(t.first);
          cost[t.first] = t.second;
        }
      }
    }
  }
}
bool input() {
  data.clear();
  for (int i = 0; i < 8; i++) {
    string str;
    if (!(cin >> str))
      return false;
    data += str;
  }
  return true;
}

bool check(string str) { return str == "01234567"; }
int main() {
  bfs();
  while (input()) {
    cout << cost[data] << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
using namespace std;

typedef pair<string, int> P;
string data;
int dx[] = {4, 1, -4, -1};
set<string> s;
bool input() {
  data.clear();
  for (int i = 0; i < 8; i++) {
    string str;
    if (!(cin >> str))
      return false;
    data += str;
  }
  return true;
}

bool check(string str) { return str == "01234567"; }
int main() {

  while (input()) {
    s.clear();
    s.insert(data);
    queue<P> que;
    que.push(P(data, 0));

    while (!que.empty()) {
      P tmp;
      tmp = que.front();
      que.pop();
      if (check(tmp.first)) {
        cout << tmp.second << endl;
        break;
      }
      int n_0;
      for (int i = 0; i < 8; i++)
        if (tmp.first[i] == '0')
          n_0 = i;
      for (int i = 0; i < 4; i++) {
        if ((n_0 + dx[i]) < 8 && (n_0 + dx[i]) >= 0) {
          P t = tmp;
          t.second++;
          swap(t.first[n_0], t.first[n_0 + dx[i]]);
          if (s.find(t.first) == s.end()) {
            que.push(t);
            s.insert(t.first);
          }
        }
      }
    }
  }
}
Problem p00121 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <assert.h>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
// #define _Test
typedef long long LL;
const int INF = 500000001;
const double EPS = 1e-9;
const double PI = acos(-1.0);
using namespace std;
int a[15], dir[4] = {1, -1, -4, 4};
char ch[15];
map<string, int> mm;
struct p {
  char s[15];
  int flag;
} fir, en;
queue<p> qq;
bool check(int x, int y) {
  if (x < 0 || x >= 8) {
    return false;
  }
  if (x == 3 && y == 4 || x == 4 && y == 3) {
    return false;
  }
  return true;
}

void bfs() {
  while (!qq.empty()) {
    en = qq.front();
    qq.pop();
    for (int i = 0; i < 4; i++) {
      fir.flag = en.flag + dir[i];
      if (check(fir.flag, en.flag)) {
        strcpy(fir.s, en.s);
        swap(fir.s[en.flag], fir.s[fir.flag]);
        if (mm.find(fir.s) == mm.end()) {
          mm[fir.s] = mm[en.s] + 1;
          qq.push(fir);
        }
      }
    }
  }
}
int main() {
#ifdef _Test
  freopen("test0.in", "r", stdin);
  freopen("test0.out", "w", stdout);
  srand(time(NULL));
#endif
  char c[100];
  while (~scanf("%d", &a[0])) {
    for (int i = 1; i < 8; i++) {
      scanf("%d", &a[i]);
    }
    for (int i = 0; i < 8; i++) {
      fir.s[i] = i + '0';
      c[i] = a[i] + '0';
    }
    c[8] = '\\0';
    fir.flag = 0;
    fir.s[8] = '\\0';
    mm[fir.s] = 1;
    qq.push(fir);
    bfs();

    printf("%d\\n", mm[c] - 1);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <string>
using namespace std;
const int MAX = 20;
struct node {
  char ch[MAX];
  int s;
  int step;
};

char sh[MAX];
int fx[] = {1, -1, 4, -4};
bool check(int a, int b) {
  if (b < 0 || b > 7)
    return false;
  if (a == 3 && b == 4)
    return false;
  else if (a == 4 && b == 3)
    return false;
  else
    return true;
}
int bfs(int star, char *ss) {
  map<string, int> s;
  s[sh] = 1;
  node a;
  queue<node> Q;
  strcpy(a.ch, sh);
  a.s = star;
  a.step = 0;
  Q.push(a);

  while (!Q.empty()) {
    node p = Q.front();
    Q.pop();
    if (strcmp(p.ch, "01234567") == 0) {
      return p.step;
    }
    for (int i = 0; i < 4; ++i) {
      int tx = p.s + fx[i];

      if (!check(p.s, tx))
        continue;
      node b;
      strcpy(b.ch, p.ch);
      swap(b.ch[p.s], b.ch[tx]);
      if (s.find(b.ch) == s.end()) {
        s[b.ch] = p.step + 1;
        b.s = tx;
        b.step = p.step + 1;
        Q.push(b);
      }
    }
  }
}
int main() {
  while (scanf("%c %c %c %c %c %c %c %c", &sh[0], &sh[1], &sh[2], &sh[3],
               &sh[4], &sh[5], &sh[6], &sh[7]) != EOF) {

    getchar();
    int star;
    sh[8] = '\\0';
    int i;
    for (i = 0; sh[i]; ++i)
      if (sh[i] == '0')
        star = i;

    printf("%d\\n", bfs(star, sh));
    //        map<string,int >::iterator ite;
    //        for(ite =s.begin();ite!=s.end();++ite)
    //            s.erase(ite);
    // printf("%d\\n",s["01234567"]);
  }
  return 0;
}
Problem p00121 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a) for (int i = 0; i < (a); i++)
#define reps(i, a, b) for (int i = (a); i < (b); i++)
#define INF 1e9
typedef vector<int> vi;
typedef vector<vi> vii;
typedef pair<int, int> pii;
int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
char memo[16434825];
class Data {
public:
  vii status;
  pii zero;
  int cnt;
  Data() {}
  Data(vii status, pii zero, int cnt) : status(status), zero(zero), cnt(cnt) {}
};
int to_s(vector<vi> s) {
  int ret = 0;
  int r = 1;
  rep(i, 8) {
    ret += s[i / 4][i % 4] * r;
    r *= 8;
  }
  return ret;
}
void solve() {
  memset(memo, -1, sizeof(memo));
  vii ans = {{0, 1, 2, 3}, {4, 5, 6, 7}};
  queue<Data> Q;
  Q.push(Data(ans, make_pair(0, 0), 0));
  while (!Q.empty()) {
    auto d = Q.front();
    Q.pop();
    int tmp = to_s(d.status);
    if (memo[tmp] != -1)
      continue;
    memo[tmp] = d.cnt;
    auto z = d.zero;
    rep(i, 4) {
      int nx = dx[i] + z.first, ny = dy[i] + z.second;
      if (nx < 0 || nx >= 4 || ny < 0 || ny >= 2)
        continue;
      auto p = d.status;
      swap(p[z.second][z.first], p[ny][nx]);
      Q.push(Data(p, make_pair(nx, ny), d.cnt + 1));
    }
  }
}
int main() {
  vii v = vii(2, vi(4, 0));
  solve();
  while (1) {
    cin >> v[0][0];
    if (cin.eof())
      break;
    reps(i, 1, 8) cin >> v[i / 4][i % 4];
    printf("%d\\n", memo[to_s(v)]);
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a) for (int i = 0; i < (a); i++)
#define reps(i, a, b) for (int i = (a); i < (b); i++)
#define INF 1e9
typedef vector<int> vi;
typedef vector<vi> vii;
typedef pair<int, int> pii;
int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
int memo[16434825];
class Data {
public:
  vii status;
  pii zero;
  int cnt;
  Data() {}
  Data(vii status, pii zero, int cnt) : status(status), zero(zero), cnt(cnt) {}
};
int to_s(vector<vi> s) {
  int ret = 0;
  int r = 1;
  rep(i, 8) {
    ret += s[i / 4][i % 4] * r;
    r *= 8;
  }
  return ret;
}
void solve() {
  memset(memo, -1, sizeof(memo));
  vii ans = {{0, 1, 2, 3}, {4, 5, 6, 7}};
  queue<Data> Q;
  Q.push(Data(ans, make_pair(0, 0), 0));
  while (!Q.empty()) {
    auto d = Q.front();
    Q.pop();
    int tmp = to_s(d.status);
    if (memo[tmp] != -1)
      continue;
    memo[tmp] = d.cnt;
    auto z = d.zero;
    rep(i, 4) {
      int nx = dx[i] + z.first, ny = dy[i] + z.second;
      if (nx < 0 || nx >= 4 || ny < 0 || ny >= 2)
        continue;
      auto p = d.status;
      swap(p[z.second][z.first], p[ny][nx]);
      Q.push(Data(p, make_pair(nx, ny), d.cnt + 1));
    }
  }
}
int main() {
  vii v = vii(2, vi(4, 0));
  solve();
  while (1) {
    cin >> v[0][0];
    if (cin.eof())
      break;
    reps(i, 1, 8) cin >> v[i / 4][i % 4];
    printf("%d\\n", memo[to_s(v)]);
  }
  return 0;
}
Problem p00121 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define REP(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
map<int, int> d; // 基本状態から状態iへの最短手数
const int dir[4] = {-4, -1, 1, 4};
int encode(int a[8]) {
  int res = 0;
  rep(i, 8) res |= a[i] << (i * 3);
  return res;
}
void decode(int x, int a[8]) { rep(i, 8) a[i] = (x >> (i * 3)) & 7; }
void bfs() {
  queue<int> cnd; // condition（遷移する状態）
  cnd.push(16434824);
  d[16434824] = 0;
  while (cnd.size()) {
    int sp[8]; // 作業用
    int bcnd = cnd.front();
    cnd.pop();
    decode(bcnd, sp);
    int bz = find(sp, sp + 8, 0) - sp;
    rep(i, 4) {
      int nz = bz + dir[i];
      if (nz < 0 || nz >= 8 || bz + nz == 7)
        continue;
      swap(sp[bz], sp[nz]);
      int ncnd = encode(sp);
      if (d.count(ncnd) == 0) {
        d[ncnd] = d[bcnd] + 1;
        cnd.push(ncnd);
      }
      swap(sp[bz], sp[nz]);
    }
  }
}
int main() {
  bfs();
  while (true) {
    int a[8];
    rep(i, 8) if (scanf("%d", a + i) == EOF) return 0;
    printf("%d\\n", d[encode(a)]);
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define REP(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
map<int, int> d; // 基本状態から状態iへの最短手数
const int dir[4] = {-4, -1, 1, 4};
int encode(int a[8]) {
  int res = 0;
  rep(i, 8) res |= a[i] << (i * 3);
  return res;
}
void decode(int x, int a[8]) { rep(i, 8) a[i] = (x >> (i * 3)) & 7; }
void bfs() {
  queue<int> cnd; // condition（遷移する状態）
  cnd.push(16434824);
  d[16434824] = 0;
  while (cnd.size()) {
    int sp[8]; // 作業用
    int bcnd = cnd.front();
    cnd.pop();
    decode(bcnd, sp);
    int bz;
    rep(i, 8) if (sp[i] == 0) bz = i;
    rep(i, 4) {
      int nz = bz + dir[i];
      if (nz < 0 || nz >= 8 || bz + nz == 7)
        continue;
      swap(sp[bz], sp[nz]);
      int ncnd = encode(sp);
      if (d.count(ncnd) == 0) {
        d[ncnd] = d[bcnd] + 1;
        cnd.push(ncnd);
      }
      swap(sp[bz], sp[nz]);
    }
  }
}
int main() {
  bfs();
  while (true) {
    int a[8];
    rep(i, 8) if (!scanf("%d", a + i)) return 0;
    printf("%d\\n", d[encode(a)]);
  }
  return 0;
}
Problem p00121 (C++) — buggy submission, verdict: Time Limit Exceeded

// AOJ 0121
#include <cstdio>
#include <map>
#include <queue>
#include <string>
#define rep(i, a) for (int i = 0; i != (a); ++i)

const int d[4] = {-4, 1, 4, -1};
std::map<std::string, int> m;

void bfs() {
  std::queue<std::string> que;
  que.push("01234567");

  while (!que.empty()) {
    std::string s = que.front();
    que.pop();
    int pos = s.find('0');

    rep(i, 4) {
      std::string ns = s;
      int npos = pos + d[i];

      if (npos >= 0 && npos < 8 && !(pos == 3 && i == 1) &&
          !(pos == 4 && i == 3)) {
        std::swap(ns[pos], ns[npos]);

        if (!m.count(ns)) {
          que.push(ns);
          m[ns] = m[s] + 1;
        }
      }
    }
  }

  return;
}

int main() {
  m["01234567"] = 0;
  bfs();

  while (true) {
    std::string s;
    rep(i, 8) {
      int a;
      if (scanf("%d", &a) == EOF)
        return 0;
      s += a + '0';
    }

    printf("%d\\n", m[s]);
  }

  return 0;
}

Error: TLE

Fixed submission:

// AOJ 0121
#include <cstdio>
#include <map>
#include <queue>
#include <string>
#define rep(i, a) for (int i = 0; i != (a); ++i)

const int d[4] = {-4, 1, 4, -1};
std::map<std::string, int> m;

int main() {
  while (true) {
    m.clear();
    std::string s;
    rep(i, 8) {
      int a;
      if (scanf("%d", &a) == EOF)
        return 0;

      s += a + '0';
    }

    std::queue<std::string> que;
    que.push(s);

    while (!que.empty()) {
      std::string s = que.front();
      que.pop();
      int zero;

      bool fl = true;
      for (int i = 0; i != 8; ++i) {
        fl &= s[i] - '0' == i;
        if (s[i] == '0')
          zero = i;
      }

      if (fl) {
        printf("%d\\n", m[s]);
        break;
      }

      rep(i, 4) {
        std::string ns = s;
        int nz = zero + d[i];

        if (nz >= 0 && nz < 8 && !(zero == 3 && i == 1) &&
            !(zero == 4 && i == 3)) {
          std::swap(ns[zero], ns[nz]);

          if (!m[ns]) {
            que.push(ns);
            m[ns] = m[s] + 1;
          }
        }
      }
    }
  }

  return 0;
}
Problem p00121 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <queue>
#include <set>
#define SWAP(A, B, C) ((C) = (A), (A) = (B), (B) = (C))
using namespace std;
class State {
public:
  int p[2][4];
  int cost;
  int zx, zy;
  bool operator<(const State &s) const {
    for (int i = 0; i < 2; i++) {
      for (int j = 0; j < 4; j++) {
        if (p[i][j] != s.p[i][j])
          return p[i][j] < s.p[i][j];
      }
    }
    return false;
  }
};

int dx[] = {0, 1, 0, -1}, dy[] = {-1, 0, 1, 0};
int main() {
  State a;
  for (int i = 0; i < 2; i++)
    for (int j = 0; j < 4; j++)
      a.p[i][j] = i * 4 + j;
  a.cost = 0;
  a.zx = 0;
  a.zy = 0;
  set<State> data;
  data.insert(a);
  queue<State> q;
  q.push(a);

  while (!q.empty()) {
    State b = q.front();
    q.pop();
    for (int i = 0; i < 4; i++) {
      State tb = b;
      int nx = tb.zx + dx[i];
      int ny = tb.zy + dy[i];
      if (nx < 0 || nx >= 4 || ny < 0 || ny >= 2)
        continue;
      tb.cost++;
      int temp;
      SWAP(tb.p[b.zy][b.zx], tb.p[ny][nx], temp);
      tb.zy = ny;
      tb.zx = nx;
      if (data.find(tb) != data.end())
        continue;
      data.insert(tb);
      q.push(tb);
    }
  }
  State input;
  while (cin >> input.p[0][0]) {
    for (int i = 1; i < 8; i++) {
      cin >> input.p[i / 4][i % 4];
    }
    cout << data.find(input)->cost << endl;
  }
}

Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <iostream>
#include <queue>
#define SWAP(A, B, C) ((C) = (A), (A) = (B), (B) = (C))
#define mp make_pair
using namespace std;
typedef pair<int, int> s;
typedef pair<s, int[2][4]> P;
// 0:上    1: 右   2:下   3:左
int dx[] = {0, 1, 0, -1}, dy[] = {-1, 0, 1, 0};
int ar[2][4] = {{0, 1, 2, 3}, {4, 5, 6, 7}};
int main() {
  //          方向               &#27493;数
  while (true) {
    P a;
    a.first.first = -1;
    a.first.second = 0;
    for (int i = 0; i < 2; i++)
      for (int j = 0; j < 4; j++)
        cin >> a.second[i][j];
    queue<P> q;
    q.push(a);
    while (1) {
      P b = q.front();
      q.pop();
      bool flag = false, reach = true;
      int i, j, zx, zy, dir = b.first.first;
      for (i = 0; i < 2; i++)
        for (j = 0; j < 4; j++) {
          if (ar[i][j] != b.second[i][j])
            reach = false;
          if (b.second[i][j] == 0) {
            zx = j;
            zy = i;
          }
        }
      if (reach) {
        cout << b.first.second << endl;
        break;
      }
      for (int i = 0, k = 2; i < 4; i++) {
        int nx = zx + dx[i], ny = zy + dy[i], e;
        if (i == 2)
          k *= -1;
        if (nx >= 0 && nx < 4 && ny >= 0 && ny < 2 && dir != i) {
          P temp;
          temp.first.first = i + k;
          temp.first.second = b.first.second + 1;
          for (int i = 0; i < 2; i++)
            for (int j = 0; j < 4; j++)
              temp.second[i][j] = b.second[i][j];
          SWAP(temp.second[zy][zx], temp.second[ny][nx], e);
          q.push(temp);
        }
      }
    }
  }
  return 0;
}