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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define rep(i, n) FOR(i, 0, n)
#define DEBUG(x) cout << #x << ": " << x << endl
#define vint vector<int>
#define vdouble vector<double>
#define vstring vector<string>
using namespace std;

#include <map>
#include <queue>
#include <set>
#include <sstream>

typedef long long ll;
typedef unsigned long long ull;

const int MAX_N = 1000000;
const int INFTY = (1 << 21); // 2097152
// const ll INFTY = (1LL << 60);
const ll MD = 1000000007LL;

struct Board {
  vector<vint> v;
  ll _hash;

  Board(vector<vint> v) {
    this->v = v;
    _hash = hash();
  }

  vector<Board> transition() {
    vector<Board> ret;

    vint dx{1, 0, -1, 0};
    vint dy{0, 1, 0, -1};

    int x0 = 0, y0 = 0;
    rep(y, 3) rep(x, 3) {
      if (v[y][x] == 0) {
        x0 = x;
        y0 = y;
      }
    }

    rep(d, 4) {
      int xn = x0 + dx[d];
      int yn = y0 + dy[d];
      if (xn >= 0 && xn < 3 && yn >= 0 && yn < 3) {
        vector<vint> vv = v;
        vv[yn][xn] = v[y0][x0];
        vv[y0][x0] = v[yn][xn];
        ret.push_back(Board(vv));
      }
    }

    return ret;
  }

  ll hash() const {
    ll ret = 0;
    ll m = 1;
    rep(y, 3) rep(x, 3) {
      ret += v[y][x] * m;
      m *= 10;
    }
    return ret;
  }

  bool operator<(const Board &p) const {
    ll h1 = _hash;
    ll h2 = p._hash;
    return h1 < h2;
  }

  bool operator==(const Board &p) const {
    ll h1 = _hash;
    ll h2 = p._hash;
    return h1 == h2;
  }
};

int main() {
  vector<vint> v0;
  rep(y, 3) {
    vint v;
    rep(x, 3) {
      int n;
      cin >> n;
      v.push_back(n);
    }
    v0.push_back(v);
  }
  Board b0 = Board(v0);

  vector<vint> ve;
  vint ve1{1, 2, 3};
  ve.push_back(ve1);
  vint ve2{4, 5, 6};
  ve.push_back(ve2);
  vint ve3{7, 8, 0};
  ve.push_back(ve3);
  Board be = Board(ve);

  set<Board> S;
  queue<pair<Board, int>> q;

  q.push(make_pair(b0, 0));
  // fprintf(stderr, "%09lld\\n", b0.hash());
  // fprintf(stderr, "%09lld\\n", be.hash());

  while (true) {
    auto p = q.front();
    q.pop();
    Board b = p.first;
    int t = p.second;

    S.find(b);
    S.end();
    if (S.find(b) != S.end())
      continue;
    S.insert(b);
    // fprintf(stderr, "%09lld %d\\n", b.hash(), t);

    if (b == be) {
      cout << t << endl;
      return 0;
    }

    vector<Board> trans = b.transition();
    for (Board bb : trans) {
      q.push(make_pair(bb, t + 1));
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define rep(i, n) FOR(i, 0, n)
#define DEBUG(x) cout << #x << ": " << x << endl
#define vint vector<int>
#define vdouble vector<double>
#define vstring vector<string>
using namespace std;

#include <map>
#include <queue>
#include <set>
#include <sstream>

typedef long long ll;
typedef unsigned long long ull;

const int MAX_N = 1000000;
const int INFTY = (1 << 21); // 2097152
// const ll INFTY = (1LL << 60);
const ll MD = 1000000007LL;

struct Board {
  vector<vint> v;

  Board(vector<vint> v) { this->v = v; }

  vector<Board> transition() {
    vector<Board> ret;

    vint dx{1, 0, -1, 0};
    vint dy{0, 1, 0, -1};

    int x0 = 0, y0 = 0;
    rep(y, 3) rep(x, 3) {
      if (v[y][x] == 0) {
        x0 = x;
        y0 = y;
      }
    }

    rep(d, 4) {
      int xn = x0 + dx[d];
      int yn = y0 + dy[d];
      if (xn >= 0 && xn < 3 && yn >= 0 && yn < 3) {
        vector<vint> vv = v;
        vv[yn][xn] = v[y0][x0];
        vv[y0][x0] = v[yn][xn];
        ret.push_back(Board(vv));
      }
    }

    return ret;
  }

  ll hash() const {
    ll ret = 0;
    ll m = 1;
    rep(y, 3) rep(x, 3) {
      ret += v[y][x] * m;
      m *= 10;
    }
    return ret;
  }

  bool operator<(const Board &p) const {
    ll h1 = hash();
    ll h2 = p.hash();
    return h1 < h2;
  }

  bool operator==(const Board &p) const {
    ll h1 = hash();
    ll h2 = p.hash();
    return h1 == h2;
  }
};

int main() {
  vector<vint> v0;
  rep(y, 3) {
    vint v;
    rep(x, 3) {
      int n;
      cin >> n;
      v.push_back(n);
    }
    v0.push_back(v);
  }
  Board b0 = Board(v0);

  vector<vint> ve;
  vint ve1{1, 2, 3};
  ve.push_back(ve1);
  vint ve2{4, 5, 6};
  ve.push_back(ve2);
  vint ve3{7, 8, 0};
  ve.push_back(ve3);
  Board be = Board(ve);

  set<Board> S;
  queue<pair<Board, int>> q;

  q.push(make_pair(b0, 0));
  // fprintf(stderr, "%09lld\\n", b0.hash());
  // fprintf(stderr, "%09lld\\n", be.hash());

  while (true) {
    auto p = q.front();
    q.pop();
    Board b = p.first;
    int t = p.second;

    S.find(b);
    S.end();
    if (S.find(b) != S.end())
      continue;
    S.insert(b);
    // fprintf(stderr, "%09lld %d\\n", b.hash(), t);

    if (b == be) {
      cout << t << endl;
      return 0;
    }

    vector<Board> trans = b.transition();
    for (Board bb : trans) {
      q.push(make_pair(bb, t + 1));
    }
  }
}