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

#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <vector>

#define rep(i, b) for (int i = 0; i < int(b); i++)

using namespace std;

map<int, int> ans;
typedef vector<int> vi;

struct state {
  state(int arr_[8], int step) : step(step) { rep(i, 8) arr[i] = arr_[i]; }
  int arr[8];
  int step;
};

int main() {
  queue<state> q;
  int a[8] = {0, 1, 2, 3, 4, 5, 6, 7};
  q.push({a, 0});

  while (q.size()) {
    auto s = q.front();
    q.pop();
    int h = 0;
    int free;
    int arr[8];

    rep(i, 8) {
      h |= s.arr[i] << (i * 3);
      if (s.arr[i] == 0)
        free = i;
      arr[i] = s.arr[i];
    }
    // printf("%i\\n", h);

    if (ans.find(h) != ans.end())
      continue;
    ans[h] = s.step;

    int other = free;
    if (free < 4)
      other += 4;
    else
      other -= 4;

    swap(arr[free], arr[other]);
    q.push(state(arr, s.step + 1));
    swap(arr[free], arr[other]);

    if (free % 4 != 0) {
      swap(arr[free], arr[free - 1]);
      q.push(state(arr, s.step + 1));
      swap(arr[free], arr[free - 1]);
    }
    if (free % 4 != 3) {
      swap(arr[free], arr[free + 1]);
      q.push(state(arr, s.step + 1));
      swap(arr[free], arr[free + 1]);
    }
  }

  while (1) {
    int h = 0;
    rep(i, 8) {
      int t;
      if (!(cin >> t))
        return 0;
      // cout << t << endl;
      h |= (t << (i * 3));
    }
    cout << ans[h] << endl;
  }
}

Error: MLE

Fixed submission:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <vector>

#define rep(i, b) for (int i = 0; i < int(b); i++)

using namespace std;

int ans[1 << 24];
typedef vector<int> vi;

struct state {
  state(int arr_[8], int step) : step(step) { rep(i, 8) arr[i] = arr_[i]; }
  int arr[8];
  int step;
};

int main() {
  memset(ans, -1, sizeof(ans));
  queue<state> q;
  int a[8] = {0, 1, 2, 3, 4, 5, 6, 7};
  q.push({a, 0});

  while (q.size()) {
    auto s = q.front();
    q.pop();
    int h = 0;
    int free;
    int arr[8];

    rep(i, 8) {
      h |= s.arr[i] << (i * 3);
      if (s.arr[i] == 0)
        free = i;
      arr[i] = s.arr[i];
    }

    if (ans[h] != -1)
      continue;
    ans[h] = s.step;

    int other = free;
    if (free < 4)
      other += 4;
    else
      other -= 4;

    swap(arr[free], arr[other]);
    q.push(state(arr, s.step + 1));
    swap(arr[free], arr[other]);

    if (free % 4 != 0) {
      swap(arr[free], arr[free - 1]);
      q.push(state(arr, s.step + 1));
      swap(arr[free], arr[free - 1]);
    }
    if (free % 4 != 3) {
      swap(arr[free], arr[free + 1]);
      q.push(state(arr, s.step + 1));
      swap(arr[free], arr[free + 1]);
    }
  }

  while (1) {
    int h = 0;
    rep(i, 8) {
      int t;
      if (!(cin >> t))
        return 0;
      // cout << t << endl;
      h |= (t << (i * 3));
    }
    cout << ans[h] << endl;
  }
}
Problem p00121 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define INF (1e9 + 1)
// #define INF (1LL<<59)

map<vector<int>, int> mp;

int main() {
  vector<int> ans(8);
  rep(i, 8) ans[i] = i;

  queue<vector<int>> que;
  que.push(ans);
  mp[ans] = 0;

  while (que.size()) {
    vector<int> q = que.front();
    que.pop();
    int x, y;
    rep(i, 8) if (q[i] == 0) {
      y = i / 4;
      x = i % 4;
    }
    int dy[] = {1, 0, -1, 0};
    int dx[] = {0, 1, 0, -1};

    rep(i, 4) {
      int ddy = y + dy[i];
      int ddx = x + dx[i];
      if (ddy < 0 || ddx < 0 || ddy >= 2 || ddx >= 4)
        continue;
      vector<int> t = q;
      swap(t[4 * ddy + ddx], t[4 * y + x]);

      if (mp.count(t) != 0)
        continue;
      que.push(t);
      mp[t] = mp[q] + 1;
    }
  }

  vector<int> v(8);
  while (cin >> v[0]) {
    rep(i, 7) cin >> v[i + 1];

    cout << mp[v] << endl;
  }
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define INF (1e9 + 1)
// #define INF (1LL<<59)

map<vector<int>, int> mp;

void solve(vector<int> v, int c) {
  mp[v] = min(mp[v], c);
  int x, y;
  rep(i, 8) if (v[i] == 0) {
    y = i / 4;
    x = i % 4;
  }
  int dy[] = {1, 0, -1, 0};
  int dx[] = {0, 1, 0, -1};

  rep(i, 4) {
    int ddy = y + dy[i];
    int ddx = x + dx[i];
    if (ddy < 0 || ddx < 0 || ddy >= 2 || ddx >= 4)
      continue;
    vector<int> t = v;
    swap(t[4 * ddy + ddx], t[4 * y + x]);
    if (mp.count(t) != 0)
      continue;
    solve(t, c + 1);
  }
}

int main() {
  vector<int> v(8);
  while (cin >> v[0]) {
    mp.clear();
    rep(i, 7) cin >> v[i + 1];

    vector<int> ans(8);
    rep(i, 8) ans[i] = i;

    queue<vector<int>> que;
    que.push(ans);
    mp[ans] = 0;

    while (que.size()) {
      vector<int> q = que.front();
      que.pop();
      int x, y;
      rep(i, 8) if (q[i] == 0) {
        y = i / 4;
        x = i % 4;
      }
      int dy[] = {1, 0, -1, 0};
      int dx[] = {0, 1, 0, -1};

      rep(i, 4) {
        int ddy = y + dy[i];
        int ddx = x + dx[i];
        if (ddy < 0 || ddx < 0 || ddy >= 2 || ddx >= 4)
          continue;
        vector<int> t = q;
        swap(t[4 * ddy + ddx], t[4 * y + x]);
        if (mp.count(t) != 0)
          continue;

        if (mp.count(t) == 0)
          que.push(t);
        mp[t] = mp[q] + 1;
      }
    }
    cout << mp[v] << endl;
  }
}
Problem p00121 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

map<string, bool> mpp1;
map<string, int> mpp2;

struct P {
  int c;
  string str;
  int x;
  int y;
};

int xdir[4] = {0, 1, 0, -1}, ydir[4] = {1, 0, -1, 0};

void func(void) {
  string str = "";
  char a[2][8];
  queue<P> que;
  for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 4; j++) {
      str += (char)('0' + (4 * i + j));
    }
  }
  P p, q;
  p.str = str;
  p.c = p.x = p.y = 0;
  que.push(p);
  mpp1[str] = true;
  mpp2[str] = 0;

  while (!que.empty()) {
    p = que.front();
    que.pop();
    for (int i = 0; i < 4; i++) {
      int dx = p.x + xdir[i], dy = p.y + ydir[i];
      str = p.str;
      if (0 <= dx && dx < 4 && 0 <= dy && dy < 2) {
        char w = str[dx + 4 * dy];
        str[dx + 4 * dy] = str[p.x + p.y * 4];
        str[p.x + p.y * 4] = w;
        if (!mpp1[str]) {
          mpp1[str] = true;
          q.c = mpp2[str] = p.c + 1;
          q.str = str;
          q.x = dx;
          q.y = dy;
          que.push(q);
        }
      }
    }
  }

  return;
}

int main() {
  int n;
  char w;
  string str;
  func();

  while (cin >> w) {
    str = "";
    str += w;
    for (int i = 1; i < 8; i++) {
      cin >> w;
      str += w;
    }

    cout << mpp2[str] << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

struct St {
  string str;
  int cnt;
};

int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};

string getStr(char a[2][4]) {
  string str = "";
  for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 4; j++) {
      str += a[i][j];
    }
  }

  return str;
}

int func(string str) {
  int r = 0;
  St st1, st2;
  queue<St> que;
  map<string, bool> mpp;
  mpp[str] = true;
  st1.str = str;
  st1.cnt = 0;
  que.push(st1);

  while (!que.empty()) {
    st1 = que.front();
    que.pop();
    if (st1.str == "01234567") {
      r = st1.cnt;
      break;
    }
    char a[2][4];
    string s = st1.str;
    int p, q;
    for (int i = 0; i < 2; i++) {
      for (int j = 0; j < 4; j++) {
        a[i][j] = s[i * 4 + j];
        if (a[i][j] == '0') {
          p = i;
          q = j;
        }
      }
    }

    for (int i = 0; i < 4; i++) {
      int xdir = q + dx[i], ydir = p + dy[i];
      if (0 <= xdir && xdir < 4 && 0 <= ydir && ydir < 2) {
        char w = a[p][q];
        a[p][q] = a[ydir][xdir];
        a[ydir][xdir] = w;
        string s2 = getStr(a);
        w = a[p][q];
        a[p][q] = a[ydir][xdir];
        a[ydir][xdir] = w;

        if (!mpp[s2]) {
          mpp[s2] = true;
          st2.str = s2;
          st2.cnt = st1.cnt + 1;
          que.push(st2);
        }
      }
    }
  }

  return r;
}

int main() {
  char t;

  while (cin >> t) {
    string s = "";
    s += t;
    for (int i = 1; i < 8; i++) {
      cin >> t;
      s += t;
    }

    cout << func(s) << endl;
  }

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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <queue>
using namespace std;
short rule[8][5] = {{2, 1, 4}, {3, 0, 2, 5}, {3, 1, 3, 6}, {2, 2, 7},
                    {2, 0, 5}, {3, 1, 4, 6}, {3, 2, 5, 7}, {2, 3, 6}};
short memo[16777216];
class P {
public:
  int data[8];
  int cnt;
  P() {}
  P(int a[8], int cnt) : cnt(cnt) {
    for (int i = 0; i < 8; i++) {
      data[i] = a[i];
    }
  }
};
int getIndex(int *a) {
  int res = 0;
  int r = 1;
  for (int i = 0; i < 8; i++) {
    res += a[i] * r;
    r *= 8;
  }
  return res;
}
int main() {
  memset(memo, -1, sizeof(memo));
  queue<P> Q;
  int F[] = {0, 1, 2, 3, 4, 5, 6, 7};
  Q.push(P(F, 0));
  int cnt = 0;
  while (!Q.empty()) {
    P p = Q.front();
    Q.pop();
    int id = getIndex(p.data);
    if (memo[id] != -1)
      continue;
    memo[id] = p.cnt;
    int zero;
    for (int i = 0; i < 8; i++) {
      if (p.data[i] == 0) {
        zero = i;
        break;
      }
    }
    for (int i = 0; i < rule[zero][0]; i++) {
      swap(p.data[zero], p.data[rule[zero][i + 1]]);
      Q.push(P(p.data, p.cnt + 1));
      swap(p.data[zero], p.data[rule[zero][i + 1]]);
    }
  }
  int in[10];
  while (~scanf("%d%d%d%d%d%d%d%d", &in[0], &in[1], &in[2], &in[3], &in[4],
                &in[5], &in[6], &in[7])) {
    int id = getIndex(in);
    printf("%d\\n", memo[id]);
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <queue>
using namespace std;
int rule[8][5] = {{2, 1, 4}, {3, 0, 2, 5}, {3, 1, 3, 6}, {2, 3, 7},
                  {2, 0, 5}, {3, 1, 4, 6}, {3, 2, 5, 7}, {2, 3, 6}};
int memo[16777216];
class P {
public:
  int data[8];
  int cnt;
  P() {}
  P(int a[8], int cnt) : cnt(cnt) {
    for (int i = 0; i < 8; i++) {
      data[i] = a[i];
    }
  }
};
int getIndex(int *a) {
  int res = 0;
  int r = 1;
  for (int i = 7; i >= 0; i--) {
    res += a[i] * r;
    r *= 8;
  }
  return res;
}
int main() {
  memset(memo, -1, sizeof(memo));
  queue<P> Q;
  int F[] = {0, 1, 2, 3, 4, 5, 6, 7};
  Q.push(P(F, 0));
  memo[getIndex(F)] = 0;
  while (!Q.empty()) {
    P p = Q.front();
    Q.pop();
    int place;
    for (int i = 0; i < 8; i++) {
      if (p.data[i] == 0) {
        place = i;
        break;
      }
    }
    for (int i = 0; i < rule[place][0]; i++) {
      swap(p.data[place], p.data[rule[place][i + 1]]);
      int id = getIndex(p.data);
      if (memo[id] != -1) {
        swap(p.data[place], p.data[rule[place][i + 1]]);
        continue;
      }
      memo[id] = p.cnt + 1;
      Q.push(P(p.data, p.cnt + 1));
      swap(p.data[place], p.data[rule[place][i + 1]]);
    }
  }
  int in[10];
  while (~scanf("%d%d%d%d%d%d%d%d", &in[0], &in[1], &in[2], &in[3], &in[4],
                &in[5], &in[6], &in[7])) {
    int id = getIndex(in);
    printf("%d\\n", memo[id]);
  }
  return 0;
}
Problem p00121 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

// #define int long long
#define FOR(i, j, k) for (int i = j; i < (int)k; ++i)
#define rep(i, j) FOR(i, 0, j)
#define INF (1 << 30)

typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<P, int> Pi;

const int MOD = 1e9 + 7;
const int dy[] = {0, 0, 1, -1};
const int dx[] = {1, -1, 0, 0};

template <class T> void chmin(T &a, const T &b) { a = min(a, b); }
template <class T> void chmax(T &a, const T &b) { a = max(a, b); }

struct puzzle {
  string s;
  int now, cnt;
};

map<string, int> mp;

int bfs(puzzle m) {
  queue<puzzle> que;
  que.push(m);
  const int d[] = {1, -1, 4, -4};
  while (!que.empty()) {
    puzzle p = que.front();
    que.pop();
    if (mp[p.s] != 0)
      continue;
    mp[p.s] = p.cnt;
    rep(i, 4) {
      int next = p.now + d[i];
      if (next < 0 || next >= 8)
        continue;
      if ((p.now == 3 && d[i] == 1) || (p.now == 4 && d[i] == -1))
        continue;
      puzzle tmp = p;
      swap(tmp.s[p.now], tmp.s[next]);
      tmp.now = next;
      ++tmp.cnt;
      que.push(tmp);
    }
  }
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  string t = "01234567";
  puzzle p = {t, 0, 0};
  bfs(p);
  char c;
  while (cin >> c) {
    int zero = 0;
    t[0] = c;
    rep(i, 7) {
      cin >> c;
      t[i + 1] = c;
      if (t[i + 1] == '0') {
        zero = i + 1;
      }
    }
    (t == "01234567") ? cout << 0 : cout << mp[t];
    cout << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

// #define int long long
#define FOR(i, j, k) for (int i = j; i < (int)k; ++i)
#define rep(i, j) FOR(i, 0, j)
#define INF (1 << 30)

typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<P, int> Pi;

const int MOD = 1e9 + 7;
const int dy[] = {0, 0, 1, -1};
const int dx[] = {1, -1, 0, 0};

template <class T> void chmin(T &a, const T &b) { a = min(a, b); }
template <class T> void chmax(T &a, const T &b) { a = max(a, b); }

struct puzzle {
  string s;
  int now, cnt;
};

int bfs(puzzle m) {
  queue<puzzle> que;
  que.push(m);
  map<string, bool> mp;
  const int d[] = {1, -1, 4, -4};

  while (!que.empty()) {
    puzzle p = que.front();
    que.pop();
    if (p.s == "01234567")
      return p.cnt;
    if (mp[p.s])
      continue;
    mp[p.s] = true;
    rep(i, 4) {
      int next = p.now + d[i];
      if (next < 0 || next >= 8)
        continue;
      if ((p.now == 3 && d[i] == 1) || (p.now == 4 && d[i] == -1))
        continue;
      puzzle tmp = p;
      swap(tmp.s[p.now], tmp.s[next]);
      tmp.now = next;
      ++tmp.cnt;
      que.push(tmp);
    }
  }
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  string t = "00000000";
  char c;
  while (cin >> c) {
    int zero = 0;
    t[0] = c;
    rep(i, 7) {
      cin >> c;
      t[i + 1] = c;
      if (t[i + 1] == '0') {
        zero = i + 1;
      }
    }
    puzzle p = {t, zero, 0};
    cout << bfs(p) << endl;
  }
  return 0;
}
Problem p00121 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#define INF 10000000
#define I64 long long int
#define FOR(a, start, end) for (I64 a = (start); a < (end); a++)
using namespace std;

int main() {
  // 4ûüpzñ
  int vec[] = {1, -1, 4, -4};
  queue<string> que;
  que.push("01234567");

  // TõÏÝe[u
  map<string, int> d;

  //"01234567"©çtü«ÉDæTõµÈªçTõÏÝe[uðßé
  while (!que.empty()) {
    string str = que.front();
    que.pop();
    int zero = str.find("0");

    string str2;
    FOR(i, 0, 4) {
      int next_z;
      next_z = zero + vec[i];
      if (next_z >= 0 && next_z <= 7 &&
          !((zero == 3 && next_z == 4) || (zero == 4 && next_z == 3))) {
        str2 = str;
        swap(str2[zero], str2[next_z]);
        if (d.find(str2) == d.end()) {
          que.push(str2);
          d[str2] = d[str] + 1;
        }
      }
    }
  }

  // üÍ©ç'
  // 'ð¢ÄTõÏÝe[u©ç¦ðoÍ
  string num;
  while (getline(cin, num)) {
    string::iterator end_it = remove(num.begin(), num.end(), ' ');
    num.erase(end_it, num.end());
    cout << d[num] << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <typeinfo>
#include <vector>
#define INF 10000000
#define I64 long long int
#define FOR(a, start, end) for (I64 a = (start); a < (end); a++)
#define FORn(a, start, end, n) for (I64 a = (start); a < (end); a += (n))
using namespace std;

typedef pair<string, int> P;

class SevenPuzzle {
  string board;
  string match;

public:
  SevenPuzzle(string str) : board(str) { match = "01234567"; }
  int bfs();
};

int SevenPuzzle::bfs() {
  // 4ツ陛サツ古シツ用
  int vi[4] = {0, 0, -1, 1}, vj[4] = {-1, 1, 0, 0};

  queue<P> que;
  que.push(P(board, 0));

  map<string, int> d;

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

    if (d[p.first])
      continue;

    // ツ環ョツ青ャツ形ツづづ個氾、ツ較
    bool flag = true;
    FOR(i, 0, 8)
    if (p.first[i] != match[i])
      flag = false;
    if (flag)
      return p.second;

    d[p.first] = p.second;

    // present_0(1ツ篠淞個ウツ配ツ療ア), present_0(i, j),
    // next_0(1ツ篠淞個ウツ配ツ療ア),  next_0(i, j)
    int pz, pi, pj, nz, ni, nj;

    // 0ツづ個暗環置
    FOR(i, 0, 8) {
      if (p.first[i] == '0') {
        pz = i;
        pi = i / 4;
        pj = i % 4;
        break;
      }
    }

    FOR(i, 0, 4) {
      string str = p.first;

      ni = pi + vi[i];
      nj = pj + vj[i];
      nz = ni * 4 + nj;

      if (ni >= 0 && ni <= 1 && nj >= 0 && nj <= 3) {
        // ツ青板篠堋禿シツづェツ妥鳴つヲ
        str[pz] = str[nz];
        str[nz] = '0';

        que.push(P(str, p.second + 1));
      }
    }
  }
}

int main() {
  string temp, str;
  while (getline(cin, temp)) {
    FORn(i, 0, 15, 2) str += temp[i];
    SevenPuzzle obj(str);
    cout << obj.bfs() << endl;
    str.clear();
  }
}
Problem p00121 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {

  int nx;
  int cnt = 0;
  int px[4] = {1, -1, 4, -4};
  string str = "        ";
  map<string, int> m;
  queue<int> q;
  queue<string> sq;

  q.push(0);
  sq.push("01234567");

  while (!q.empty()) {
    int siz = q.size();
    for (int nsiz = 0; nsiz < siz; nsiz++) {
      nx = q.front();
      q.pop();
      str = sq.front();
      sq.pop();

      if (m.count(str) == 0) {
        m[str] = cnt;
        for (int i = 0; i < 4; i++) {
          if (i < 2 && nx + px[i] >= 0 && nx + px[i] < 8 &&
              nx / 4 == (nx + px[i]) / 4) {
            swap(str[nx], str[nx + px[i]]);
            q.push(nx + px[i]);
            sq.push(str);
            swap(str[nx], str[nx + px[i]]);
          }
          if (i > 1 && nx + px[i] >= 0 && nx + px[i] < 8) {
            swap(str[nx], str[nx + px[i]]);
            q.push(nx + px[i]);
            sq.push(str);
            swap(str[nx], str[nx + px[i]]);
          }
        }
      }
    }
    cnt++;
  }

  while (1) {
    for (int i = 0; i < 8; i++) {
      if (!(cin >> str[i]))
        return 0;
    }
    cout << m[str] << endl;
  }

  return (0);
}

Error: TLE

Fixed submission:

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

int main() {

  int nx;
  int cnt = 0;
  int px[4] = {1, -1, 4, -4};
  string str = "        ";
  map<string, int> m;
  queue<int> q;
  queue<string> sq;

  q.push(0);
  sq.push("01234567");

  while (!q.empty()) {
    int siz = q.size();
    for (int nsiz = 0; nsiz < siz; nsiz++) {
      nx = q.front();
      q.pop();
      str = sq.front();
      sq.pop();

      if (m.count(str) == 0) {
        m[str] = cnt;
        for (int i = 0; i < 4; i++) {
          if (i < 2 && nx + px[i] >= 0 && nx + px[i] < 8 &&
              nx / 4 == (nx + px[i]) / 4) {
            swap(str[nx], str[nx + px[i]]);
            q.push(nx + px[i]);
            sq.push(str);
            swap(str[nx], str[nx + px[i]]);
          }
          if (i > 1 && nx + px[i] >= 0 && nx + px[i] < 8) {
            swap(str[nx], str[nx + px[i]]);
            q.push(nx + px[i]);
            sq.push(str);
            swap(str[nx], str[nx + px[i]]);
          }
        }
      }
    }
    cnt++;
  }

  while (1) {
    for (int i = 0; i < 8; i++) {
      cin >> str[i];
      if (str[i] == EOF)
        return (0);
    }
    cout << m[str] << endl;
  }

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

#include <bits/stdc++.h>

using namespace std;

struct Puzzle {
  int x;
  string s;
};

int vx[] = {1, -1, 4, -4};
map<string, int> res;

void solve() {

  queue<Puzzle> q;

  q.push((Puzzle){0, "01234567"});

  while (!q.empty()) {

    Puzzle now = q.front();
    q.pop();

    for (int i = 0; i < 4; ++i) {
      if (now.x + vx[i] >= 0 && now.x + vx[i] < 8 && !(now.x == 3 && i == 0) &&
          !(now.x == 4 && i == 1)) {

        Puzzle next = now;
        next.x = now.x + vx[i];
        swap(next.s[now.x], next.s[next.x]);

        if (!res[next.s]) {
          q.push(next);
          res[next.s] = res[now.s] + 1;
        }
      }
    }
  }
}

int main() {

  res["01234567"] = 1;

  solve();

  while (1) {

    char num;
    string start;

    for (int i = 0; i < 8; ++i) {
      if (!(cin >> num))
        return 0;
      start += num;
    }

    cout << res[start] - 1 << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

struct Card {
  int x, y, cnt;
  string stat[2];
};

int main() {

  int vx[] = {0, 1, 0, -1};
  int vy[] = {-1, 0, 1, 0};

  while (1) {

    char num;
    map<string, bool> check;
    queue<Card> q;
    Card start;

    start.stat[0] = "";
    start.stat[1] = "";

    for (int i = 0; i < 2; ++i) {
      for (int j = 0; j < 4; ++j) {
        if (!(cin >> num)) {
          return 0;
        } else if (num == '0') {
          start.x = j;
          start.y = i;
        }
        start.stat[i] += num;
      }
    }

    start.cnt = 0;
    q.push(start);

    while (!q.empty()) {

      Card now = q.front();
      q.pop();

      if (now.stat[0] + now.stat[1] == "01234567") {
        cout << now.cnt << endl;
        break;
      }

      for (int i = 0; i < 4; ++i) {

        Card next;
        next.x = now.x + vx[i];
        next.y = now.y + vy[i];
        next.cnt = now.cnt + 1;
        next.stat[0] = now.stat[0];
        next.stat[1] = now.stat[1];

        if (next.x >= 0 && next.x < 4 && next.y >= 0 && next.y < 2) {
          swap(next.stat[now.y][now.x], next.stat[next.y][next.x]);
          if (!check[next.stat[0] + next.stat[1]]) {
            check[next.stat[0] + next.stat[1]] = true;
            q.push(next);
          }
        }
      }
    }
  }
}