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

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#define INF 10000000
#define I64 long long int
#define FOR(a, start, end) for (I64 a = (start); a < (end); a++)
using namespace std;

int main() {
  // 4ûüpzñ
  int vec[] = {1, -1, 4, -4};
  queue<string> que;
  que.push("01234567");

  // TõÏÝe[u
  map<string, int> d;

  //"01234567"©çtü«ÉDæTõµÈªçTõÏÝe[uðßé
  while (!que.empty()) {
    string str = que.front();
    que.pop();
    int zero = str.find("0");

    string str2;
    FOR(i, 0, 4) {
      int next_z;
      next_z = zero + vec[i];
      if (next_z >= 0 && next_z <= 7 &&
          !((zero == 3 && next_z == 4) || (zero == 4 && next_z == 3))) {
        str2 = str;
        swap(str2[zero], str2[next_z]);
        if (d.find(str2) == d.end()) {
          que.push(str2);
          d[str2] = d[str] + 1;
        }
      }
    }
  }

  // üÍ©ç'
  // 'ð¢ÄTõÏÝe[u©ç¦ðoÍ
  string num;
  while (getline(cin, num)) {
    string::iterator end_it = remove(num.begin(), num.end(), ' ');
    num.erase(end_it, num.end());
    cout << d[num] << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

const double EPS = 1e-10;
const double PI = acos(-1.0);

#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

int solve(string goal) {

  string cards = "01234567";

  queue<string> q;
  q.push("01234567");
  map<string, int> d;
  d["01234567"] = 0;

  while (!q.empty()) {
    string s = q.front();
    q.pop();

    if (s == goal)
      return d[s];

    /*
    cerr << "--(" << p.first << ")--" << endl;
    REP(i, 4) cerr << p.second[i]; cerr << endl;
    REP(i, 4) cerr << p.second[i+4]; cerr << endl;
    */
    int pos0 = 0;
    REP(i, 8) if (s[i] == '0') pos0 = i;
    if (pos0 % 4 > 0) {
      string s2 = s;
      swap(s2[pos0], s2[pos0 - 1]);
      if (d.find(s2) == d.end()) {
        d[s2] = d[s] + 1;
        q.push(s2);
      }
    }
    if (pos0 % 4 < 3) {
      string s2 = s;
      swap(s2[pos0], s2[pos0 + 1]);
      if (d.find(s2) == d.end()) {
        d[s2] = d[s] + 1;
        q.push(s2);
      }
    }
    if (pos0 / 4 == 0) {
      string s2 = s;
      swap(s2[pos0], s2[pos0 + 4]);
      if (d.find(s2) == d.end()) {
        d[s2] = d[s] + 1;
        q.push(s2);
      }
    } else {
      string s2 = s;
      swap(s2[pos0], s2[pos0 - 4]);
      if (d.find(s2) == d.end()) {
        d[s2] = d[s] + 1;
        q.push(s2);
      }
    }
  }
  return -1;
}

int main() {
  string s;
  while (getline(cin, s)) {
    stringstream sin(s);
    string cards = "        ";
    REP(i, 8) sin >> cards[i];
    // cerr << cards << endl;
    int answer = solve(cards);
    cout << answer << endl;
  }
}