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

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <utility>
#include <vector>

using namespace std;

#define dump(n) cout << "# " << #n << "=" << (n) << endl
#define debug(n)                                                               \\
  cout << __FILE__ << "," << __LINE__ << ": #" << #n << "=" << (n) << endl
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define rep(i, n) repi(i, 0, n)
#define iter(c) __typeof((c).begin())
#define foreach(i, c) for (iter(c) i = (c).begin(); i != (c).end(); i++)
#define allof(c) (c).begin(), (c).end()
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;

int main() {
  queue<pair<string, int>> q;
  q.push(mp(string("01234567"), 0));
  map<string, int> res;
  while (q.size()) {
    string cur = q.front().first;
    int cur_count = q.front().second;
    q.pop();

    if (res.count(cur))
      continue;
    res[cur] = cur_count;

    int ci = -1, cj = -1;
    rep(i, 8) if (cur[i] == '0') ci = i / 4, cj = i % 4;
    int di[] = {-1, 1, 0, 0};
    int dj[] = {0, 0, -1, 1};
    rep(i, 4) {
      int ni = ci + di[i], nj = cj + dj[i];
      if (ni < 0 || 2 <= ni || nj < 0 || 4 <= nj)
        continue;
      string temp = cur;
      swap(temp[ci * 4 + cj], temp[ni * 4 + nj]);
      q.push(mp(temp, cur_count + 1));
    }
  }
  for (string s; getline(cin, s), s.size() == 15;) {
    s.erase(remove(allof(s), ' '), s.end());
    cout << res[s] << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <utility>
#include <vector>

using namespace std;

#define dump(n) cout << "# " << #n << "=" << (n) << endl
#define debug(n)                                                               \\
  cout << __FILE__ << "," << __LINE__ << ": #" << #n << "=" << (n) << endl
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define rep(i, n) repi(i, 0, n)
#define iter(c) __typeof((c).begin())
#define foreach(i, c) for (iter(c) i = (c).begin(); i != (c).end(); i++)
#define allof(c) (c).begin(), (c).end()
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;

bool is_end(const vi &a) {
  rep(i, 8) if (a[i] != i) return false;
  return true;
}

ostream &operator<<(ostream &os, const vi &a) {
  rep(i, a.size()) os << a[i] << (i == a.size() - 1 ? "" : " ");
  return os;
}

int solve(const vi &a) {
  queue<pair<vi, int>> q;
  set<vi> memo;
  q.push(mp(a, 0));
  while (q.size()) {
    vi cur = q.front().first;
    int cur_count = q.front().second;
    q.pop();
    if (memo.count(cur))
      continue;
    memo.insert(cur);

    if (is_end(cur))
      return cur_count;
    int ci = -1, cj = -1;
    rep(i, 8) if (cur[i] == 0) ci = i / 4, cj = i % 4;
    int di[] = {-1, 1, 0, 0};
    int dj[] = {0, 0, -1, 1};
    rep(i, 4) {
      int ni = ci + di[i], nj = cj + dj[i];
      if (ni < 0 || 2 <= ni || nj < 0 || 4 <= nj)
        continue;
      vi temp = cur;
      swap(temp[ci * 4 + cj], temp[ni * 4 + nj]);
      q.push(mp(temp, cur_count + 1));
    }
  }
  return -1; // dummy
}

int main() {
  for (string s; getline(cin, s), s.size() == 15;) {
    istringstream iss(s);
    vi a(8);
    rep(i, 8) iss >> a[i];
    cout << solve(a) << endl;
  }

  return 0;
}