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

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

using namespace std;

#define all(c) ((c).begin()), ((c).end())
#define dump(c) cerr << "> " << #c << " = " << (c) << endl;
#define iter(c) __typeof((c).begin())
#define tr(i, c) for (iter(c) i = (c).begin(); i != (c).end(); i++)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repi(i, a, b) for (int i = (int)(a); i <= (int)(b); i++)

#define mp make_pair
#define fst first
#define snd second
#define pb push_back

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

const int INF = 1 << 29;
const double EPS = 1e-10;

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  return os << '(' << p.first << ',' << p.second << ')';
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &a) {
  os << '[';
  rep(i, a.size()) os << (i ? " " : "") << a[i];
  return os << ']';
}

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

int hash(vi &b) {
  int res = 0;
  int e = 1;
  rep(i, 8) {
    res += b[i] * e;
    e *= 10;
  }
  return res;
}
int bits_of(int dat, int msb, int lsb) {
  int x = dat >> lsb;
  int m = 1 << msb - lsb + 1;
  return x & m - 1;
}

int swap(int n, int a, int b) {
  int x = bits_of(n, 2 + a * 3, a * 3);
  int y = bits_of(n, 2 + b * 3, b * 3);
  int _z = ~(n ^ (x << a * 3));
  int z = ~(_z ^ (y << b * 3));
  return z | x << b * 3 | y << a * 3;
}

int main() {
  queue<pii> q;
  q.push(mp(0, 01234567));
  map<int, int> visited;

  while (!q.empty()) {
    pii st = q.front();
    int b = st.snd;
    q.pop();

    if (visited[b] != 0)
      continue;
    visited[b] = st.fst + 1;

    int p = 0;
    int t = b;
    while (t != 0) {
      if (t == (t >> 3) << 3)
        break;
      t >>= 3;
      p++;
    }
    int d[] = {-1, 1, -4, 4};
    rep(i, 4) {
      if (p == 3 && i == 1)
        continue;
      if (p == 4 && i == 0)
        continue;
      int tp = p + d[i];
      if (tp < 0 || tp >= 8)
        continue;
      b = swap(b, p, tp);
      q.push(mp(st.fst + 1, b));
      b = swap(b, p, tp);
    }
  }

  for (string s; getline(cin, s);) {
    int board = 0;
    rep(i, 8) { board = board << 3 | s[i * 2] - '0'; }
    cout << visited[board] - 1 << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

#define all(c) ((c).begin()), ((c).end())
#define dump(c) cerr << "> " << #c << " = " << (c) << endl;
#define iter(c) __typeof((c).begin())
#define tr(i, c) for (iter(c) i = (c).begin(); i != (c).end(); i++)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repi(i, a, b) for (int i = (int)(a); i <= (int)(b); i++)

#define mp make_pair
#define fst first
#define snd second
#define pb push_back

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

const int INF = 1 << 29;
const double EPS = 1e-10;

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  return os << '(' << p.first << ',' << p.second << ')';
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &a) {
  os << '[';
  rep(i, a.size()) os << (i ? " " : "") << a[i];
  return os << ']';
}

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

int hash(vi &b) {
  int res = 0;
  int e = 1;
  rep(i, 8) {
    res += b[i] * e;
    e *= 10;
  }
  return res;
}
int bits_of(int dat, int msb, int lsb) {
  int x = dat >> lsb;
  int m = 1 << msb - lsb + 1;
  return x & m - 1;
}

int swap(int n, int a, int b) {
  int x = bits_of(n, 2 + a * 3, a * 3);
  int y = bits_of(n, 2 + b * 3, b * 3);
  int _z = ~(n ^ (x << a * 3));
  int z = ~(_z ^ (y << b * 3));
  return z | x << b * 3 | y << a * 3;
}

bool visited[17000000];

int main() {
  for (string s; getline(cin, s);) {
    int init = 0;
    int e = 1;
    rep(i, 8) { init = init << 3 | s[i * 2] - '0'; }
    queue<pii> q;
    q.push(mp(0, init));
    memset(visited, false, sizeof(visited));

    while (!q.empty()) {
      pii st = q.front();
      int b = st.snd;
      q.pop();

      if (visited[b])
        continue;
      visited[b] = true;

      if (b == 01234567) {
        cout << st.fst << endl;
        ;
        break;
      }

      int p = 0;
      int t = b;
      while (t != 0) {
        if (t == (t >> 3) << 3)
          break;
        t >>= 3;
        p++;
      }
      int d[] = {-1, 1, -4, 4};
      rep(i, 4) {
        if (p == 3 && i == 1)
          continue;
        if (p == 4 && i == 0)
          continue;
        int tp = p + d[i];
        if (tp < 0 || tp >= 8)
          continue;
        b = swap(b, p, tp);
        q.push(mp(st.fst + 1, b));
        b = swap(b, p, tp);
      }
    }
  }

  return 0;
}