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

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define reep(i, f, n) for (int i = f; i < int(n); ++i)
#define rep(i, n) reep(i, 0, n)

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef complex<double> Point;

const int dy[] = {0, -1, 0, 1};
const int dx[] = {-1, 0, 1, 0};
map<int, int> table;

int main() {
  int depth = 0;
  int next = -1;
  map<int, int> table;
  queue<int> Q;
  Q.push(01234567);
  while (!Q.empty()) {
    int num = Q.front();
    Q.pop();
    if (table.count(num))
      continue;

    if (num == next) {
      next = -1;
      ++depth;
    }
    table[num] = depth;

    int zero;
    rep(i, 8) {
      if (((num >> (3 * i)) & 7) == 0) {
        zero = i;
        break;
      }
    }

    rep(i, 4) {
      int py = zero / 4 + dy[i];
      int px = zero % 4 + dx[i];
      if (py < 0 || 2 <= py || px < 0 || 4 <= px)
        continue;

      int dst = py * 4 + px;
      int tmp[] = {(num >> (3 * dst)) & 7, (num >> (3 * zero)) & 7};
      int ko = (num | (7 << (3 * zero))) &
               (((1 << 24) - 1 - (7 << (3 * zero))) | (tmp[0] << (3 * zero)));
      ko = (ko | (7 << (3 * dst))) &
           (((1 << 24) - 1 - (7 << (3 * dst))) | (tmp[1] << (3 * dst)));
      if (!table.count(ko)) {
        Q.push(ko);
        if (next == -1)
          next = ko;
      }
    }
  }

  for (;;) {
    int num = 0;
    rep(i, 8) {
      int a;
      if (scanf("%d", &a) == EOF)
        return 0;
      num = num * 8 + a;
    }
    printf("%d\\n", table[num]);
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define reep(i, f, n) for (int i = f; i < int(n); ++i)
#define rep(i, n) reep(i, 0, n)

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef complex<double> Point;

int solve(int puzzle[]) {
  set<int> visited;

  int num = 0;
  for (int i = 7; i >= 0; --i)
    num = num * 10 + puzzle[i];
  queue<pii> Q;
  Q.push(pii(num, 0));

  int ans;
  while (!Q.empty()) {
    pii p = Q.front();
    Q.pop();
    if (visited.count(p.first))
      continue;
    visited.insert(p.first);

    if (p.first == 76543210) {
      ans = p.second;
      break;
    }

    pii zero;
    int puz[8];
    rep(i, 8) {
      puz[i] = p.first % 10;
      p.first /= 10;
      if (puz[i] == 0)
        zero = pii(i / 4, i % 4);
    }

    const int dy[] = {-1, 0, 0, 1};
    const int dx[] = {0, -1, 1, 0};
    rep(i, 4) {
      int py = zero.first + dy[i];
      int px = zero.second + dx[i];
      if (py < 0 || 2 <= py || px < 0 || 4 <= px)
        continue;
      swap(puz[zero.first * 4 + zero.second], puz[py * 4 + px]);
      int num = 0;
      for (int j = 7; j >= 0; --j)
        num = num * 10 + puz[j];
      Q.push(pii(num, p.second + 1));
      swap(puz[zero.first * 4 + zero.second], puz[py * 4 + px]);
    }
  }

  return ans;
}

int main() {
  int puzzle[8];
  for (;;) {
    rep(i, 8) {
      if (scanf("%d", &puzzle[i]) == EOF)
        return 0;
    }
    printf("%d\\n", solve(puzzle));
  }
}