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

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define foreach(c, i)                                                          \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)

typedef pair<string, int> P;
unordered_map<string, int> m;
const int d[] = {-4, 1, 4, -1};

void f() {
  queue<P> Q;
  Q.push(P(string("01234567"), 0));
  while (!Q.empty()) {
    P p = Q.front();
    Q.pop();
    int cost = p.second;
    if (m.count(p.first))
      continue;
    m.insert(P(p.first, cost));
    int zidx;
    for (int i = 0; i < 8; ++i)
      if (p.first[i] == '0')
        zidx = i;
    for (int i = 0; i < 4; ++i) {
      if (zidx == 3 && i == 1)
        continue;
      if (zidx == 4 && i == 3)
        continue;
      int tidx = zidx + d[i];
      if (tidx < 0 || 8 <= tidx)
        continue;
      string copy = p.first;
      swap(copy[zidx], copy[tidx]);
      Q.push(P(copy, cost + 1));
    }
  }
}

int main() {
  f();
  int t;
  while (cin >> t) {
    string s = "";
    s += '0' + t;
    for (int i = 1; i <= 7; ++i) {
      cin >> t;
      s += '0' + t;
    }
    cout << m[s] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define foreach(c, i)                                                          \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)

int t[10];
typedef pair<string, int> P;
unordered_set<string> memo;

// 1 - up, 2 - right, 3 - down, 4 - left
string create(const string &base, const int zidx, const int dir) {
  string r(base);
  switch (dir) {
  case 1: {
    swap(r[zidx], r[zidx - 4]);
    break;
  }
  case 2: {
    swap(r[zidx], r[zidx + 1]);
    break;
  }
  case 3: {
    swap(r[zidx], r[zidx + 4]);
    break;
  }
  case 4: {
    swap(r[zidx], r[zidx - 1]);
    break;
  }
  }
  return r;
}

int solve() {
  string s;
  for (int i = 0; i < 8; ++i)
    s += '0' + t[i];
  queue<P> Q;
  Q.push(P(s, 0));

  const string goal = "01234567";
  while (!Q.empty()) {
    P p = Q.front();
    Q.pop();
    if (memo.count(p.first))
      continue;
    memo.insert(p.first);
    // is end
    if (p.first == goal) {
      return p.second;
    }
    int zidx;
    const int nc = p.second + 1;
    for (int i = 0; i < 8; ++i)
      if (p.first[i] == '0')
        zidx = i;
    switch (zidx) {
    case 0: {
      Q.push(P(create(p.first, zidx, 2), nc));
      Q.push(P(create(p.first, zidx, 3), nc));
      break;
    }
    case 1:
    case 2: {
      Q.push(P(create(p.first, zidx, 2), nc));
      Q.push(P(create(p.first, zidx, 3), nc));
      Q.push(P(create(p.first, zidx, 4), nc));
      break;
    }
    case 3: {
      Q.push(P(create(p.first, zidx, 3), nc));
      Q.push(P(create(p.first, zidx, 4), nc));
      break;
    }
    case 4: {
      Q.push(P(create(p.first, zidx, 1), nc));
      Q.push(P(create(p.first, zidx, 2), nc));
      break;
    }
    case 5:
    case 6: {
      Q.push(P(create(p.first, zidx, 1), nc));
      Q.push(P(create(p.first, zidx, 2), nc));
      Q.push(P(create(p.first, zidx, 4), nc));
      break;
    }
    case 7: {
      Q.push(P(create(p.first, zidx, 1), nc));
      Q.push(P(create(p.first, zidx, 4), nc));
      break;
    }
    }
  }
  return 0;
}

int main() {
  while (scanf("%d", t) != -1) {
    for (int i = 1; i < 8; ++i)
      scanf("%d", t + i);
    memo.clear();
    printf("%d\\n", solve());
  }
  return 0;
}