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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define REP(i, s, e) for (int i = (s); i < (int)(e); i++)
#define rep(i, n) REP(i, 0, n)
#define pb push_back
// #define mp make_pair
#define all(r) (r).begin(), (r).end()
#define rall(r) (r).rbegin(), (r).rend()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int INF = 1000000;
const double EPS = 1e-8;
const int mod = 1e9 + 7;

int vtoi(vi v) {
  int ret = 0, p = 1;
  for (int i = 0; i < v.size(); i++) {
    ret += v[i] * p;
    p *= 8;
  }
  return ret;
}

vi itov(int n) {
  vi ret(8, 0);
  int idx = 0;
  while (n > 0) {
    ret[idx] = n % 8;
    n /= 8;
    idx++;
  }
  return ret;
}

vector<int> get_nexts(vi v) {
  vi ret;
  for (int i = 0; i < v.size(); i++) {
    if (v[i] != 0)
      continue;
    if (i % 4 != 0) {
      swap(v[i], v[i - 1]);
      ret.pb(vtoi(v));
      swap(v[i], v[i - 1]);
    }
    if (i % 4 != 3) {
      swap(v[i], v[i + 1]);
      ret.pb(vtoi(v));
      swap(v[i], v[i + 1]);
    }
    if (i / 4 == 0) {
      swap(v[i], v[i + 4]);
      ret.pb(vtoi(v));
      swap(v[i], v[i + 4]);
    } else {
      swap(v[i], v[i - 4]);
      ret.pb(vtoi(v));
      swap(v[i], v[i - 4]);
    }
    return ret;
  }
  return ret;
}

map<int, int> mp;

void print(vi v) {
  for (int i = 0; i < v.size(); i++) {
    cout << " " << v[i];
  }
  cout << endl;
}

int main() {
  vi a(8);
  rep(i, 8) a[i] = i;
  queue<pii> q;
  q.push(pii(0, vtoi(a)));
  mp[vtoi(a)] = 0;
  pii p;
  int c, t;
  vi nexts;
  while (!q.empty()) {
    p = q.front();
    q.pop();
    a = itov(p.se);
    c = p.fi;
    nexts = get_nexts(a);
    for (int i = 0; i < nexts.size(); i++) {
      t = nexts[i];
      if (mp.find(t) != mp.end())
        continue;
      mp[t] = c + 1;
      q.push(pii(c + 1, t));
      // print(itov(t));
    }
  }
  // cout<<mp.size()<<endl;
  vi d(8);
  while (cin >> d[0]) {
    rep(i, 7) cin >> d[i + 1];
    cout << mp[vtoi(d)] << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define rep(i, n) REP(i, 0, n)
#define REP(i, s, e) for (int i = (s); i < (int)(e); i++)
#define pb push_back
#define mp make_pair
#define all(r) r.begin(), r.end()
#define fi first
#define se second
#define println(X) cout << X << endl;
#define DBG(X) cout << #X << " : " << X << endl;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int INF = 1e9;
const ll MOD = 1e9 + 7;
double EPS = 1e-7;

vi d(8, 0), t;
vi a(8, 0);

int b;

set<int> s;
pair<int, int> p;

int ans = 0;
int c;
int tt;

bool check(vi v) {
  for (int i = 0; i < 8; i++) {
    if (v[i] != i)
      return false;
  }
  return true;
}

int calc1(const vi &v) {
  int ret = 0, p = 1;
  for (int i = 0; i < v.size(); i++) {
    ret += p * v[i];
    p *= 8;
  }
  return ret;
}
vi calc2(int p) {
  vi ret(8, 0);
  int idx = 0;
  while (p > 0 && idx < 8) {
    ret[idx] = p % 8;
    p /= 8;
    idx++;
  }
  return ret;
}

int main() {
  rep(i, 8) a[i] = i;
  b = calc1(a);
  int cnt = 0;
  while (/*cin>>d[0]*/ scanf("%d", &d[0]) != EOF) {
    // rep(i, 7) cin>>d[i+1];
    rep(i, 7) { scanf("%d", &d[i + 1]); }
    s.clear();
    queue<pair<int, int>> q;
    q.push(mp(0, calc1(d)));
    s.insert(calc1(d));
    cnt = 0;
    while (!q.empty()) {
      // cout<<cnt++<<endl;
      cnt++;
      p = q.front();
      q.pop();
      c = p.fi;
      d = t = calc2(p.se);
      tt = calc1(t);
      // if(cnt == 0){
      // 	for(int i = 0; i < 8; i++){
      // 		cout<<" "<<t[i];
      // 	}
      // 	cout<<endl;
      // }
      if (/*check(t)*/ tt == b) {
        // cout<<c<<endl;
        printf("%d\\n", c);
        break;
      }
      for (int i = 0; i < 8; i++) {
        // cout<<" "<<i<<endl;
        if (d[i] == 0) {
          if (i % 4 != 0) {
            t = d;
            swap(t[i], t[i - 1]);
            tt = calc1(t);
            if (s.count(tt) == 0) {
              q.push(mp(c + 1, tt));
              s.insert(tt);
            }
          }
          if (i % 4 != 3) {
            t = d;
            swap(t[i], t[i + 1]);
            tt = calc1(t);
            if (s.count(tt) == 0) {
              q.push(mp(c + 1, tt));
              s.insert(tt);
            }
          }
          if (i / 4 == 0) {
            t = d;
            swap(t[i], t[i + 4]);
            tt = calc1(t);
            if (s.count(calc1(t)) == 0) {
              q.push(mp(c + 1, tt));
              s.insert(tt);
            }
          } else {
            t = d;
            swap(t[i], t[i - 4]);
            tt = calc1(t);
            if (s.count(tt) == 0) {
              q.push(mp(c + 1, tt));
              s.insert(tt);
            }
          }
          break;
        }
      }
    }
  }
  return 0;
}