Problem p00121 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <iterator>
#include <map>
#include <math.h>
#include <queue>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define rep(I, N) for (int I = 0; I < (int)(N); I++)
#define FIN(V) cout << V << endl
#define pb push_back
#define INF 1e+8
typedef pair<string, int> P;
typedef long long ll;
typedef priority_queue<int> pq;

map<string, int> data;
int direc[4] = {1, -1, 4, -4};

void bfs() {
  queue<P> que;
  que.push(P("01234567", 0));

  while (!que.empty()) {
    P p = que.front();
    que.pop();
    string panel = p.first;
    int now = p.second;

    rep(i, 4) {
      if (now + direc[i] >= 0 && now + direc[i] < 8) {
        if ((now == 3 && direc[i] == 1) || (now == 4 && direc[i] == -1))
          continue;
        string next = panel;
        next[now] = panel[now + direc[i]];
        next[now + direc[i]] = panel[now];

        if (data[next] != 0)
          continue;
        que.push(P(next, now + direc[i]));
        data[next] = data[panel] + 1;
      }
    }
  }
}

int main(void) {
  data["01234567"] = 1;
  string a;
  bfs();
  while (cin >> a) {
    string S, L;
    S += a;
    for (int i = 1; i <= 7; i++) {
      cin >> L;
      S += L;
    }
    cout << data[S] - 1 << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define FOR(I, N) for (int I = 0; I < (int)(N); I++)
#define FIN(V) cout << V << endl
#define pb push_back
#define INF (1 << 30)
typedef pair<int, int> P;

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

struct Data {
  string panel;
  int space, cost;
  Data(string panel_, int space_, int cost_) {
    panel = panel_;
    space = space_;
    cost = cost_;
  }
  Data() {}
};
bool check(string s) {
  FOR(i, 8) {
    if (s[i] != '0' + i)
      return false;
  }
  return true;
}

int bfs(Data d) {
  map<string, bool> mp;
  queue<Data> que;
  que.push(d);
  mp[d.panel] = 1;
  while (!que.empty()) {
    Data now = que.front();
    que.pop();
    int cost = now.cost;

    if (check(now.panel)) {
      return cost;
    }

    int idx1 = now.space;
    FOR(i, 4) {
      int idx2 = idx1 + mv[i];
      if (idx2 == 4 && idx1 == 3)
        continue;
      if (idx2 == 3 && idx1 == 4)
        continue;
      if (idx2 < 0 || idx2 >= 8)
        continue;
      //??????????\\?
      string next = now.panel;
      next[idx1] = now.panel[idx2];
      next[idx2] = now.panel[idx1];
      if (mp.count(next))
        continue;

      Data next_stat;
      next_stat.panel = next;
      next_stat.space = idx2;
      next_stat.cost = cost + 1;

      mp[next_stat.panel] = 1;
      que.push(next_stat);
    }
  }
  return 0;
}

int main(void) {
  int n, space;
  string c;

  while (cin >> c) {
    string panel;
    panel += c;
    if (c[0] == '0')
      space = 0;
    for (int i = 1; i <= 7; i++) {
      string S;
      cin >> S;
      panel += S;
      if (c[i] == '0')
        space = i;
    }
    cout << panel << endl;
    Data d(panel, space, 0);
    FIN(bfs(d));
  }
  return 0;
}