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

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <string>
using namespace std;
typedef pair<string, int> P;
int dx[4] = {-1, 1, -4, 4};
map<string, int> strMap;
bool check(int num) {
  if (num >= 0 && num <= 7) {
    return true;
  }
  return false;
}

void BFS(string s) {
  queue<P> que;
  strMap.insert(P(s, 0));
  que.push(P(s, 0));
  while (que.size()) {
    P temp = que.front();
    que.pop();
    //	cout << temp.first  << endl;
    string strTemp = temp.first;
    int ZeroPos = 0;
    for (int i = 0; i < 8; ++i) {
      if (strTemp[i] == '0') {
        ZeroPos = i;
        break;
      }
    }
    for (int i = 0; i < 4; ++i) {
      if (check(ZeroPos + dx[i])) {
        if (ZeroPos == 3 &&
            dx[i] == 1) { // 不能从3移&#21160;到4或 4移&#21160;到3
          continue;
        }
        if (ZeroPos == 4 && dx[i] == -1) {
          continue;
        }
        swap(strTemp[ZeroPos], strTemp[ZeroPos + dx[i]]);
        string strTemp2 = strTemp;
        if (strMap.find(strTemp2) == strMap.end()) {
          strMap.insert(P(strTemp2, temp.second + 1));
          que.push(P(strTemp2, temp.second + 1));
        }
        swap(strTemp[ZeroPos], strTemp[ZeroPos + dx[i]]);
      }
    }
  }
}

int main() {
  string s;
  string des = "01234567";

  while (getline(cin, s)) {
    s.erase(remove(s.begin(), s.end(), ' '), s.end());
    BFS(des);
    int result = strMap.find(s)->second;
    cout << result << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <string>
using namespace std;
typedef pair<string, int> P;
string des;
int dx[4] = {-1, 1, -4, 4};

bool check(int num) {
  if (num >= 0 && num <= 7) {
    return true;
  }
  return false;
}

int BFS(string s) {
  queue<P> que;
  set<string> strSet;
  strSet.insert(s);
  que.push(P(s, 0));
  while (que.size()) {
    P temp = que.front();
    que.pop();
    //	cout << temp.first  << endl;
    if (temp.first == des) {
      return temp.second;
    }
    string strTemp = temp.first;
    int ZeroPos = 0;
    for (int i = 0; i < 8; ++i) {
      if (strTemp[i] == '0') {
        ZeroPos = i;
        break;
      }
    }
    for (int i = 0; i < 4; ++i) {
      if (check(ZeroPos + dx[i])) {
        if (ZeroPos == 3 &&
            dx[i] == 1) { // 不能从3移&#21160;到4或 4移&#21160;到3
          continue;
        }
        if (ZeroPos == 4 && dx[i] == -1) {
          continue;
        }
        swap(strTemp[ZeroPos], strTemp[ZeroPos + dx[i]]);
        string strTemp2 = strTemp;
        if (strSet.find(strTemp2) == strSet.end()) {
          strSet.insert(strTemp2);
          que.push(P(strTemp2, temp.second + 1));
        }
        swap(strTemp[ZeroPos], strTemp[ZeroPos + dx[i]]);
      }
    }
  }
}

int main() {
  string s;
  des = "01234567";
  while (getline(cin, s)) {
    s.erase(remove(s.begin(), s.end(), ' '), s.end());
    int result = BFS(s);
    cout << result << endl;
  }
  return 0;
}
Problem p00121 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

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

const vector<int> goal = {0, 1, 2, 3, 4, 5, 6, 7};

map<vector<int>, int> d;

void bfs(vector<int> puzzle) {
  queue<pair<vector<int>, int>> que;
  que.push(make_pair(puzzle, 1));

  d[puzzle] = 1;

  while (!que.empty()) {
    vector<int> v = que.front().first;

    int dist = que.front().second;
    que.pop();

    int zero = -1;

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

    for (int i = 0; i < 4; i++) {
      if (0 <= zero + dir[i] && zero + dir[i] < 8 && !(i == 1 && zero == 4) &&
          !(i == 2 && zero == 3)) {
        vector<int> v2 = v;

        swap(v2[zero], v2[zero + dir[i]]);

        if (d[v2] == 0) {
          d[v2] = dist + 1;

          que.push(make_pair(v2, dist + 1));
        }
      }
    }
  }
}

int main() {
  vector<int> v(8);

  bfs(goal);

  while (cin >> v[0] >> v[1] >> v[2] >> v[3] >> v[4] >> v[5] >> v[6] >> v[7]) {
    cout << d[v] - 1 << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

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

const vector<int> goal = {0, 1, 2, 3, 4, 5, 6, 7};

int bfs(vector<int> puzzle) {
  map<vector<int>, int> d;
  d[puzzle] = 1;

  queue<pair<vector<int>, int>> que;
  que.push(make_pair(puzzle, 1));

  while (!que.empty()) {
    vector<int> v = que.front().first;

    int dist = que.front().second;
    que.pop();

    int zero = -1;

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

    for (int i = 0; i < 4; i++) {
      if (0 <= zero + dir[i] && zero + dir[i] < 8) {
        vector<int> v2 = v;

        swap(v2[zero], v2[zero + dir[i]]);

        if (v2 == goal) {
          return dist;
        }

        if (d[v2] == 0) {
          d[v2] = dist + 1;

          que.push(make_pair(v2, dist + 1));
        }
      }
    }
  }

  return -1;
}

int main() {
  vector<int> v(8);

  while (cin >> v[0] >> v[1] >> v[2] >> v[3] >> v[4] >> v[5] >> v[6] >> v[7]) {
    cout << bfs(v) << endl;
  }

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

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

using namespace std;

typedef pair<string, int> P;

string String(vector<int> v) {
  string ret = "";
  for (int i = 0; i < v.size(); ++i)
    ret += v[i] + '0';

  return ret;
}

int main() {
  map<string, int> steps;
  queue<P> Q;
  Q.push(P("01234567", 0));

  while (!Q.empty()) {
    P p = Q.front();
    Q.pop();
    string str = p.first;
    int step = p.second;

    if (steps[str])
      continue;
    steps[str] = step + 1;

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

    string next = str;

    if (pos > 3)
      swap(next[pos], next[pos - 4]);
    else
      swap(next[pos], next[pos + 4]);

    Q.push(P(next, step + 1));
    next = str;

    if (pos % 4 != 0) {
      swap(next[pos], next[pos - 1]);
      Q.push(P(next, step + 1));
      next = str;
    }
    if (pos % 4 != 3) {
      swap(next[pos], next[pos + 1]);
      Q.push(P(next, step + 1));
      next = str;
    }
  }

  int a;
  while (scanf("%d", &a) != EOF) {
    vector<int> now(8, a);
    for (int i = 1; i < 8; ++i)
      cin >> now[i];
    cout << (steps[String(now)] - 1) << endl;
  }
}

Error: TLE

Fixed submission:

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

using namespace std;

typedef pair<string, int> P;

vector<string> make(string str) {
  vector<string> ret;

  int pos;
  for (int i = 0; i < str.size(); ++i)
    if (str[i] == '0')
      pos = i;

  vector<int> v;
  if (pos % 4 != 3)
    v.push_back(pos + 1);
  if (pos % 4 != 0)
    v.push_back(pos - 1);
  if (pos < 4)
    v.push_back(pos + 4);
  else
    v.push_back(pos - 4);

  for (int i = 0; i < v.size(); ++i) {
    string s = str;
    swap(s[pos], s[v[i]]);
    ret.push_back(s);
  }

  return ret;
}

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

    queue<P> Q;
    Q.push(P(str, 0));
    map<string, bool> m;
    while (!Q.empty()) {
      P p = Q.front();
      Q.pop();
      string str = p.first;
      int step = p.second;

      if (m[str])
        continue;
      m[str] = true;

      if (str == "01234567") {
        cout << step << endl;
        break;
      }

      vector<string> v = make(str);
      for (int i = 0; i < v.size(); ++i)
        Q.push(P(v[i], step + 1));
    }
  }
}
Problem p00121 (C++) — buggy submission, verdict: Time Limit Exceeded

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

map<string, int> dp;
int direction[4] = {1, -1, 4, -4};
void bfs() {
  queue<string> que;
  que.push("01234567");
  dp["01234567"] = 0;
  while (!que.empty()) {
    string now = que.front();
    que.pop();
    // p是'0'的位置
    int p = 0;
    for (int j = 0; j < 8; ++j) {
      if (now[j] == '0') {
        p = j;
        break;
      }
    }

    for (int i = 0; i < 4; ++i) {
      int n = p + direction[i];
      if (0 <= n && n < 8 && !(p == 3 && i == 0) && !(p == 4 && i == 1)) {
        string next = now;
        swap(next[p], next[n]);
        if (dp.find(next) == dp.end()) {
          dp[next] = dp[now] + 1;
          que.push(next);
        }
      }
    }
  }
}

int main() {
  bfs();
  string a = "        ";
  while (cin >> a[0] >> a[1] >> a[2] >> a[3] >> a[4] >> a[5] >> a[6] >> a[7]) {
    cout << dp[a] << endl;
  }
  // system("pause");
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
int a[8];
int step;

bool endsearch;
map<vector<int>, bool> visited;

bool istarget(vector<int> vec) {
  for (int i = 0; i < 8; i++) {
    if (vec[i] != i) {
      return false;
    }
  }
  return 1;
}

queue<vector<int>> bfs(queue<vector<int>> que) {
  int quesize = que.size();
  step++;
  for (int i = 0; i < quesize; i++) {
    if (istarget(que.front())) {
      endsearch = 1;
      step--;
      return que;
    } else {
      if (!visited[que.front()]) {
        visited[que.front()] = 1;
        for (int j = 0; j < 7; j++) {
          if (j == 3)
            continue;
          vector<int> tmp(que.front());
          tmp[j] = que.front()[j + 1];
          tmp[j + 1] = que.front()[j];
          que.push(tmp);
        }
        for (int j = 0; j < 4; j++) {
          vector<int> tmp(que.front());
          tmp[j] = que.front()[j + 4];
          tmp[j + 4] = que.front()[j];
          que.push(tmp);
        }
        que.pop();
      } else {
        que.pop();
      }
    }
  }
  return que;
}

int main() {
  while (cin >> a[0] >> a[1] >> a[2] >> a[3] >> a[4] >> a[5] >> a[6] >> a[7]) {
    vector<int> vec(a, a + 8);
    queue<vector<int>> que;
    visited.clear();
    step = 0;
    endsearch = 0;
    que.push(vec);
    while (que.size() != 0) {
      que = bfs(que);
      if (endsearch) {
        cout << step << endl;
        break;
      }
    }
  }
  return 0;
}
Problem p00121 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define for_(i, a, b) for (int i = (a); i < (b); ++i)
#define size_of(a) (int)(a).size()
#define allof(a) (a).begin(), (a).end()

int to[8][3] = {
    {1, 4, -1}, {0, 2, 5}, {1, 3, 6}, {2, 7, -1},
    {0, 5, -1}, {1, 4, 6}, {2, 5, 7}, {3, 6, -1},
};

struct State {
  int step, p[8];

  int zero() {
    for_(i, 0, 8) if (p[i] == 0) return i;
    return -1;
  }

  int getHash() {
    int hash = 0;
    for_(i, 0, 8) hash = 10 * hash + p[i];
    return hash;
  }
};

int main() {
  State ini;
  ini.step = 0;
  for_(i, 0, 8) ini.p[i] = i;

  map<int, int> hash;
  hash[ini.getHash()] = 0;

  queue<State> que;
  que.push(ini);

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

    s.step++;

    int t = s.zero();

    for_(i, 0, 3) {
      int nxt = to[t][i];

      if (nxt != -1) {
        swap(s.p[t], s.p[nxt]);

        int hs = s.getHash();

        if (hash.find(hs) == hash.end()) {
          que.push(s);
          hash[hs] = s.step;
        }

        swap(s.p[t], s.p[nxt]);
      }
    }
  }

  int dumy;
  while (cin >> dumy) {
    ini.p[0] = dumy;
    for_(i, 1, 8) cin >> ini.p[i];
    cout << hash[ini.getHash()] << endl;
  }
}

Error: TLE

Fixed submission:

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

#define for_(i, a, b) for (int i = (a); i < (b); ++i)
#define size_of(a) (int)(a).size()
#define allof(a) (a).begin(), (a).end()

int to[8][3] = {
    {1, 4, -1}, {0, 2, 5}, {1, 3, 6}, {2, 7, -1},
    {0, 5, -1}, {1, 4, 6}, {2, 5, 7}, {3, 6, -1},
};

struct State {
  int step, p[8];

  bool isEnd() {
    for_(i, 0, 8) if (p[i] != i) return false;
    return true;
  }

  int zero() {
    for_(i, 0, 8) if (p[i] == 0) return i;
    return -1;
  }

  int getHash() {
    int hash = 0;
    for_(i, 0, 8) hash = 10 * hash + p[i];
    return hash;
  }
};

int main() {
  State ini;
  ini.step = 0;

  int dumy;
  while (cin >> dumy) {
    ini.p[0] = dumy;
    for_(i, 1, 8) cin >> ini.p[i];

    set<int> hash;

    queue<State> que;
    que.push(ini);

    hash.insert(ini.getHash());

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

      if (s.isEnd()) {
        cout << s.step << endl;
        break;
      }

      ++s.step;
      int t = s.zero();

      for_(j, 0, 3) {
        int nxt = to[t][j];

        if (nxt != -1) {
          swap(s.p[t], s.p[nxt]);

          int hs = s.getHash();

          if (hash.find(hs) == hash.end()) {
            que.push(s);
            hash.insert(hs);
          }

          swap(s.p[t], s.p[nxt]);
        }
      }
    }
  }
}
Problem p00121 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

map<string, int> dp;
int x[] = {1, -1, 4, -4};

void bfs() {
  dp["01234567"] = 0;
  queue<string> q;
  q.push("01234567");
  while (!q.empty()) {
    string now = q.front();
    q.pop();
    int p;
    for (int i = 0; i <= 7; ++i) {
      if (now[i] == '0') {
        p = i;
        break;
      }
    }
    for (int i = 0; i < 4; ++i) {
      int qn = p + x[i];
      if (qn < 0 || qn > 7 || (p == 3 && qn == 4) || (p == 4 && qn == 3))
        continue;
      string next = now;
      swap(next[p], next[qn]);
      if (dp.find(next) == dp.end()) {
        dp[next] = dp[now] + 1;
        q.push(next);
      }
    }
  }
}

int main() {
  bfs();
  string line;
  while (getline(cin, line)) {
    line.erase(remove(line.begin(), line.end(), ' '), line.end());
    cout << dp[line] << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

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

typedef long long ll;
const int N = 8;
const int M = 15;

int a[N];
int ans[1 << 24];
struct Node {
  int x, p, step;
};

inline int hs() {
  int ret = 0;
  for (int i = 0; i < 8; i++) {
    ret = (ret << 3) + a[i];
  }
  return ret;
}

inline void uhs(int x) {
  for (int i = 7; i >= 0; i--) {
    a[i] = x & 7;
    x >>= 3;
  }
}

void bfs() {
  memset(ans, -1, sizeof(ans));
  int src = 0;
  for (int i = 0; i < 8; i++) {
    src = (src << 3) + i;
  }
  queue<Node> que;
  que.push({src, 0, 0});
  ans[src] = 0;
  while (!que.empty()) {
    int x = que.front().x, p = que.front().p, step = que.front().step, t;
    que.pop();
    ans[x] = step;
    uhs(x);
    if (p >= 4) {
      swap(a[p], a[p - 4]);
      t = hs();
      if (ans[t] == -1) {
        que.push({t, p - 4, step + 1});
      }
      swap(a[p], a[p - 4]);
    } else {
      swap(a[p], a[p + 4]);
      t = hs();
      if (ans[t] == -1) {
        que.push({t, p + 4, step + 1});
      }
      swap(a[p], a[p + 4]);
    }
    if (p != 0 && p != 4) {
      swap(a[p], a[p - 1]);
      t = hs();
      if (ans[t] == -1) {
        que.push({t, p - 1, step + 1});
      }
      swap(a[p], a[p - 1]);
    }
    if (p != 3 && p != 7) {
      swap(a[p], a[p + 1]);
      t = hs();
      if (ans[t] == -1) {
        que.push({t, p + 1, step + 1});
      }
      swap(a[p], a[p + 1]);
    }
  }
}

int main() {
  bfs();
  while (~scanf("%d%d%d%d%d%d%d%d", a, a + 1, a + 2, a + 3, a + 4, a + 5, a + 6,
                a + 7)) {
    printf("%d\\n", ans[hs()]);
  }
}
Problem p00121 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

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

typedef pair<int, int> pii;

int encode(int *tile) {
  int res = tile[0];

  FOR(i, 1, 8) {
    res <<= 3;
    res |= tile[i];
  }
  return res;
}

void decode(int x, int *tile) {
  for (int i = 7; i >= 0; i--) {
    tile[i] = x & 7;
    x >>= 3;
  }
}

int main() {
  int tile[8];
  map<int, int> m[8];

  while (cin >> tile[0]) {
    FOR(i, 1, 8) cin >> tile[i];
    int ansT = encode(tile);

    if (m[tile[0]].find(ansT) != m[tile[0]].end()) {
      cout << m[tile[0]][ansT] << endl;
      continue;
    }

    set<int> s[8];
    queue<pii> que;
    int ans;

    REP(i, 8) tile[i] = i;

    que.push(pii(encode(tile), 0));
    while (!que.empty()) {
      int now = que.front().first;
      int cnt = que.front().second;
      que.pop();

      decode(now, tile);
      if (s[tile[0]].find(now) != s[tile[0]].end())
        continue;

      s[tile[0]].insert(now);
      m[tile[0]][now] = cnt;

      if (now == ansT) {
        ans = cnt;
        break;
      }

      int x, y;
      REP(i, 8) if (tile[i] == 0) {
        x = i % 4;
        y = i / 4;
      }

      int dx[4] = {1, 0, -1, 0};
      int dy[4] = {0, 1, 0, -1};
      REP(i, 4) {
        int nx = x + dx[i], ny = y + dy[i];
        if (!(nx >= 0 && nx < 4 && ny >= 0 && ny < 2))
          continue;

        swap(tile[y * 4 + x], tile[ny * 4 + nx]);
        que.push(pii(encode(tile), cnt + 1));
        swap(tile[y * 4 + x], tile[ny * 4 + nx]);
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

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

typedef pair<int, int> pii;

int make(int *tile) {
  int res = tile[0];

  FOR(i, 1, 8) {
    res <<= 3;
    res |= tile[i];
  }
  return res;
}

void decode(int x, int *tile) {
  for (int i = 7; i >= 0; i--) {
    tile[i] = x & ((1 << 3) - 1);
    x >>= 3;
  }
}

int main() {
  int tile[8];

  REP(i, 8) tile[i] = i;
  int ansT = make(tile);

  while (cin >> tile[0]) {
    FOR(i, 1, 8) cin >> tile[i];

    set<int> s;
    queue<pii> que;
    int ans;

    que.push(pii(make(tile), 0));
    while (!que.empty()) {
      int now = que.front().first;
      int cnt = que.front().second;
      que.pop();

      if (s.find(now) != s.end())
        continue;
      if (now == ansT) {
        ans = cnt;
        break;
      }

      s.insert(now);

      decode(now, tile);
      int x, y;
      REP(i, 8) if (tile[i] == 0) {
        x = i % 4;
        y = i / 4;
      }

      int dx[4] = {1, 0, -1, 0};
      int dy[4] = {0, 1, 0, -1};
      REP(i, 4) {
        int nx = x + dx[i], ny = y + dy[i];
        if (!(nx >= 0 && nx < 4 && ny >= 0 && ny < 2))
          continue;
        swap(tile[y * 4 + x], tile[ny * 4 + nx]);
        que.push(pii(make(tile), cnt + 1));
        swap(tile[y * 4 + x], tile[ny * 4 + nx]);
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00121 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <map>
#include <queue>
#define SF scanf
#define PF printf
using namespace std;
int s[11] = {1, 8, 64, 512, 4096, 32768, 262144, 2097152};
map<int, int> vis;
queue<int> q;
int push_down(int a[]) {
  int res = 0;
  for (int i = 1; i <= 8; i++)
    res += s[i - 1] * a[i];
  return res;
}
void push_up(int a[], int num) {
  for (int i = 7; i >= 0; i--) {
    a[i + 1] = num / s[i];
    num = num % s[i];
  }
}
void prepare() {
  q.push(16434824);
  vis[16434824] = 1;
  while (!q.empty()) {
    int a[10] = {0};
    int nex = q.front();
    q.pop();
    push_up(a, nex);
    for (int i = 1; i <= 8; i++)
      if (a[i] == 0) {
        if (i != 5 && i != 1) {
          swap(a[i], a[i - 1]);
          int newx = push_down(a);
          if (vis[newx] == 0) {
            vis[newx] = vis[nex] + 1;
            q.push(newx);
          }
          swap(a[i], a[i - 1]);
        }
        if (i != 4 && i != 8) {
          swap(a[i], a[i + 1]);
          int newx = push_down(a);
          if (vis[newx] == 0) {
            vis[newx] = vis[nex] + 1;
            q.push(newx);
          }
          swap(a[i], a[i + 1]);
        }
        if (i > 4) {
          swap(a[i], a[i - 4]);
          int newx = push_down(a);
          if (vis[newx] == 0) {
            vis[newx] = vis[nex] + 1;
            q.push(newx);
          }
          swap(a[i], a[i - 4]);
        }
        if (i < 5) {
          swap(a[i], a[i + 4]);
          int newx = push_down(a);
          if (vis[newx] == 0) {
            vis[newx] = vis[nex] + 1;
            q.push(newx);
          }
          swap(a[i], a[i + 4]);
        }
      }
  }
}
int main() {
  prepare();
  int a[11];
  while (SF("%d", &a[1]) != EOF) {
    int x = 0;
    for (int i = 2; i <= 8; i++)
      SF("%d", &a[i]);
    for (int i = 1; i <= 8; i++)
      x += a[i] * s[i - 1];
    PF("%d\\n", vis[x] - 1);
  }
}

Error: TLE

Fixed submission:

#define ll long long
#define ffor(i, a, b) for (int i = (a); i < (b); i++)
#define rfor(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#define SIZE 100001
#define MOD 1000000007
#define INF 100000000
using namespace std;
int sx, sy, gx, gy;
// 移動方向
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int card[8];
int d[2][8];
bool visited[101][101];
typedef pair<int, int> P;
int w, h;

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

  while (1001) {
    rep(i, 8) cin >> card[i];
    int count = 0;
    rep(i, 7) {
      for (int j = 7; j > i; j--) {
        if (card[j] < card[j - 1]) {
          int t = card[j];
          card[j] = card[j - 1];
          card[j - 1] = t;
          count++;
        }
      }
    }
    cout << count << endl;
  }

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

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <string>
using namespace std;
map<string, int> dp;
int direction[] = {1, -1, 4, -4};
// 分?代表右移、左移、下移、上移
void bfs() {
  queue<string> que;
  que.push("01234567");
  dp["01234567"] = 0;
  while (!que.empty()) {
    string now = que.front();
    que.pop();
    // p是字符串中0的位置
    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 n = p + direction[i];
      if (n >= 0 && n < 8 && !(p == 3 && i == 0) && !(p == 4 && i == 1)) {
        // 右上角不能向右移、左下角不能向左移
        string next = now;
        swap(next[p], next[n]);
        if (dp.find(next) == dp.end()) {
          // 用find函数来定位数据出?位置，它返回的一个迭代器，
          // 当数据出??，它返回数据所在位置的迭代器，
          // 如果map中没有要?找的数据，它返回的迭代器等于end函数返回的迭代器
          dp[next] = dp[now] + 1;
          que.push(next);
        }
      }
    }
  }
}
int main() {
  bfs();
  string s;
  while (getline(cin, s)) {
    s.erase(remove(s.begin(), s.end(), ' '), s.end());
    cout << dp[s] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#pragma GCC optimize(2)
#include <bits/stdc++.h>
using namespace std;
string bg, ed = "01234567", k1, k2;
int x, y, z, nx, ny, nz;
int a[11];
const int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
void bfs() {
  if (bg == ed) {
    printf("0\\n");
    return;
  }
  queue<string> p;
  map<string, int> s;
  p.push(bg);
  s[bg] = 0;
  while (!p.empty()) {
    k1 = p.front();
    p.pop();
    for (int i = 0; i < 8; i++)
      if (k1[i] == '0')
        z = i;
    x = z / 4, y = z % 4;
    for (int i = 0; i < 4; i++) {
      nx = x + dx[i], ny = y + dy[i], nz = nx * 4 + ny;
      if (nx >= 0 && nx < 2 && ny >= 0 && ny < 4 && nz < 8) {
        k2 = k1;
        swap(k2[z], k2[nz]);
        if (!s.count(k2)) {
          if (k2 == ed) {
            printf("%d\\n", s[k1] + 1);
            return;
          }
          p.push(k2);
          s[k2] = s[k1] + 1;
          swap(k2[z], k2[nz]);
        }
      }
    }
  }
}
int main() {
  while (scanf("%d", &a[0]) != EOF) {
    bg = "";
    bg += (char)(a[0] + '0');
    for (int i = 1; i < 8; i++) {
      scanf("%d", &a[i]);
      bg += (char)(a[i] + '0');
    }
    bfs();
  }
  return 0;
}