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

// include
//------------------------------------------
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#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
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

// container util
//------------------------------------------
#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())

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

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

struct B {
  int n[8];
  int p;

  void dup(const B &rhs) {
    REP(i, 8) n[i] = rhs.n[i];
    p = rhs.p;
  }

  int encode() {
    int p = 1, res = 0;
    for (int i = 0; i < 8; ++i) {
      res += p * n[i];
      p *= 8;
    }
    return res;
  }
};

map<int, int> mem;
void calc() {
  B b;
  REP(i, 8) b.n[i] = i;
  b.p = 0;
  queue<B> q;
  q.push(b);
  mem[b.encode()] = 0;

  while (!q.empty()) {
    B pazz = q.front();
    q.pop();
    int en = pazz.encode();

    if (pazz.p < 4) {
      B after;
      after.dup(pazz);
      swap(after.n[after.p], after.n[after.p + 4]);
      after.p += 4;
      if (mem.find(after.encode()) == mem.end()) {
        mem[after.encode()] = mem[en] + 1;
        q.push(after);
      }
    } else {
      B after;
      after.dup(pazz);
      swap(after.n[after.p], after.n[after.p - 4]);
      after.p -= 4;
      if (mem.find(after.encode()) == mem.end()) {
        mem[after.encode()] = mem[en] + 1;
        q.push(after);
      }
    }
    if (pazz.p != 0 && pazz.p != 4) {
      B after;
      after.dup(pazz);
      swap(after.n[after.p], after.n[after.p - 1]);
      after.p--;
      if (mem.find(after.encode()) == mem.end()) {
        mem[after.encode()] = mem[en] + 1;
        q.push(after);
      }
    }
    if (pazz.p != 3 && pazz.p != 7) {
      B after;
      after.dup(pazz);
      swap(after.n[after.p], after.n[after.p + 1]);
      after.p++;
      if (mem.find(after.encode()) == mem.end()) {
        mem[after.encode()] = mem[en] + 1;
        q.push(after);
      }
    }
  }
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  calc();
  while (true) {
    B b;
    REP(i, 8) {
      if (!(cin >> b.n[i]))
        return 0;
      if (b.n[i] == 0)
        b.p = i;
    }
    cout << mem[b.encode()] << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

// include
//------------------------------------------
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#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
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

// container util
//------------------------------------------
#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())

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

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

struct B {
  int n[8];
  int p;

  void dup(const B &rhs) {
    REP(i, 8) n[i] = rhs.n[i];
    p = rhs.p;
  }

  int encode() {
    int p = 1, res = 0;
    for (int i = 0; i < 8; ++i) {
      res += p * n[i];
      p *= 8;
    }
    return res;
  }

  bool check() {
    int i;
    for (i = 0; i < 8; ++i) {
      if (n[i] != i)
        break;
    }
    return i == 8;
  }
};

int calc(B &b) {
  queue<pair<B, int>> q;
  q.push(MP(b, 0));

  set<int> mem;
  while (!q.empty()) {
    B pazz = q.front().first;
    int t = q.front().second;
    q.pop();
    if (mem.find(pazz.encode()) != mem.end())
      continue;
    mem.insert(pazz.encode());
    if (pazz.check())
      return t;
    if (pazz.p < 4) {
      B after;
      after.dup(pazz);
      swap(after.n[after.p], after.n[4 + after.p]);
      after.p += 4;
      q.push(MP(after, t + 1));
    } else {
      B after;
      after.dup(pazz);
      swap(after.n[after.p], after.n[after.p - 4]);
      after.p -= 4;
      q.push(MP(after, t + 1));
    }
    if (pazz.p != 0 && pazz.p != 4) {
      B after;
      after.dup(pazz);
      swap(after.n[after.p], after.n[after.p - 1]);
      after.p--;
      q.push(MP(after, t + 1));
    }
    if (pazz.p != 3 && pazz.p != 7) {
      B after;
      after.dup(pazz);
      swap(after.n[after.p], after.n[after.p + 1]);
      after.p++;
      q.push(MP(after, t + 1));
    }
  }
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  while (!cin.eof()) {
    B b;
    REP(i, 8) {
      cin >> b.n[i];
      if (b.n[i] == 0)
        b.p = i;
    }
    cout << calc(b) << endl;
  }

  return 0;
}