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

#include <bits/stdc++.h>
using namespace std;
#define ANSWER(x) cout << x << endl
#define debug(x) cout << #x << ": " << x
const int BA = 1000;
const int INFTY = (1 << 21);
typedef pair<int, string> p;
int main() {
  int up[3] = {-1, 1, 4};
  int under[3] = {-1, 1, -4};
  map<string, bool> m;
  map<string, int> ans;
  string s = "01234567", hoge = "";
  char c;
  queue<p> q;
  q.push(make_pair(0, s));
  while (!q.empty()) {
    p a = q.front();
    q.pop();
    int b = a.second.find('0');
    m[a.second] = true;
    ans[a.second] = a.first;
    a.first++;
    string ss;
    for (int i = 0; i < 3; i++) {
      ss = a.second;
      if (b < 4) {
        if (!(b == 0 && i == 0) && !(b == 3 && i == 1)) {
          swap(ss[b], ss[b + up[i]]);
          if (!m[ss]) {
            q.push(make_pair(a.first, ss));
          }
        }
      } else {
        if (!(b == 4 && i == 0) && !(b == 7 && i == 1)) {
          swap(ss[b], ss[b + under[i]]);
          if (!m[ss]) {
            q.push(make_pair(a.first, ss));
          }
        }
      }
    }
  }
  while (1) {
    cin >> c;
    if (cin.eof())
      break;
    hoge += c;
    for (int i = 1; i <= 7; i++) {
      cin >> c;
      hoge += c;
    }
    cout << ans[hoge] << endl;
    hoge = "";
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ANSWER(x) cout << x << endl
#define debug(x) cout << #x << ": " << x
const int BA = 1000;
const int INFTY = (1 << 21);
typedef pair<int, string> p;
int main() {
  int up[3] = {-1, 1, 4};
  int under[3] = {-1, 1, -4};
  while (1) {
    int ans;
    map<string, bool> m;
    string s = "";
    char c;
    cin >> c;
    if (cin.eof())
      break;
    s += c;
    for (int i = 1; i < 8; i++) {
      cin >> c;
      s += c;
    }
    queue<p> q;
    q.push(make_pair(0, s));
    while (!q.empty()) {
      p a = q.front();
      q.pop();
      if (a.second == "01234567") {
        cout << a.first << endl;

        break;
      }
      int b = a.second.find('0');
      m[a.second] = true;
      a.first++;
      string ss;
      for (int i = 0; i < 3; i++) {
        ss = a.second;
        if (b < 4) {
          if (!(b == 0 && i == 0) && !(b == 3 && i == 1)) {
            swap(ss[b], ss[b + up[i]]);
            if (!m[ss])
              q.push(make_pair(a.first, ss));
          }
        } else {
          if (!(b == 4 && i == 0) && !(b == 7 && i == 1)) {
            swap(ss[b], ss[b + under[i]]);
            if (!m[ss])
              q.push(make_pair(a.first, ss));
          }
        }
      }
    }
  }
}
Problem p00121 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

int main() {
  map<string, int> used;
  queue<string> que;
  que.push("01234567");
  for (int i = 0;; i++) {
    int siz = que.size();
    if (!siz)
      break;
    for (int j = 0; j < siz; j++) {
      string s = que.front();
      que.pop();
      if (used.find(s) != used.end())
        continue;
      used[s] = i;
      for (int k = 0; k < 8; k++) {
        if (s[k] == '0') {
          for (int l = 0; l < 4; l++) {
            int x = k / 4 + dx[l], y = k % 4 + dy[l];
            if (x >= 0 && x < 2 && y >= 0 && y < 4) {
              string t = s;
              t[k] = t[x * 4 + y];
              t[x * 4 + y] = '0';
              que.push(t);
            }
          }
        }
      }
    }
  }
  string s;
  while (getline(cin, s)) {
    string t;
    for (int i = 0; i < s.length(); i++) {
      if (s[i] != ' ')
        t += s.substr(i, 1);
    }
    cout << used[t] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

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

int main() {
  string s;
  while (getline(cin, s)) {
    string t = "";
    for (int i = 0; i < s.length(); i++) {
      if (s[i] != ' ')
        t += s.substr(i, 1);
    }
    map<string, bool> used;
    queue<string> que;
    que.push(t);
    for (int i = 0;; i++) {
      int siz = que.size();
      if (!siz)
        break;
      for (int j = 0; j < siz; j++) {
        t = que.front();
        que.pop();
        if (t == "01234567") {
          cout << i << endl;
          goto end;
        }
        if (used[t])
          continue;
        used[t] = true;
        for (int k = 0; k < 8; k++) {
          if (t[k] == '0') {
            int x = k / 4, y = k % 4;
            for (int l = 0; l < 4; l++) {
              int nx = x + dx[l], ny = y + dy[l];
              if (nx >= 0 && nx < 2 && ny >= 0 && ny < 4) {
                s = t;
                s[k] = s[nx * 4 + ny];
                s[nx * 4 + ny] = '0';
                if (!used[s])
                  que.push(s);
              }
            }
          }
        }
      }
    }
  end:
    continue;
  }
  return 0;
}
Problem p00121 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

typedef pair<int, string> P;

int main() {

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

  map<string, int> ans;

  ans["01234567"] = 0;

  queue<P> que;

  que.push(P(0, "01234567"));

  while (que.size()) {
    P p = que.front();
    que.pop();
    int k = ans[p.second];
    for (int i = 0; i < 4; i++) {

      int n = p.first + d[i];
      if (i == 0 && n == 3)
        continue;
      if (i == 1 && n == 4)
        continue;
      if (n >= 0 && n < 8) {
        string tmp;
        tmp = p.second;

        tmp[p.first] = tmp[n];
        tmp[n] = '0';

        if (ans.find(tmp) == ans.end()) {
          ans[tmp] = k + 1;
          que.push(P(n, tmp));
        }
      }
    }
  }
  string s;
  char t;
  while (cin >> t) {
    s += t;
    for (int i = 0; i < 7; i++) {
      cin >> t;
      s += t;
    }

    cout << ans[s] << endl;

    s.clear();
  }

  return 0;
}


Error: TLE

Fixed submission:

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

const int MAX_PTN = 76543211;

struct state {
  int i;
  int n;
  int t;
  state(int i, int n, int t) : i(i), n(n), t(t) {}
};

bool done[MAX_PTN];
int zi, n;

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

int make_nextnum(state cur, int nexti) {
  int nd = (cur.n / (int)pow(10, 7 - nexti)) % 10;
  int ret = cur.n + nd * (int)pow(10, 7 - cur.i) - nd * (int)pow(10, 7 - nexti);
  return ret;
}

int bfs() {
  queue<state> que;
  que.push(state(zi, n, 0));
  memset(done, false, sizeof done);
  done[n] = true;

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

    if (s.n == 1234567)
      return s.t;

    for (int i = 0; i < 4; i++) {
      int ni = s.i + di[i];
      int nn;

      if (s.i == 3 && di[i] == 1)
        continue;
      if (s.i == 4 && di[i] == -1)
        continue;
      if (ni < 0 || 7 < ni)
        continue;

      nn = make_nextnum(s, ni);

      if (done[nn])
        continue;

      done[nn] = true;
      que.push(state(ni, nn, s.t + 1));
    }
  }

  return -1;
}

int solve() { return bfs(); }

int main() {
  char s[32];

  while (cin >> s[0] >> s[1] >> s[2] >> s[3] >> s[4] >> s[5] >> s[6] >> s[7]) {
    int d = 10000000;
    if (s[0] < '0' || '9' < s[0])
      break;

    n = 0;
    for (int i = 0; i < 8; i++) {
      int a;
      a = s[i] - '0';
      n += a * d;
      d /= 10;

      if (a == 0) {
        zi = i;
      }
    }

    cout << solve() << endl;
  }

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

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

const int INF = 1e+9;
typedef pair<vector<int>, int> P;

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

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

  vector<int> init{0, 1, 2, 3, 4, 5, 6, 7};
  map<vector<int>, int> visited;

  queue<P> que;
  que.push(P(init, 0));
  while (!que.empty()) {
    P p = que.front();
    que.pop();
    vector<int> v = p.first;
    if (visited[v])
      continue;
    visited[v] = p.second;
    int zero;
    for (int i = 0; i < 8; ++i) {
      if (v[i] == 0) {
        zero = i;
        break;
      }
    }
    for (int i = 0; i < 4; ++i) {
      if (zero == 3 && i == 1)
        continue;
      if (zero == 4 && i == 0)
        continue;
      int next = zero + d[i];
      if (next >= 0 && next < 8) {
        int tmp = v[next];
        v[next] = v[zero];
        v[zero] = tmp;
        que.push(P(v, p.second + 1));
        tmp = v[next];
        v[next] = v[zero];
        v[zero] = tmp;
      }
    }
  }
  vector<int> vec;
  vec.resize(8);
  while (cin >> vec[0]) {
    for (int i = 1; i < 8; ++i) {
      cin >> vec[i];
    }
    if (vec == init)
      cout << "0" << endl;
    else {
      cout << visited[vec] << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

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

const int INF = 1e+9;
typedef pair<vector<int>, int> P;

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

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  vector<int> vec;
  vec.resize(10);
  while (cin >> vec[0]) {
    for (int i = 1; i < 8; ++i) {
      cin >> vec[i];
    }

    int ans = INF;
    map<vector<int>, bool> visited;
    queue<P> que;
    que.push(P(vec, 0));
    while (!que.empty()) {
      P p = que.front();
      que.pop();
      vector<int> v = p.first;
      if (visited[v])
        continue;
      visited[v] = true;
      int zero;
      bool flag = false;
      for (int i = 0; i < 8; ++i) {
        if (v[i] != i) {
          flag = true;
        }
        if (v[i] == 0) {
          zero = i;
          if (flag)
            break;
        }
        if (i == 7 && !flag) {
          ans = p.second;
          goto end;
        }
      }
      for (int i = 0; i < 4; ++i) {
        if (zero == 3 && i == 1)
          continue;
        if (zero == 4 && i == 0)
          continue;
        int next = zero + d[i];
        if (next >= 0 && next < 8) {
          int tmp = v[next];
          v[next] = v[zero];
          v[zero] = tmp;
          que.push(P(v, p.second + 1));
          tmp = v[next];
          v[next] = v[zero];
          v[zero] = tmp;
        }
      }
    }
  end:
    cout << ans << endl;
  }
  return 0;
}
Problem p00121 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <string>
using namespace std;
const string target = "01234567";
struct puzzle {
  string stat;
  int zero;
  puzzle(string _s, int _x) {
    stat = _s;
    zero = _x;
  }
};
int d[4] = {-1, 1, -4, 4};
int n;
int main() {
  queue<puzzle> que;
  map<string, int> done;
  que.push(puzzle(target, 0));
  done[target] = 0;
  while (!que.empty()) {
    puzzle now = que.front();
    que.pop();
    int pos = now.zero;
    for (int i = 0; i < 4; i++) {
      int nx = pos + d[i];
      if ((nx >= 0 && nx < 4 && pos >= 0 && pos < 4) ||
          (nx >= 4 && nx < 8 && pos >= 4 && pos < 8) ||
          (nx >= 0 && nx < 4 && pos >= 4 && pos < 8 && abs(nx - pos) == 4) ||
          (nx >= 4 && nx < 8 && pos >= 0 && pos < 4 && abs(nx - pos) == 4)) {
        string temp = now.stat;
        swap(temp[pos], temp[nx]);
        if (done.count(temp) == 0) {
          que.push(puzzle(temp, nx));
          done[temp] = done[now.stat] + 1;
        }
      }
    }
  }
  while (scanf("%d", &n) != EOF) {
    string stats = "";
    stats += (n + '0');
    for (int i = 1; i < 8; i++) {
      scanf("%d", &n);
      stats += (n + '0');
    }
    printf("%d\\n", done[stats]);
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#include <set>
#include <string>
using namespace std;
const string target = "01234567";
struct puzzle {
  string stat;
  int cnt;
  int zero;
  puzzle(string _s, int _c, int _x) {
    stat = _s;
    cnt = _c;
    zero = _x;
  }
};
int d[4] = {-1, 1, -4, 4};
int n, res, x;
int main() {
  while (scanf("%d", &n) != EOF) {
    string stats = "";
    stats += (n + '0');
    if (n == 0)
      x = 0;
    for (int i = 1; i < 8; i++) {
      scanf("%d", &n);
      stats += (n + '0');
      if (n == 0)
        x = i;
    }
    queue<puzzle> que;
    set<string> done;
    que.push(puzzle(stats, 0, x));
    done.insert(stats);
    while (!que.empty()) {
      puzzle now = que.front();
      que.pop();
      if (now.stat == target) {
        res = now.cnt;
        break;
      }
      int pos = now.zero;
      for (int i = 0; i < 4; i++) {
        int nx = pos + d[i];
        if ((nx >= 0 && nx < 4 && pos >= 0 && pos < 4) ||
            (nx >= 4 && nx < 8 && pos >= 4 && pos < 8) ||
            (nx >= 0 && nx < 4 && pos >= 4 && pos < 8 && abs(nx - pos) == 4) ||
            (nx >= 4 && nx < 8 && pos >= 0 && pos < 4 && abs(nx - pos) == 4)) {
          string temp = now.stat;
          swap(temp[pos], temp[nx]);
          if (done.count(temp) == 0) {
            que.push(puzzle(temp, now.cnt + 1, nx));
            done.insert(temp);
          }
        }
      }
    }
    printf("%d\\n", res);
  }
}
Problem p00121 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

struct puzzle {
  int f[8], space, cnt;
  bool operator<(const puzzle &p) const {
    rep(i, 8) {
      if (f[i] == p.f[i])
        continue;
      return f[i] < p.f[i];
    }
    return false;
  }
};
int dx[]{1, -1, 0, 0}, dy[]{0, 0, 1, -1};
map<int, int> ans;
queue<puzzle> que;
map<puzzle, bool> mp;

int main() {
  puzzle p = {{0, 1, 2, 3, 4, 5, 6, 7}, 0, 0};
  ans[1234567] = 0;
  puzzle u, v;
  que.push(p);
  mp[p] = true;
  while (!que.empty()) {
    u = que.front();
    que.pop();
    int s = 0, I = 10000000;
    rep(i, 8) {
      s += I * u.f[i];
      I /= 10;
    }
    if (s != 1234567 && ans[s] == 0)
      ans[s] = u.cnt;
    int sx = u.space / 4;
    int sy = u.space % 4;
    rep(i, 4) {
      int nx = sx + dx[i];
      int ny = sy + dy[i];
      if (nx < 0 || ny < 0 || nx >= 2 || ny >= 4)
        continue;
      v = u;
      swap(v.f[u.space], v.f[nx * 4 + ny]);
      v.space = nx * 4 + ny;
      if (!mp[v]) {
        mp[v] = true;
        v.cnt++;
        que.push(v);
      }
    }
  }
  while (1) {
    int s = 0, I = 10000000;
    rep(i, 8) {
      int d;
      if (!(~scanf("%d", &d)))
        return 0;
      s += I * d;
      I /= 10;
    }
    printf("%d\\n", ans[s]);
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

struct puzzle {
  int f[8], space, cnt;
  bool operator<(const puzzle &p) const {
    rep(i, 8) {
      if (f[i] == p.f[i])
        continue;
      return f[i] < p.f[i];
    }
    return false;
  }
};
int dx[]{1, -1, 0, 0}, dy[]{0, 0, 1, -1};
map<int, int> ans;
queue<puzzle> que;
map<puzzle, bool> mp;

int main() {
  puzzle p = {{0, 1, 2, 3, 4, 5, 6, 7}, 0, 0};
  ans[1234567] = 0;
  puzzle u, v;
  que.push(p);
  mp[p] = true;
  while (!que.empty()) {
    u = que.front();
    que.pop();
    int s = 0, I = 10000000;
    rep(i, 8) {
      s += I * u.f[i];
      I /= 10;
    }
    if (s != 1234567 && ans[s] == 0)
      ans[s] = u.cnt;
    int sx = u.space / 4;
    int sy = u.space % 4;
    rep(i, 4) {
      int nx = sx + dx[i];
      int ny = sy + dy[i];
      if (nx < 0 || ny < 0 || nx >= 2 || ny >= 4)
        continue;
      v = u;
      swap(v.f[u.space], v.f[nx * 4 + ny]);
      v.space = nx * 4 + ny;
      if (!mp[v]) {
        mp[v] = true;
        v.cnt++;
        que.push(v);
      }
    }
  }
  while (1) {
    int s = 0, I = 10000000;
    rep(i, 8) {
      int d;
      if (!scanf("%d", &d))
        return 0;
      s += I * d;
      I /= 10;
    }
    if (s != 1234567 && ans[s] == 0)
      puts("Hello,world!");
    printf("%d\\n", ans[s]);
  }
}
Problem p00121 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define foreach(c, i)                                                          \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)

typedef pair<string, int> Psi;

map<string, int> m;

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

int main() {
  queue<Psi> que;
  que.push(Psi(string("01234567"), 0));
  while (!que.empty()) {
    Psi p = que.front();
    int cost = p.second;
    que.pop();
    if (m.count(p.first))
      continue;
    m.insert(Psi(p.first, cost));

    int z_idx = -1;
    rep(i, 8) if (p.first[i] == '0') {
      z_idx = i;
      break;
    }

    for (int i = 0; i < 4; ++i) {
      if (z_idx == 3 && i == 3)
        continue;
      if (z_idx == 4 && i == 2)
        continue;
      int t_idx = z_idx + d[i];
      if (t_idx < 0 || t_idx >= 8)
        continue;
      string copy = p.first;
      swap(copy[z_idx], copy[t_idx]);
      que.push(Psi(copy, cost + 1));
    }
  }

  string a = "01234567";
  while (cin >> a[0] >> a[1] >> a[2] >> a[3] >> a[4] >> a[5] >> a[6] >> a[7]) {
    cout << m[a] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define foreach(c, i)                                                          \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)

int a[8], ar[2][4], ans;
const int dx[4] = {0, 0, -1, 1}, dy[4] = {1, -1, 0, 0};

bool check() {
  int ptr = 0;
  rep(i, 2) {
    rep(j, 4) {
      if (ar[i][j] != ptr)
        return false;
      ptr++;
    }
  }
  return true;
}

void dfs(int turn, int x, int y, int prev) {
  if (turn >= ans)
    return;
  if (check())
    ans = turn;
  rep(i, 4) {
    if ((prev == 0 && i == 1) || (prev == 1 && i == 0) ||
        (prev == 2 && i == 3) || (prev == 3 && i == 2)) {
      continue;
    }
    int tx = x + dx[i];
    int ty = y + dy[i];
    if (tx < 0 || ty < 0 || tx >= 4 || ty >= 2)
      continue;
    swap(ar[y][x], ar[ty][tx]);
    dfs(turn + 1, tx, ty, i);
    swap(ar[y][x], ar[ty][tx]);
  }
}

void solve() {
  int x, y;
  rep(i, 2) rep(j, 4) if (!ar[i][j]) { y = i, x = j; }
  ans = 30;
  dfs(0, x, y, -1);
  cout << ans << endl;
}

int main() {
  while (cin >> a[0] >> a[1] >> a[2] >> a[3] >> a[4] >> a[5] >> a[6] >> a[7]) {
    int ptr = 0;
    rep(i, 2) {
      rep(j, 4) { ar[i][j] = a[ptr++]; }
    }
    solve();
  }
  return 0;
}
Problem p00121 (C++) — buggy submission, verdict: Time Limit Exceeded

/*
 * Filename:    aoj0121.cpp
 * Desciption:  Desciption
 * Created:     2016-03-07
 * Author:      JIngwei Xu [mail:xu_jingwei@outlook.com]
 *
 */
#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <stdio.h>
#define INT_MAX 1 << 30
using namespace std;
// typedef long long ll;
const int INF = 0x7F;
int dx[5] = {1, 0, 0, -1, 0}, dy[5] = {0, 1, -1, 0, 0};
map<string, int> dp;
void bfs() {
  queue<string> que;
  que.push("01234567");
  dp["01234567"] = 0;
  while (!que.empty()) {
    string get = que.front();
    que.pop();
    int cr = 0;
    for (int i = 0; i < 8; i += 1) {
      if (get[i] == '0') {
        cr = i;
        break;
      }
    }
    int cx = cr / 4, cy = cr % 4;
    for (int i = 0; i < 4; i += 1) {
      int px = cx + dx[i], py = cy + dy[i];
      if (px < 0 || py < 0 || px > 1 || py > 3)
        continue;
      int pr = 4 * px + py;
      string ps = get;
      swap(ps[cr], ps[pr]);
      if (dp.find(ps) == dp.end()) {
        dp[ps] = dp[get] + 1;
        que.push(ps);
      }
    }
  }
}
char str[10];
int main() {
  bfs();
  bool vvv = false;
  while (1) {
    for (int i = 0; i < 8; i += 1) {
      if (scanf("%s", &str[i]) == EOF) {
        vvv = true;
        break;
      }
    }
    if (vvv)
      break;
    printf("%d\\n", dp[str]);
    //		printf("%d\\n",dp["10234567"]);
  }
  return 0;
}

Error: TLE

Fixed submission:

/*
 * Filename:    aoj0121.cpp
 * Desciption:  Desciption
 * Created:     2016-03-07
 * Author:      JIngwei Xu [mail:xu_jingwei@outlook.com]
 *
 */
#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <stdio.h>
#define INT_MAX 1 << 30
using namespace std;
// typedef long long ll;
const int INF = 0x7F;
int dx[5] = {1, 0, 0, -1, 0}, dy[5] = {0, 1, -1, 0, 0};
map<string, int> dp;
void bfs() {
  queue<string> que;
  que.push("01234567");
  dp["01234567"] = 0;
  while (!que.empty()) {
    string get = que.front();
    que.pop();
    int cr = 0;
    for (int i = 0; i < 8; i += 1) {
      if (get[i] == '0') {
        cr = i;
        break;
      }
    }
    int cx = cr / 4, cy = cr % 4;
    for (int i = 0; i < 4; i += 1) {
      int px = cx + dx[i], py = cy + dy[i];
      if (px < 0 || py < 0 || px > 1 || py > 3)
        continue;
      int pr = 4 * px + py;
      string ps = get;
      swap(ps[cr], ps[pr]);
      if (dp.find(ps) == dp.end()) {
        dp[ps] = dp[get] + 1;
        que.push(ps);
      }
    }
  }
}
char str[10];
int main() {
  bfs();
  while (scanf(" ") != EOF) {
    for (int i = 0; i < 8; i += 1) {
      scanf("%s", &str[i]);
    }
    printf("%d\\n", dp[str]);
    //		printf("%d\\n",dp["10234567"]);
  }
  return 0;
}
Problem p00121 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <map>
#include <queue>
#include <string>
using namespace std;
const int dx[] = {1, -1, 4, -4};
#define fr first
#define sc second
typedef pair<string, int> sPi;
string s;
int now;
map<string, int> used;
int bfs() {
  queue<sPi> que;
  que.push(sPi("01234567", 0));
  used["01234567"] = 1;
  while (!que.empty()) {
    sPi p = que.front();
    que.pop();
    for (int i = 0; i < 4; i++) {
      string next = p.fr;
      if (0 <= p.sc + dx[i] && p.sc + dx[i] < 8 && !(p.sc == 3 && !i) &&
          !(p.sc == 4 && i == 1)) {
        swap(next[p.sc], next[p.sc + dx[i]]);
        if (!used[next]) {
          used[next] = used[p.fr] + 1;
          que.push(sPi(next, p.sc + dx[i]));
        }
      }
    }
  }
  return -1;
}
int main() {
  s.resize(8);
  bfs();
  while (cin >> s[0] >> s[1] >> s[2] >> s[3] >> s[4] >> s[5] >> s[6] >> s[7]) {
    cout << used[s] - 1 << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <map>
#include <queue>
#include <string>
using namespace std;
const int dx[] = {1, -1, 4, -4};
#define fr first
#define sc second
typedef pair<string, int> sPi;
string s;
int now;
int bfs() {
  queue<sPi> que;
  map<string, int> used;
  que.push(sPi(s, now));
  used[s] = 1;
  while (!que.empty()) {
    sPi p = que.front();
    que.pop();
    if (p.fr == "01234567")
      return used[p.fr];
    for (int i = 0; i < 4; i++) {
      string next = p.fr;
      if (0 <= p.sc + dx[i] && p.sc + dx[i] < 8 && !(p.sc == 3 && !i) &&
          !(p.sc == 4 && i == 1)) {
        swap(next[p.sc], next[p.sc + dx[i]]);
        if (!used[next]) {
          used[next] = used[p.fr] + 1;
          que.push(sPi(next, p.sc + dx[i]));
        }
      }
    }
  }
  return -1;
}
int main() {
  s.resize(8);
  while (cin >> s[0] >> s[1] >> s[2] >> s[3] >> s[4] >> s[5] >> s[6] >> s[7]) {
    now = s.find('0');
    cout << bfs() - 1 << endl;
  }
}