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

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

vector<int> cm[8];

typedef pair<string, int> P;

map<string, int> m;

inline void SCM(int i, int v) {
  cm[i].push_back(v);
  cm[v].push_back(i);
}

void Initcm() {
  SCM(0, 1);
  SCM(1, 2);
  SCM(2, 3);
  SCM(4, 5);
  SCM(5, 6);
  SCM(6, 7);
  SCM(0, 4);
  SCM(1, 5);
  SCM(2, 6);
  SCM(3, 7);
}

void bfs() {
  queue<P> q;
  q.push(P(
      "01234567",
      1)); // if()でその値があるかどうかSerchしやすくするために+1そのため結果は-1
  while (!q.empty()) {
    P p = q.front();
    q.pop();
    if (m[p.first])
      continue; // 値が存在するとき
    m[p.first] = p.second;
    int cnum;
    for (int i = 0; i < 8; ++i) {
      if (p.first[i] == '0') {
        cnum = i;
        break;
      }
    }
    for (int i = 0; i < cm[cnum].size(); ++i) {
      string str = p.first;
      char c = str[cnum];
      str[cnum] = str[cm[cnum].at(i)];
      str[cm[cnum].at(i)] = c;
      q.push(P(str, p.second + 1));
    }
  }
}

int main(void) {
  Initcm();
  bfs();
  while (1) {
    string a;
    string sum;
    for (int i = 0; i < 8; ++i) {
      cin >> a;
      sum += a;
    }
    if (cin.eof())
      return 0;
    cout << m[sum] - 1 << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:


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

vector<int> cm[8];

typedef pair<string, int> P;

map<string, int> m;

inline void SCM(int i, int v) {
  cm[i].push_back(v);
  cm[v].push_back(i);
}

void Initcm() {
  SCM(0, 1);
  SCM(1, 2);
  SCM(2, 3);
  SCM(4, 5);
  SCM(5, 6);
  SCM(6, 7);
  SCM(0, 4);
  SCM(1, 5);
  SCM(2, 6);
  SCM(3, 7);
}

void bfs() {
  queue<P> q;
  q.push(P(
      "01234567",
      1)); // if()でその値があるかどうかSerchしやすくするために+1そのため結果は-1
  while (!q.empty()) {
    P p = q.front();
    q.pop();
    if (m[p.first])
      continue; // 値が存在するとき
    m[p.first] = p.second;
    int cnum;
    for (int i = 0; i < 8; ++i) {
      if (p.first[i] == '0') {
        cnum = i;
        break;
      }
    }
    for (int i = 0; i < cm[cnum].size(); ++i) {
      string str = p.first;
      char c = str[cnum];
      str[cnum] = str[cm[cnum].at(i)];
      str[cm[cnum].at(i)] = c;
      q.push(P(str, p.second + 1));
    }
  }
}

int main(void) {
  Initcm();
  bfs();
  while (1) {
    string a;
    string sum;
    for (int i = 0; i < 8; ++i) {
      cin >> a;
      sum += a;
    }
    cout << m[sum] - 1 << endl;
  }
  return 0;
}
Problem p00121 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

struct S {
  int co;
  string str;
  int z;
};
map<string, int> cnt;

void dfs() {
  ;
  int d[2][3] = {{-1, 1, 4}, {-1, 1, -4}};
  queue<S> que;
  que.push((S){0, "01234567", 0});
  while (!que.empty()) {
    S s = que.front();
    que.pop();
    int co = s.co;
    string puzzu = s.str;
    string tmp;
    int z = s.z;
    if (cnt.count(puzzu) != 0)
      continue;
    cnt[puzzu] = co;
    for (int i = 0; i < 3; i++) {
      if (!(z == 3 && z + d[z / 4][i] == 4) &&
          !(z == 4 && z + d[z / 4][i] == 3) && z + d[z / 4][i] >= 0 &&
          z + d[z / 4][i] < 8) {
        tmp = puzzu;
        swap(tmp[z], tmp[z + d[z / 4][i]]);
        que.push((S){co + 1, tmp, z + d[z / 4][i]});
      }
    }
  }
}

int main() {
  string imput = "00000000";
  dfs();
  while (cin >> imput[0]) {
    for (int i = 1; i < 8; i++) {
      cin >> imput[i];
    }
    cout << cnt[imput] << endl;
  }
}

Error: MLE

Fixed submission:

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

typedef struct {
  string str;
  int idx;
} Node;
map<string, int> know;
int v[4] = {-1, 1, -4, 4};

void solve();

int main() {
  char num;
  solve();
  while (1) {
    // 入力
    string card;
    for (int i = 0; i < 8; i++) {
      if (!(cin >> num))
        return 0;
      card += num;
    }

    cout << know[card] << endl;
  }
}
void solve() {
  map<string, int> kaku;
  queue<Node> que;
  que.push((Node){"01234567", 0});
  kaku["01234567"] = 2;
  know["01234567"] = 0;
  // 幅
  while (!que.empty()) {
    Node now = que.front();
    que.pop();
    string temp;

    for (int i = 0; i < 4; i++) {
      temp = now.str;
      if (now.idx + v[i] >= 0 || now.idx + v[i] <= 7 &&
                                     (now.idx + v[i] != 4 && now.idx == 3) &&
                                     (now.idx == 4 && now.idx + v[i] != 3)) {
        swap(temp[now.idx], temp[now.idx + v[i]]);

        if (kaku[temp] == 2) {
          continue;
        }
        kaku[temp] = 2;

        que.push((Node){temp, now.idx + v[i]});
        know[temp] = know[now.str] + 1;
      }
    }
  }
}
Problem p00121 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

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

  while (!que.empty()) {
    std::string p = que.front();
    que.pop();

    int s;
    for (int i = 0; i != 8; ++i)
      if (p[i] == '0')
        s = i;

    for (int i = 0; i != 4; ++i) {
      int ns = s + dir[i];

      if (ns < 0 || ns > 7 || (s == 3 && i == 2) || (s == 4 && !i))
        continue;

      std::string q = p;
      std::swap(q[s], q[ns]);

      if (!res[q]) {
        que.push(q);
        res[q] = res[p] + 1;
      }
    }
  }

  return;
}

int main() {
  res["01234567"] = 1;

  bfs();

  while (true) {
    std::string s;

    for (int i = 0; i != 8; ++i) {
      int a;
      if (!(std::cin >> a))
        return 0;

      s += a + '0';
    }

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

  return 0;
}

Error: TLE

Fixed submission:

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

std::string s;
int dir[4] = {-1, -4, 1, 4};
std::map<std::string, int> res;

int main() {
  int a;
  while (std::cin >> a) {
    res.clear();
    s.clear();
    s += a + '0';
    for (int i = 0; i != 7; ++i)
      std::cin >> a, s += a + '0';

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

    while (!que.empty()) {
      std::string p = que.front();
      que.pop();

      if (p == s) {
        std::cout << res[s] << std::endl;
        break;
      }

      int s;
      for (int i = 0; i != 8; ++i)
        if (p[i] == '0')
          s = i;

      for (int i = 0; i != 4; ++i) {
        int ns = s + dir[i];

        if (ns < 0 || ns > 7 || (s == 3 && i == 2) || (s == 4 && !i))
          continue;

        std::string q = p;
        std::swap(q[s], q[ns]);

        if (!res[q]) {
          que.push(q);
          res[q] = res[p] + 1;
        }
      }
    }
  }

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

#include <cstdio>
#include <queue>
#include <vector>
using namespace std;

short ans[1 << 24];

int adj[] = {-6, -1, 1, 6};
int pos[] = {7, 8, 9, 10, 13, 14, 15, 16};

int encode(const vector<int> &v) {
  int r = 0;
  for (int i = 0; i < 8; ++i) {
    r |= v[pos[i]] << (3 * i);
  }
  return r;
}

void decode(int r, vector<int> &ret) {
  ret.assign(24, -1);
  for (int i = 0; i < 8; ++i) {
    ret[pos[i]] = r & 7;
    r >>= 3;
  }
}

int main() {
  for (int i = 0; i < (1 << 24); ++i) {
    ans[i] = -1;
  }

  vector<int> v(24, -1), w;
  for (int i = 0; i < 8; ++i) {
    v[pos[i]] = i;
  }
  int c = encode(v);
  ans[c] = 0;
  queue<int> q;
  q.push(c);
  q.push(-1);
  int tm = 1;
  while (q.size() > 1) {
    c = q.front();
    q.pop();
    if (c == -1) {
      ++tm;
      q.push(-1);
    } else {
      decode(c, v);
      int x;
      for (x = 7; v[x]; ++x)
        ;
      for (int i = 0; i < 4; ++i) {
        int y = x + adj[i];
        if (v[y] > 0) {
          swap(v[x], v[y]);
          c = encode(v);
          if (ans[c] == -1) {
            ans[c] = tm;
            q.push(c);
          }
          swap(v[x], v[y]);
        }
      }
    }
  }

  while (1) {
    for (int i = 0; i < 8; ++i) {
      if (scanf("%d", &v[pos[i]]) == EOF) {
        return 0;
      }
    }
    printf("%d\\n", ans[encode(v)]);
  }
}

Error: MLE

Fixed submission:

#include <cstdio>
#include <queue>
#include <vector>
using namespace std;

int ans[1 << 24];

int adj[] = {-6, -1, 1, 6};
int pos[] = {7, 8, 9, 10, 13, 14, 15, 16};

int encode(const vector<int> &v) {
  int r = 0;
  for (int i = 0; i < 8; ++i) {
    r |= v[pos[i]] << (3 * i);
  }
  return r;
}

void decode(int r, vector<int> &ret) {
  ret.assign(24, -1);
  for (int i = 0; i < 8; ++i) {
    ret[pos[i]] = r & 7;
    r >>= 3;
  }
}

int main() {
  for (int i = 0; i < (1 << 24); ++i) {
    ans[i] = -1;
  }

  vector<int> v(24, -1), w;
  for (int i = 0; i < 8; ++i) {
    v[pos[i]] = i;
  }
  int c = encode(v);
  ans[c] = 0;
  queue<int> q;
  q.push(c);
  q.push(-1);
  int tm = 1;
  while (q.size() > 1) {
    c = q.front();
    q.pop();
    if (c == -1) {
      ++tm;
      q.push(-1);
    } else {
      decode(c, v);
      int x;
      for (x = 7; v[x]; ++x)
        ;
      for (int i = 0; i < 4; ++i) {
        int y = x + adj[i];
        if (v[y] > 0) {
          swap(v[x], v[y]);
          c = encode(v);
          if (ans[c] == -1) {
            ans[c] = tm;
            q.push(c);
          }
          swap(v[x], v[y]);
        }
      }
    }
  }

  while (1) {
    for (int i = 0; i < 8; ++i) {
      if (scanf("%d", &v[pos[i]]) == EOF) {
        return 0;
      }
    }
    printf("%d\\n", ans[encode(v)]);
  }
}
Problem p00121 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <string>
using namespace std;
string s;
map<string, int> v;
int main() {
  queue<pair<string, int>> q;
  q.push({"01234567", 0});
  v["01234567"] = 0;
  while (!q.empty()) {
    string ss = q.front().first;
    int step = q.front().second;
    q.pop();
    int p = 0;
    while (p < 8 && ss[p] != '0')
      ++p;
    swap(ss[p], ss[p + (p < 4 ? 4 : -4)]);
    if (!v.count(ss)) {
      v[ss] = step + 1;
      q.push({ss, step + 1});
    }
    swap(ss[p], ss[p + (p < 4 ? 4 : -4)]);
    if (p % 4 != 3) {
      swap(ss[p], ss[p + 1]);
      if (!v.count(ss)) {
        v[ss] = step + 1;
        q.push({ss, step + 1});
      }
      swap(ss[p], ss[p + 1]);
    }
    if (p % 4 != 0) {
      swap(ss[p], ss[p - 1]);
      if (!v.count(ss)) {
        v[ss] = step + 1;
        q.push({ss, step + 1});
      }
      swap(ss[p], ss[p - 1]);
    }
  }
  while (getline(cin, s)) {
    s.erase(remove(s.begin(), s.end(), ' '), s.end());
    cout << v[s] << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <string>
using namespace std;
string s;
string t = "01234567";
int main() {
  while (getline(cin, s)) {
    s.erase(remove(s.begin(), s.end(), ' '), s.end());
    int ans = 0;
    queue<pair<string, int>> q;
    q.push({s, 0});
    set<string> v;
    while (!q.empty()) {
      string ss = q.front().first;
      int step = q.front().second;
      if (ss == t) {
        ans += step;
        break;
      }
      q.pop();
      int p = 0;
      while (p < 8 && ss[p] != '0')
        ++p;
      swap(ss[p], ss[p + (p < 4 ? 4 : -4)]);
      if (!v.count(ss)) {
        v.insert(ss);
        q.push({ss, step + 1});
      }
      swap(ss[p], ss[p + (p < 4 ? 4 : -4)]);
      if (p % 4 != 3) {
        swap(ss[p], ss[p + 1]);
        if (!v.count(ss)) {
          v.insert(ss);
          q.push({ss, step + 1});
        }
        swap(ss[p], ss[p + 1]);
      }
      if (p % 4 != 0) {
        swap(ss[p], ss[p - 1]);
        if (!v.count(ss)) {
          v.insert(ss);
          q.push({ss, step + 1});
        }
        swap(ss[p], ss[p - 1]);
      }
    }
    cout << ans << endl;
  }

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

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

using namespace std;

const string goal = "01234567";
typedef pair<string, int> P;
map<string, int> M;

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

int getpos(string s) {

  for (int i = 0; i < s.length(); i++)
    if (s[i] == '0')
      return i;

  return 0;
}

vector<int> getable(int n) {
  vector<int> V;

  for (int i = 0; i < 4; i++) {
    int nex = n + dx[i];
    if (0 <= nex && nex <= 7) {
      if (!(n == 3 && nex == 4 || n == 4 && nex == 3))
        V.push_back(nex);
    }
  }
  return V;
}

void make() {
  queue<string> Q;

  Q.push(goal);
  M[goal] = 1;

  while (!Q.empty()) {
    string s = Q.front();
    Q.pop();
    int pos = getpos(s);
    vector<int> able = getable(pos);

    for (int i = 0; i < able.size(); i++) {
      string next = s;
      swap(next[pos], next[able[i]]);
      if (M[next] == 0) {
        M[next] = M[s] + 1;
        Q.push(next);
      }
    }
  }
}
int main() {
  make();

  int data[8];

  while (1) {
    for (int i = 0; i < 8; i++)
      if (!(cin >> data[i]))
        return 0;

    string s = "";

    for (int i = 0; i < 8; i++)
      s += data[i] + '0';

    cout << M[s] - 1 << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

const string goal = "01234567";

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

int getpos(string s) {

  for (int i = 0; i < s.length(); i++)
    if (s[i] == '0')
      return i;

  return 0;
}

vector<int> getable(int n) {
  vector<int> V;

  for (int i = 0; i < 4; i++) {
    int nex = n + dx[i];
    if (0 <= nex && nex <= 7) {
      if (!(n == 3 && nex == 4 || n == 4 && nex == 3))
        V.push_back(nex);
    }
  }
  /*
  for(int i = 0; i < V.size(); i++)
    cout << V[i] << " " ;
    cout << endl;
  */
  return V;
}

int main() {

  int data[8];

  while (1) {

    map<string, int> M;
    queue<string> Q;

    for (int i = 0; i < 8; i++)
      if (!(cin >> data[i]))
        return 0;

    string s = "";

    for (int i = 0; i < 8; i++)
      s += data[i] + '0';

    if (s == goal)
      cout << "0" << endl;
    else {
      M[s] = 1;
      Q.push(s);

      bool flg = false;

      while (!Q.empty()) {
        string ss = Q.front();
        Q.pop();

        int pos = getpos(ss);

        vector<int> able = getable(pos);

        for (int i = 0; i < able.size(); i++) {
          string next = ss;
          swap(next[pos], next[able[i]]);

          if (next == goal) {
            cout << M[ss] << endl;
            flg = true;
            break;
          } else if (M[next] == 0) {
            M[next] = M[ss] + 1;
            Q.push(next);
          }
        }
        if (flg)
          break;
      }
    }
  }

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

#include <stdio.h>
#include <stdlib.h>

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

using namespace std;

#define L 8

map<string, int> state;
int mv[4] = {1, -1, 4, -4};

// int bfs(string start, string goal)
void bfs(string start) {
  int res = 0;
  int i, j;

  queue<string> q;
  q.push(start);

  while (q.size()) {

    string t = q.front();
    q.pop();

    /*
                    if (t == goal) {
                            res = state[t];

    cout << "Found goal:" << t << '\\n';

                            break;
                    }
    */

    int zPos = t.find("0");

    for (i = 0; i < 4; i++) {
      int nmv = zPos + mv[i];
      if (0 <= nmv && nmv < L && !(zPos == 4 && nmv == 3) &&
          !(zPos == 3 && nmv == 4)) {
        string u = t;
        swap(u[zPos], u[nmv]);

        if (!state.count(u)) {
          q.push(u);
          state[u] = state[t] + 1;
          // cout << "pushed:" << u << ", count:" << state[u] << '\\n';
        }
      }
    }
  }
}

int main() {

  int i, j;
  string start = "01234567";
  string goal;
  state[start] = 0;

  bfs(start);

  while (1) {
    // cout << "-----------------------------" << '\\n';
    // input
    char c;
    if (scanf("%c ", &c) == EOF)
      break;
    goal = c;
    for (i = 1; i < L; i++) {
      scanf("%c ", &c);
      goal += c;
    }

    // bfs & output
    // printf("%d\\n", bfs(start, goal));
    printf("%d\\n", state[goal]);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>
#include <stdlib.h>

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

using namespace std;

#define L 8

map<string, int> state;
int mv[4] = {1, -1, 4, -4};

int bfs(string start, string goal) {
  int res = 0;
  int i, j;

  //	map<string, int> state;
  queue<string> q;
  map<string, bool> passed;
  q.push(start);
  passed[start] = true;

  // cout << "start:" << start << '\\n';
  // cout << "goal: " << goal  << '\\n';

  while (q.size()) {

    string t = q.front();
    q.pop();

    if (t == goal) {
      res = state[t];

      // cout << "Found goal:" << t << '\\n';

      break;
    }

    int zPos = t.find("0");

    for (i = 0; i < 4; i++) {
      int nmv = zPos + mv[i];
      if (0 <= nmv && nmv < L && !(zPos == 4 && nmv == 3) &&
          !(zPos == 3 && nmv == 4)) {
        string u = t;
        swap(u[zPos], u[nmv]);

        //				if (!state.count(u)) {
        if (!passed[u]) {
          q.push(u);
          passed[u] = true;

          if (!state.count(u))
            state[u] = state[t] + 1;

          // cout << "pushed:" << u << '\\n';
        }
      }
    }
  }

  return res;
}

int main() {

  int i, j;
  string start = "01234567";
  string goal;
  state[start] = 0;

  while (1) {
    // cout << "-----------------------------" << '\\n';
    // input
    char c;
    if (scanf("%c ", &c) == EOF)
      break;
    goal = c;
    for (i = 1; i < L; i++) {
      scanf("%c ", &c);
      goal += c;
    }

    // bfs & output
    printf("%d\\n", bfs(start, goal));
  }

  return 0;
}
Problem p00121 (C++) — buggy submission, verdict: Time 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: TLE

Fixed submission:

#include <stdio.h>
#include <stdlib.h>

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

using namespace std;

#define L 8

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

int bfs(string start, string goal) {
  int res = 0;
  int i, j;

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

  while (q.size()) {

    string t = q.front();
    q.pop();

    if (t == goal) {
      res = state[t];
      break;
    }

    //		unsigned int zPos = t.find("0");
    unsigned int zPos;

    for (i = 0; i < L; i++)
      if (t[i] == '0')
        zPos = i;

    for (i = 0; i < 4; i++) {
      int nmv = zPos + mv[i];
      if (0 <= nmv && nmv < L && !(zPos == 4 && nmv == 3) &&
          !(zPos == 3 && nmv == 4)) {
        string u = t;
        swap(u[zPos], u[nmv]);

        if (!state.count(u)) {
          q.push(u);
          state[u] = state[t] + 1;
        }
      }
    }
  }

  return res;
}

int main() {

  int i, j;
  string start;
  string goal = "01234567";

  while (1) {
    // input
    char c;
    if (scanf("%c ", &c) == EOF)
      break;
    start = c;
    for (i = 1; i < L; i++) {
      scanf("%c ", &c);
      start += c;
    }

    // bfs & output
    printf("%d\\n", bfs(start, goal));
  }

  return 0;
}
Problem p00121 (C++) — buggy submission, verdict: Time 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: TLE

Fixed submission:

#include <stdio.h>
#include <stdlib.h>

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

using namespace std;

#define L 8

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

int bfs(string start, string goal) {
  int res = 0;
  int i, j;

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

  while (q.size()) {

    string t = q.front();
    q.pop();

    if (t == goal) {
      res = state[t];
      break;
    }

    unsigned int zPos = t.find("0");
    for (i = 0; i < 4; i++) {
      int nmv = zPos + mv[i];
      if (0 <= nmv && nmv < L) {
        string u = t;
        u.replace(zPos, 1, t.substr((unsigned int)nmv, 1));
        u.replace((unsigned int)nmv, 1, "0");

        if (!state[u]) {
          q.push(u);
          state[u] = state[t] + 1;
        }
      }
    }
  }

  return res;
}

int main() {

  int i, j;
  string start;
  string goal = "01234567";

  while (1) {
    // input
    char c;
    if (scanf("%c ", &c) == EOF)
      break;
    start = c;
    for (i = 1; i < L; i++) {
      scanf("%c ", &c);
      start += c;
    }

    // bfs & output
    printf("%d\\n", bfs(start, goal));
  }

  return 0;
}