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

#include <bits/stdc++.h>

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(x) (x).begin(), (x).end()
const int INF = 1e9;

using namespace std;
const int answer[9] = {1, 2, 3, 4, 5, 6, 7, 8, 0};
const bool dir[9][4] = {{false, false, true, true}, {true, false, true, true},
                        {true, false, true, false}, {false, true, true, true},
                        {true, true, true, true},   {true, true, true, false},
                        {false, true, false, true}, {true, true, false, true},
                        {true, true, false, false}}; /* left, up, down, right */
vector<int> original(9, 0);

class board;

priority_queue<board> pq;
vector<int> searched[9][9];

class board {
public:
  int moves;
  int wrongs;
  int zero_place;
  int cost;
  vector<int> tiles;

  explicit board(vector<int> num, int move) {
    wrongs = 0;
    tiles = num;
    REP(i, 9) {
      if (num[i] == 0)
        zero_place = i;
      if (num[i] != answer[i])
        wrongs++;
    }
    moves = move;
    cost = wrongs + moves;
  }

  void board_to_int() {
    int k = 0;
    REP(i, 7)
    k += (tiles[i + 2] * pow(10, i));
    for (int i = searched[tiles[0]][tiles[1]].size(); i > 0; i--)
      if (k == searched[tiles[0]][tiles[1]][i - 1])
        return;
    pq.push(board(tiles, moves + 1));
    searched[tiles[0]][tiles[1]].push_back(k);
    return;
  }

  void move() {
    if (dir[zero_place][0]) {
      swap(tiles[zero_place], tiles[zero_place - 1]);
      board_to_int();
      swap(tiles[zero_place], tiles[zero_place - 1]);
    }
    if (dir[zero_place][1]) {
      swap(tiles[zero_place], tiles[zero_place - 3]);
      board_to_int();
      swap(tiles[zero_place], tiles[zero_place - 3]);
    }
    if (dir[zero_place][2]) {
      swap(tiles[zero_place], tiles[zero_place + 3]);
      board_to_int();
      swap(tiles[zero_place], tiles[zero_place + 3]);
    }
    if (dir[zero_place][3]) {
      swap(tiles[zero_place], tiles[zero_place + 1]);
      board_to_int();
      swap(tiles[zero_place], tiles[zero_place + 1]);
    }
  }
};

bool operator<(board b1, board b2) {
  return ((b1.cost > b2.cost) ? true : false);
}

int solve() {
  pq.push(board(original, 0));
  while (1) {
    auto target = pq.top();
    if (target.wrongs == 0)
      return target.cost;
    pq.pop();
    target.move();
  }
}

int main() {
  REP(i, 9) cin >> original[i];
  int move = solve();
  cout << move << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(x) (x).begin(), (x).end()
const int INF = 1e9;

using namespace std;
const int answer[9] = {1, 2, 3, 4, 5, 6, 7, 8, 0};
const bool dir[9][4] = {{false, false, true, true}, {true, false, true, true},
                        {true, false, true, false}, {false, true, true, true},
                        {true, true, true, true},   {true, true, true, false},
                        {false, true, false, true}, {true, true, false, true},
                        {true, true, false, false}}; /* left, up, down, right */
vector<int> original(9, 0);

class board;

priority_queue<board> pq;
vector<int> searched[9];

class board {
public:
  int moves;
  int wrongs;
  int zero_place;
  int cost;
  vector<int> tiles;

  explicit board(vector<int> num, int move) {
    wrongs = 0;
    tiles = num;
    REP(i, 9) {
      if (num[i] == 0)
        zero_place = i;
      if (num[i] != answer[i])
        wrongs++;
    }
    moves = move;
    cost = wrongs + moves;
  }

  void board_to_int() {
    int k = 0;
    REP(i, 9)
    k += (tiles[i] * pow(10, i));
    for (int i = searched[tiles[0]].size(); i > 0; i--)
      if (k == searched[tiles[0]][i - 1])
        return;
    pq.push(board(tiles, moves + 1));
    searched[tiles[0]].push_back(k);
    return;
  }

  void move() {
    if (dir[zero_place][0]) {
      swap(tiles[zero_place], tiles[zero_place - 1]);
      board_to_int();
      swap(tiles[zero_place], tiles[zero_place - 1]);
    }
    if (dir[zero_place][1]) {
      swap(tiles[zero_place], tiles[zero_place - 3]);
      board_to_int();
      swap(tiles[zero_place], tiles[zero_place - 3]);
    }
    if (dir[zero_place][2]) {
      swap(tiles[zero_place], tiles[zero_place + 3]);
      board_to_int();
      swap(tiles[zero_place], tiles[zero_place + 3]);
    }
    if (dir[zero_place][3]) {
      swap(tiles[zero_place], tiles[zero_place + 1]);
      board_to_int();
      swap(tiles[zero_place], tiles[zero_place + 1]);
    }
  }
};

bool operator<(board b1, board b2) {
  return ((b1.cost > b2.cost) ? true : false);
}

int solve() {
  pq.push(board(original, 0));
  while (1) {
    auto target = pq.top();
    pq.pop();
    if (target.wrongs == 0)
      return target.cost;
    target.move();
  }
}

int main() {
  REP(i, 9) scanf("%d", &original[i]);
  int move = solve();
  printf("%d\\n", move);
  return 0;
}