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

#include <iostream>
#include <map>
#include <queue>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
int dx[] = {-1, 1, 0, 0}, dy[] = {0, 0, -1, 1};
class DICE {
public:
  vector<int> dice;
  DICE() {
    dice.resize(6);
    rep(i, 6) dice[i] = i + 1;
  }
  // top/front/right/left/back/bottom
  void back() {
    int to[] = {4, 0, 2, 3, 5, 1};
    vector<int> tmp(6);
    rep(i, 6) tmp[to[i]] = dice[i];
    dice = tmp;
  }
  void left() {
    int to[] = {3, 1, 0, 5, 4, 2};
    vector<int> tmp(6);
    rep(i, 6) tmp[to[i]] = dice[i];
    dice = tmp;
  }
  void rotate() {
    int to[] = {0, 3, 1, 4, 2, 5};
    vector<int> tmp(6);
    rep(i, 6) tmp[to[i]] = dice[i];
    dice = tmp;
  }
  void select(int n) {
    if (n == 0)
      left();
    else if (n == 1)
      left(), left(), left();
    else if (n == 2)
      back();
    else if (n == 3)
      back(), back(), back();
  }
};
struct NODE {
  int cost, x, y;
  DICE dice;
  NODE(int a, int b, int c, DICE d) { cost = a, x = b, y = c, dice = d; }
};
bool operator<(const NODE &a, const NODE &b) {
  if (a.x != b.x)
    return a.x < b.x;
  if (a.y != b.y)
    return a.y < b.y;
  if (a.dice.dice != b.dice.dice)
    return a.dice.dice < b.dice.dice;
  return false;
}
bool operator>(const NODE &a, const NODE &b) { return a.cost > b.cost; }
int main() {
  int W, H;
  int data[10][10];
  while (cin >> H >> W, H) {
    map<NODE, bool> done;
    priority_queue<NODE, vector<NODE>, greater<NODE>> Q;
    rep(i, H) rep(j, W) cin >> data[i][j];
    int sx, sy, gx, gy;
    cin >> sy >> sx >> gy >> gx;
    Q.push(NODE(0, sx, sy, DICE()));
    while (Q.size()) {
      NODE q = Q.top();
      Q.pop();
      if (done[q])
        continue;
      else
        done[q] = true;
      if (q.x == gx && q.y == gy) {
        cout << q.cost << endl;
        break;
      }
      rep(i, 4) {
        NODE next = q;
        next.x = dx[i] + q.x;
        next.y = dy[i] + q.y;
        next.dice.select(i);
        if (next.x < 0 || next.y < 0 || next.y >= H || next.x >= W)
          continue;
        next.cost = q.cost + next.dice.dice[5] * data[next.y][next.x];
        Q.push(next);
      }
    }
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <map>
#include <queue>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
int dx[] = {-1, 1, 0, 0}, dy[] = {0, 0, -1, 1};
class DICE {
public:
  vector<int> dice;
  DICE() {
    dice.resize(6);
    rep(i, 6) dice[i] = i + 1;
  }
  // top/front/right/left/back/bottom
  void back() {
    int to[] = {4, 0, 2, 3, 5, 1};
    vector<int> tmp(6);
    rep(i, 6) tmp[to[i]] = dice[i];
    dice = tmp;
  }
  void left() {
    int to[] = {3, 1, 0, 5, 4, 2};
    vector<int> tmp(6);
    rep(i, 6) tmp[to[i]] = dice[i];
    dice = tmp;
  }
  void rotate() {
    int to[] = {0, 3, 1, 4, 2, 5};
    vector<int> tmp(6);
    rep(i, 6) tmp[to[i]] = dice[i];
    dice = tmp;
  }
  void select(int n) {
    if (n == 0)
      left();
    else if (n == 1)
      left(), left(), left();
    else if (n == 2)
      back();
    else if (n == 3)
      back(), back(), back();
  }
};
struct NODE {
  int cost, x, y;
  DICE dice;
  NODE(int a, int b, int c, DICE d) { cost = a, x = b, y = c, dice = d; }
};
bool operator<(const NODE &a, const NODE &b) {
  if (a.cost != b.cost)
    return a.cost > b.cost;
  if (a.x != b.x)
    return a.x < b.x;
  if (a.y != b.y)
    return a.y < b.y;
  if (a.dice.dice != b.dice.dice)
    return a.dice.dice < b.dice.dice;
  return false;
}
int main() {
  int W, H;
  int data[10][10];

  while (cin >> H >> W, H) {
    map<NODE, bool> done;
    priority_queue<NODE> Q;
    rep(i, H) rep(j, W) cin >> data[i][j];
    int sx, sy, gx, gy;
    cin >> sy >> sx >> gy >> gx;
    Q.push(NODE(0, sx, sy, DICE()));
    while (Q.size()) {
      NODE q = Q.top();
      Q.pop();
      if (done[q])
        continue;
      else
        done[q] = true;
      if (q.x == gx && q.y == gy) {
        cout << q.cost << endl;
        break;
      }
      rep(i, 4) {
        NODE next = q;
        next.x = dx[i] + q.x;
        next.y = dy[i] + q.y;
        next.dice.select(i);
        if (next.x < 0 || next.y < 0 || next.y >= H || next.x >= W)
          continue;
        next.cost = q.cost + next.dice.dice[5] * data[next.y][next.x];
        Q.push(next);
      }
    }
  }
}