alexjercan/bugnet huggingface dataset:
Problem p00120 (C++) — buggy submission, verdict: Runtime Error

#include <cmath>
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;

int main() {
  double dp[12][1 << 12] = {};
  string ln;
  while (getline(cin, ln)) {
    for (int i = 0; i < 12; i++)
      for (int j = 0; j < 1 << 12; j++)
        dp[i][j] = 1e9;
    stringstream ss(ln);
    vector<int> item;

    int w, t;
    ss >> w;
    while (ss >> t)
      item.push_back(t);
    for (int i = 0; i < item.size(); i++) {
      dp[i][1 << i] = item[i];
    }
    for (int i = 0; i < (1 << item.size()); i++) {
      for (int j = 0; j < item.size(); j++) {
        for (int k = 0; k < item.size(); k++) {
          dp[k][i | (1 << k)] =
              min(dp[k][i | (1 << k)],
                  dp[j][i] + sqrt((item[j] + item[k]) * (item[j] + item[k]) -
                                  (item[j] - item[k]) * (item[j] - item[k])));
        }
      }
    }

    double ans = 1e9;
    for (int i = 0; i < item.size(); i++) {
      ans = min(ans, dp[i][(1 << item.size()) - 1] + item[i]);
    }
    if (ans <= w + 1e-9) {
      cout << "OK" << endl;
    } else {
      cout << "NA" << endl;
    }
  }
}

Error: 0

Fixed submission:

#include <cmath>
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;

int main() {
  double dp[10][1 << 10] = {};
  string ln;
  while (getline(cin, ln)) {
    for (int i = 0; i < 10; i++)
      for (int j = 0; j < 1 << 10; j++)
        dp[i][j] = 1e9;
    stringstream ss(ln);
    vector<int> item;

    int w, t;
    ss >> w;
    while (ss >> t)
      item.push_back(t);
    for (int i = 0; i < item.size(); i++) {
      dp[i][1 << i] = item[i];
    }
    for (int i = 0; i < (1 << item.size()); i++) {
      for (int j = 0; j < item.size(); j++) {
        for (int k = 0; k < item.size(); k++) {
          dp[k][i | (1 << k)] =
              min(dp[k][i | (1 << k)],
                  dp[j][i] + sqrt((item[j] + item[k]) * (item[j] + item[k]) -
                                  (item[j] - item[k]) * (item[j] - item[k])));
        }
      }
    }

    double ans = 1e9;
    for (int i = 0; i < item.size(); i++) {
      ans = min(ans, dp[i][(1 << item.size()) - 1] + item[i]);
    }
    if (ans <= w + 1e-9) {
      cout << "OK" << endl;
    } else {
      cout << "NA" << endl;
    }
  }
}
Problem p00120 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <climits>
#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 <vector>
using namespace std;

const int LOOP_TIMES = 1e5;
const double EPS = 1e-6;
int L, N;

double Evaluate(int *r) {
  double val = r[0] + r[N - 1];
  for (int i = 0; i < N - 1; i++) {
    int a = abs(r[i] - r[i + 1]), c = r[i] + r[i + 1];
    val += sqrt(c * c - a * a);
  }
  return val;
}

bool IsChange(int cnt) { return cnt < rand() % LOOP_TIMES + 1; }

double Solve(int *r) {
  double score = Evaluate(r), mi = score;
  for (int cnt = 0; cnt < LOOP_TIMES; cnt++) {
    int *a = r + rand() % N, *b = r + rand() % N;
    swap(*a, *b);

    double next_score = Evaluate(r);
    if (score >= next_score && !IsChange(cnt))
      swap(*a, *b);
    else
      score = next_score;

    mi = min(mi, score);
  }
  return mi;
}

int main() {
  srand(time(NULL));

  while (cin >> L) {
    N = 0;
    int r[12];
    string S;
    getline(cin, S);
    stringstream SS(S);
    while (!SS.eof()) {
      SS >> r[N++];
    }

    double ans;
    if (N == 1)
      ans = r[0] * 2;
    else
      ans = Solve(r);

    if (L - ans + EPS > 0)
      puts("OK");
    else
      puts("NA");
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#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 <vector>
using namespace std;

const int LOOP_TIMES = 1e6;
const double EPS = 1e-6;
int L, N;

double Evaluate(int *r) {
  double val = r[0] + r[N - 1];
  for (int i = 0; i < N - 1; i++) {
    int a = abs(r[i] - r[i + 1]), c = r[i] + r[i + 1];
    val += sqrt(c * c - a * a);
  }
  return val;
}

bool IsChange(int cnt) { return cnt < rand() % LOOP_TIMES + 1; }

double Solve(int *r) {
  double score = Evaluate(r), mi = score;
  for (int cnt = 0; cnt < LOOP_TIMES; cnt++) {
    int *a = r + rand() % N, *b = r + rand() % N;
    swap(*a, *b);

    double next_score = Evaluate(r);
    if (score >= next_score && !IsChange(cnt))
      swap(*a, *b);
    else
      score = next_score;

    mi = min(mi, score);
  }
  return mi;
}

int main() {
  while (cin >> L) {
    N = 0;
    int r[12];
    string S;
    getline(cin, S);
    stringstream SS(S);
    while (!SS.eof()) {
      SS >> r[N++];
    }

    double ans;
    if (N == 1)
      ans = r[0] * 2;
    else
      ans = Solve(r);

    if (L - ans + EPS > 0)
      puts("OK");
    else
      puts("NA");
  }
  return 0;
}
Problem p00120 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
static const double EPS = 1e-5;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)

int main(void) {
  string in;
  while (getline(cin, in)) {
    if (in == "")
      break;
    stringstream ss;
    ss << in;
    int l, tmp;
    vector<int> r;
    ss >> l;
    while (ss >> tmp)
      r.push_back(tmp);
    bool able = false;
    REP(i, r.size()) {
      vector<int> tmp = r;
      int prev = r[i];
      double alen = r[i];
      tmp.erase(tmp.begin() + i);
      while (!tmp.empty()) {
        PII maxp = PII(-1, 0);
        REP(i, tmp.size()) {
          if (abs(prev - tmp[i]) > maxp.first) {
            maxp = PII(abs(prev - tmp[i]), i);
          }
        }
        int next = tmp[maxp.second];
        alen += 2 * sqrt(next * prev);
        prev = next;
        tmp.erase(tmp.begin() + maxp.second);
      }
      alen += prev;
      if (alen <= (double)l) {
        able = true;
        break;
      }
    }
    if (able)
      cout << "OK" << endl;
    else
      cout << "NA" << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
static const double EPS = 1e-5;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)

int main(void) {
  string in;
  while (getline(cin, in)) {
    stringstream ss;
    ss << in;
    int l, tmp;
    vector<int> r;
    ss >> l;
    while (ss >> tmp)
      r.push_back(tmp);
    vector<int> perm;
    REP(i, r.size()) { perm.push_back(i); }
    bool able = false;
    do {
      double len = r[perm[0]] + r[perm[r.size() - 1]];
      REP(i, r.size() - 1) { len += sqrt(4 * r[perm[i]] * r[perm[i + 1]]); }
      if (len <= (double)l) {
        able = true;
        break;
      }
    } while (next_permutation(perm.begin(), perm.end()));
    if (able)
      cout << "OK" << endl;
    else
      cout << "NA" << endl;
  }
  return 0;
}
Problem p00121 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bitset>
#include <queue>
#include <stdio.h>
using namespace std;

const int N = 8;
const int SHIFT_BITS = 3;

unsigned Swap3Bits(unsigned x, int b1, int b2) {
  unsigned a, b, s;
  b1 *= SHIFT_BITS;
  b2 *= SHIFT_BITS;
  a = (x & (07 << b1));
  b = (x & (07 << b2));
  s = (a >> b1 << b2) | (b >> b2 << b1);
  return (x & ~a & ~b) | s;
}

int Ans(int initField) {
  int ans, i;
  unsigned field;
  static bitset<16777216> bitHis;
  queue<unsigned> q;
  unsigned int NEXT_COUNT = 0xffffffff;

  ans = 0;
  bitHis.reset();
  q.push(initField);
  q.push(NEXT_COUNT);
  for (;;) {
    field = q.front();
    q.pop();
    if (field == NEXT_COUNT) {
      ++ans;
      q.push(NEXT_COUNT);
      continue;
    }
    if (bitHis[field])
      continue;
    if (field == 001234567)
      break;
    bitHis[field] = true;

    int zi = 0;
    for (i = 0; i < N; ++i) {
      if ((field & (07 << i * SHIFT_BITS)) == 00) {
        zi = i;
        break;
      }
    }

    q.push(Swap3Bits(field, zi, (zi < 4 ? zi + 4 : zi - 4)));
    if ((zi & 0x3) != 0x3)
      q.push(Swap3Bits(field, zi, zi + 1));
    if ((zi & 0x3) != 0x0)
      q.push(Swap3Bits(field, zi, zi - 1));
  }
  return ans;
}

int main(void) {
  int a[N], i;

  for (;;) {
    unsigned field = 0;
    for (i = 0; i < N; ++i) {
      if (scanf("%d", &a[i]) != 1)
        return 0;
      field |= (a[i] << ((N - i - 1) * SHIFT_BITS));
    }
    printf("%d\\n", Ans(field));
  }
  return 1;
}

Error: TLE

Fixed submission:

#include <queue>
#include <set>
#include <stdio.h>
using namespace std;

const int N = 8;
const int SHIFT_BITS = 2;

unsigned Swap4Bits(unsigned x, int b1, int b2) {
  unsigned a, b, s;
  b1 <<= SHIFT_BITS;
  b2 <<= SHIFT_BITS;
  a = (x & (0xf << b1));
  b = (x & (0xf << b2));
  s = (a >> b1 << b2) | (b >> b2 << b1);
  return (x & ~a & ~b) | s;
}

int Ans(int initField) {
  int ans, i, j;
  int zi;
  unsigned field;
  set<unsigned> s;
  queue<unsigned> q;
  unsigned int NEXT_COUNT = 0xffffffff;

  ans = 0;
  q.push(initField);
  q.push(NEXT_COUNT);
  for (;;) {
    field = q.front();
    q.pop();
    if (field == NEXT_COUNT) {
      ++ans;
      q.push(NEXT_COUNT);
      continue;
    }
    if (s.find(field) != s.end())
      continue;
    if (field == 0x01234567)
      break;
    s.insert(field);

    for (i = 0; i < N; ++i) {
      if ((field & (0xf << (i << SHIFT_BITS))) == 0x00) {
        zi = i;
        break;
      }
    }

    q.push(Swap4Bits(field, zi, (zi < 4 ? zi + 4 : zi - 4)));
    if ((zi & 0x3) != 0x3)
      q.push(Swap4Bits(field, zi, zi + 1));
    if ((zi & 0x3) != 0x0)
      q.push(Swap4Bits(field, zi, zi - 1));
  }
  return ans;
}

int main(void) {
  int a[N], i, j;

  for (;;) {
    unsigned field = 0;
    for (i = 0; i < N; ++i) {
      if (scanf("%d", &a[i]) != 1)
        return 0;
      field |= (a[i] << ((N - i - 1) << SHIFT_BITS));
    }
    printf("%d\\n", Ans(field));
  }
  return 1;
}
Problem p00121 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

using namespace std;

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

void bfs(void) {
  queue<string> que;

  que.push("01234567");

  while (!que.empty()) {

    string v = que.front();

    que.pop();

    int pos = 0;
    for (int i = 0; i < 8; i++)
      if (v[i] == '0')
        pos = i;

    for (int i = 0; i < 4; i++) {
      if (0 <= pos + dx[i] && pos + dx[i] < 8 && !(pos == 3 && i == 0) &&
          !(pos == 4 && i == 1)) {
        string u = v;
        swap(u[pos], u[pos + dx[i]]);

        if (res[u] == 0) {
          que.push(u);
          res[u] = res[v] + 1;
        }
      }
    }
  }
}

int main(void) {
  int in;

  res["01234567"] = 1;

  bfs();

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

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

  return 0;
}

Error: MLE

Fixed submission:

#include <cmath>
#include <iostream>
#include <queue>
using namespace std;

int val[8];
int dp[16777217];

class State {
public:
  int t, cost;

  State(int tt, int tcost) {
    t = tt;
    cost = tcost;
  }
};

int change(int t, int from, int to) {
  int digit = (t / val[to]) % val[1];
  return t - digit * val[to] + digit * val[from];
}

int zeroIdx(int t) {
  for (int i = 0; i < 8; i++) {
    if ((t / val[i]) % val[1] == 0)
      return i;
  }
  return -1;
}

int DP(void) {
  int start = 0;
  for (int i = 0; i < 8; i++) {
    val[i] = (int)pow(8, i);
    start += i * val[i];
  }

  queue<State> open;
  State st(start, 0);
  open.push(st);
  dp[start] = -1;

  while (!open.empty()) {
    st = open.front();
    open.pop();
    int idx = zeroIdx(st.t);

    if (idx >= 4) {
      int aft = change(st.t, idx, idx - 4);
      if (dp[aft] == 0) {
        dp[aft] = st.cost + 1;
        open.push(State(aft, st.cost + 1));
      }
    } else if (idx < 4) {
      int aft = change(st.t, idx, idx + 4);
      if (dp[aft] == 0) {
        dp[aft] = st.cost + 1;
        open.push(State(aft, st.cost + 1));
      }
    }
    if (idx % 4 > 0) {
      int aft = change(st.t, idx, idx - 1);
      if (dp[aft] == 0) {
        dp[aft] = st.cost + 1;
        open.push(State(aft, st.cost + 1));
      }
    }
    if (idx % 4 < 3) {
      int aft = change(st.t, idx, idx + 1);
      if (dp[aft] == 0) {
        dp[aft] = st.cost + 1;
        open.push(State(aft, st.cost + 1));
      }
    }
  }

  dp[start] = 0;
}

int main(void) {
  DP();

  while (1) {
    int sum = 0;
    for (int i = 0; i < 8; i++) {
      int x;
      if (!(cin >> x))
        return 0;
      sum += x * val[i];
    }
    cout << dp[sum] << endl;
  }

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

#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
#define INF 10000000
using namespace std;
typedef pair<int, int> P;

void swap(char *a, char *b) {
  char temp = *a;
  *a = *b;
  *b = temp;
}

int main() {
  int dx[] = {+1, -1, +4, -4};

  string start = "01234567";

  queue<string> que;
  map<string, int> d;
  que.push(start);
  d[start] = 0;

  while (que.size()) {
    string q = que.front();
    que.pop();
    int x = q.find("0");
    for (int i = 0; i < 4; i++) {
      int next = x + dx[i];
      if (next >= 0 && next <= 7 && !(x == 3 && next == 4) &&
          !(x == 4 && next == 3)) {
        string nextq = q;
        swap(&nextq[x], &nextq[next]);
        if (d.find(nextq) == d.end()) {
          que.push(nextq);
          d[nextq] = d[q] + 1;
        }
      }
    }
  }

  while (1) {
    string input = "";
    char temp;
    for (int i = 0; i < 8; i++) {
      if (cin >> temp) {
        input.push_back(temp);
      } else {
        return 0;
      }
    }
    cout << d[input] << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
#define INF 10000000
using namespace std;
typedef pair<int, int> P;

void swap(char *a, char *b) {
  char temp = *a;
  *a = *b;
  *b = temp;
}

int main() {
  int dx[] = {+1, -1, +4, -4};
  while (1) {
    string input = "";
    char temp;
    for (int i = 0; i < 8; i++) {
      if (cin >> temp) {
        input.push_back(temp);
      } else {
        return 0;
      }
    }

    queue<string> que;
    map<string, int> d;
    que.push(input);
    d[input] = 0;

    while (que.size()) {
      string q = que.front();
      que.pop();
      if (q == "01234567") {
        break;
      }
      int x = q.find("0");
      for (int i = 0; i < 4; i++) {
        int next = x + dx[i];
        if (next >= 0 && next <= 7 && !(x == 3 && next == 4) &&
            !(x == 4 && next == 3)) {
          string nextq = q;
          swap(&nextq[x], &nextq[next]);
          if (d.find(nextq) == d.end()) {
            que.push(nextq);
            d[nextq] = d[q] + 1;
          }
        }
      }
    }
    cout << d["01234567"] << endl;
  }
}
Problem p00121 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <string>
using namespace std;
int main() {
  map<string, int> ans;
  queue<string> log;
  string f = "01234567";
  int dx[4] = {1, -1, 4, -4};
  log.push(f);
  ans[f] = 0;
  while (!log.empty()) {
    string s = log.front();
    int x = s.find("0");
    for (int i = 0; i < 4; i++) {
      int nx = x + dx[i];
      if (nx >= 0 && nx <= 7 && !(x == 3 && nx == 4) && !(x == 4 && nx == 3)) {
        string t = s;
        swap(t[x], t[nx]);
        if (ans.find(t) == ans.end()) {
          ans[t] = ans[s] + 1;
          log.push(t);
        }
      }
    }
    log.pop();
  }
  string n;
  while (getline(cin, n)) {
    stringstream ss(n);
    string m;
    while (ss >> n)
      m += n;
    cout << ans[m] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int p[4][2];
bool fn();
bool ans(int n, int l, int x, int y);
int main() {
  int n, x, y;
  while (cin >> n) {
    for (int i = 0; i < 8; i++) {
      if (i != 0)
        cin >> n;
      p[i % 4][i / 4] = n;
      if (n == 0) {
        x = i % 4;
        y = i / 4;
      }
    }
    for (int i = 0;; i++) {
      if (ans(i, 0, x, y)) {
        cout << i << endl;
        break;
      }
    }
  }
  return 0;
}
bool fn() {
  for (int i = 0; i < 8; i++)
    if (p[i % 4][i / 4] != i)
      return false;
  return true;
}
bool ans(int n, int l, int x, int y) {
  int f = false;
  if (n == 0)
    return fn();
  if (x > 0 && l != 2) {
    p[x][y] = p[x - 1][y];
    p[x - 1][y] = 0;
    f |= ans(n - 1, 1, x - 1, y);
    if (f)
      return f;
    p[x - 1][y] = p[x][y];
    p[x][y] = 0;
  }
  if (x < 3 && l != 1) {
    p[x][y] = p[x + 1][y];
    p[x + 1][y] = 0;
    f |= ans(n - 1, 2, x + 1, y);
    if (f)
      return f;
    p[x + 1][y] = p[x][y];
    p[x][y] = 0;
  }
  if (y == 1 && l != 4) {
    p[x][y] = p[x][y - 1];
    p[x][y - 1] = 0;
    f |= ans(n - 1, 3, x, y - 1);
    if (f)
      return f;
    p[x][y - 1] = p[x][y];
    p[x][y] = 0;
  }
  if (y == 0 && l != 3) {
    p[x][y] = p[x][y + 1];
    p[x][y + 1] = 0;
    f |= ans(n - 1, 4, x, y + 1);
    if (f)
      return f;
    p[x][y + 1] = p[x][y];
    p[x][y] = 0;
  }
  return f;
}
Problem p00121 (C++) — buggy submission, verdict: Time Limit Exceeded

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

class Puzzle {
public:
  string s;
  int count;
};

int main() {
  int d[4] = {1, -1, 4, -4};
  map<string, int> memo;
  queue<Puzzle> que;
  Puzzle target;
  target.s = "01234567";
  target.count = 0;
  que.push(target);
  memo[target.s] = target.count;

  while (!que.empty()) {
    Puzzle p = que.front();
    que.pop();
    int zeroPos = p.s.find('0');
    for (int i = 0; i < 4; i++) {
      if (zeroPos % 4 == 0 && i == 1)
        continue;
      if (zeroPos % 4 == 3 && i == 0)
        continue;
      if (!(0 <= zeroPos + d[i] && zeroPos + d[i] < 8))
        continue;
      Puzzle next = p;
      next.count++;
      swap(next.s[zeroPos], next.s[zeroPos + d[i]]);
      if (memo.count(next.s))
        continue;
      memo[next.s] = next.count;
      que.push(next);
    }
  }

  while (true) {
    string str;
    char c;
    for (int i = 0; i < 8; i++) {
      if (!(cin >> c))
        return 0;
      str.push_back(c);
    }
    cout << memo[str] << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

string exchange(int a, int b, string s) {
  char tmp;
  tmp = s[a];
  s[a] = s[b];
  s[b] = tmp;
  return s;
}

int main() {
  string first;
  queue<string> numbers;
  map<string, int> step;

  while (!cin.eof()) {
    first = "01234567";

    for (int i = 0; i < 8; ++i) {
      int tmp;
      cin >> tmp;
      first[i] = tmp + '0';
    }

    while (!numbers.empty())
      numbers.pop();
    step.clear();

    numbers.push(first);
    step[first] = 0;

    while (!cin.eof()) {
      string tmp;

      if (numbers.front() == "01234567") {
        cout << step[numbers.front()] << endl;
        break;
      }

      for (int i = 0; i < 8; ++i) {
        if (numbers.front()[i] == '0') {
          tmp = numbers.front();
          if (i > 3 && step.count(exchange(i, i - 4, tmp)) == 0) {
            numbers.push(exchange(i, i - 4, tmp));
            step[exchange(i, i - 4, tmp)] = 1 + step[tmp];
          }
          if (i < 4 && step.count(exchange(i, i + 4, tmp)) == 0) {
            numbers.push(exchange(i, i + 4, tmp));
            step[exchange(i, i + 4, tmp)] = 1 + step[tmp];
          }
          if (i != 0 && i != 4 && step.count(exchange(i, i - 1, tmp)) == 0) {
            numbers.push(exchange(i, i - 1, tmp));
            step[exchange(i, i - 1, tmp)] = 1 + step[tmp];
          }
          if (i != 3 && i != 7 && step.count(exchange(i, i + 1, tmp)) == 0) {
            numbers.push(exchange(i, i + 1, tmp));
            step[exchange(i, i + 1, tmp)] = 1 + step[tmp];
          }
          numbers.pop();
        }
      }
    }
  }

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

//??????????¨????????????????????¬¬??????????¢?
// BFS DP ?§£?????´??????
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <stdio.h>

using namespace std;

int dm[4] = {1, -1, 4, -4};
map<string, int> DP;

void BFS() {
  queue<string> que;
  que.push("01234567");
  DP["01234567"] = 0;
  while (que.size()) {
    string now = que.front();
    que.pop();
    int p = 0;
    for (int i = 0; i < 8; i++) {
      if (now[i] == '0') {
        p = i;
        break;
      }
    }

    for (int i = 0; i < 4; i++) {
      int d = p + dm[i];
      if (d >= 0 && d <= 7 && !(p == 3 && i == 0) && !(p == 4 && i == 1)) {
        string next = now;
        swap(next[p], next[d]);
        if (!DP[next]) {
          DP[next] = DP[now] + 1;
          que.push(next);
        }
      }
    }
  }
}

int main() {
  BFS();
  int d;
  while (~scanf("%d", &d)) {
    string temp;
    temp += d + '0';
    for (int i = 0; i < 7; i++) {
      scanf("%d", &d);
      temp += d + '0';
    }
    cout << DP[temp] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

//??????????¨????????????????????¬¬??????????¢?
// BFS DP ?§£?????´??????
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <stdio.h>

using namespace std;

int dm[4] = {1, -1, 4, -4};
map<string, int> DP;

void BFS() {
  queue<string> que;
  que.push("01234567");
  DP["01234567"] = 0;
  while (que.size()) {
    string now = que.front();
    que.pop();
    int p = 0;
    for (int i = 0; i < 8; i++) {
      if (now[i] == '0') {
        p = i;
        break;
      }
    }

    for (int i = 0; i < 4; i++) {
      int d = p + dm[i];
      if (d >= 0 && d <= 7 && !(p == 3 && i == 0) && !(p == 4 && i == 1)) {
        string next = now;
        swap(next[p], next[d]);
        if (!DP[next]) {
          DP[next] = DP[now] + 1;
          que.push(next);
        }
      }
    }
  }
}

int main() {
  BFS();
  int d;
  while (true) {
    string temp;
    for (int i = 0; i < 8; i++) {
      scanf("%d", &d);
      temp += d + '0';
    }
    cout << DP[temp] << endl;
  }
  return 0;
}