Problem p00643 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define dump(...)                                                              \
  cout << 'L' << __LINE__ << ": " << #__VA_ARGS__ << '=' << (__VA_ARGS__)      \
       << endl
template <typename Tuple> void print_tuple(ostream &, const Tuple &) {}
template <typename Car, typename... Cdr, typename Tuple>
void print_tuple(ostream &os, const Tuple &t) {
  print_tuple<Cdr...>(os, t);
  os << (sizeof...(Cdr) ? "," : "") << get<sizeof...(Cdr)>(t);
}
template <typename... Args>
ostream &operator<<(ostream &os, const tuple<Args...> &t) {
  os << '(';
  print_tuple<Args...>(os, t);
  return os << ')';
}

struct Die {
  array<int, 6> faces;
  Die() {
    faces[0] = 2;
    faces[1] = 5;
    faces[2] = 3;
    faces[3] = 4;
    faces[4] = 1;
    faces[5] = 6;
  }
  void Rotate(int a, int b, int c, int d) {
    swap(faces[a], faces[b]);
    swap(faces[b], faces[c]);
    swap(faces[c], faces[d]);
  }
  void TurnU() { Rotate(4, 0, 5, 1); }
  void TurnD() { Rotate(4, 1, 5, 0); }
  void TurnL() { Rotate(4, 2, 5, 3); }
  void TurnR() { Rotate(4, 3, 5, 2); }
};
bool operator<(const Die &a, const Die &b) {
  return lexicographical_compare(begin(a.faces), end(a.faces), begin(b.faces),
                                 end(b.faces));
}
bool operator>(const Die &a, const Die &b) {
  return lexicographical_compare(begin(b.faces), end(b.faces), begin(a.faces),
                                 end(a.faces));
}

template <typename Functor> struct functor_traits {
  template <typename C, typename Ret, typename Arg, typename... Args>
  static Arg helper(Ret (C::*)(Arg, Args...));
  template <typename C, typename Ret, typename Arg, typename... Args>
  static Arg helper(Ret (C::*)(Arg, Args...) const);
  typedef decltype(helper(&Functor::operator())) first_argument_type;
};
template <typename Compare,
          typename T = typename functor_traits<Compare>::first_argument_type>
priority_queue<T, vector<T>, Compare> make_priority_queue(Compare comp) {
  return priority_queue<T, vector<T>, Compare>(move(comp));
}

int main() {
  for (int h, w; cin >> h >> w && h | w;) {
    vector<vector<int>> grid(h, vector<int>(w));
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++)
        cin >> grid[i][j];
    int si, sj, gi, gj;
    cin >> si >> sj >> gi >> gj;

    auto pq = make_priority_queue(
        [](tuple<Die, int, int, int> a, tuple<Die, int, int, int> b) {
          return get<3>(a) > get<3>(b);
        });
    pq.emplace(Die(), si, sj, 0);
    set<tuple<Die, int, int>> vis;
    while (pq.size()) {
      Die d;
      int i, j, c;
      tie(d, i, j, c) = pq.top();
      pq.pop();
      if (vis.count(make_tuple(d, i, j)))
        continue;
      if (i == gi && j == gj) {
        cout << c << endl;
        break;
      }
      function<void(Die &)> turns[] = {&Die::TurnU, &Die::TurnD, &Die::TurnL,
                                       &Die::TurnR};
      for (int k = 0; k < 4; k++) {
        int ni = i + "\xff\x1\0\0"[k], nj = j + "\0\0\xff\x1"[k];
        if (0 <= ni && ni < h && 0 <= nj && nj < w) {
          Die t = d;
          turns[k](t);
          pq.emplace(t, ni, nj, c + t.faces[5] * grid[ni][nj]);
        }
      }
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define dump(...)                                                              \
  cout << 'L' << __LINE__ << ": " << #__VA_ARGS__ << '=' << (__VA_ARGS__)      \
       << endl
template <typename Tuple> void print_tuple(ostream &, const Tuple &) {}
template <typename Car, typename... Cdr, typename Tuple>
void print_tuple(ostream &os, const Tuple &t) {
  print_tuple<Cdr...>(os, t);
  os << (sizeof...(Cdr) ? "," : "") << get<sizeof...(Cdr)>(t);
}
template <typename... Args>
ostream &operator<<(ostream &os, const tuple<Args...> &t) {
  os << '(';
  print_tuple<Args...>(os, t);
  return os << ')';
}

struct Die {
  array<int, 6> faces;
  Die() {
    faces[0] = 2;
    faces[1] = 5;
    faces[2] = 3;
    faces[3] = 4;
    faces[4] = 1;
    faces[5] = 6;
  }
  void Rotate(int a, int b, int c, int d) {
    swap(faces[a], faces[b]);
    swap(faces[b], faces[c]);
    swap(faces[c], faces[d]);
  }
  void TurnU() { Rotate(4, 0, 5, 1); }
  void TurnD() { Rotate(4, 1, 5, 0); }
  void TurnL() { Rotate(4, 2, 5, 3); }
  void TurnR() { Rotate(4, 3, 5, 2); }
};
bool operator<(const Die &a, const Die &b) {
  return lexicographical_compare(begin(a.faces), end(a.faces), begin(b.faces),
                                 end(b.faces));
}
bool operator>(const Die &a, const Die &b) {
  return lexicographical_compare(begin(b.faces), end(b.faces), begin(a.faces),
                                 end(a.faces));
}

template <typename Functor> struct functor_traits {
  template <typename C, typename Ret, typename Arg, typename... Args>
  static Arg helper(Ret (C::*)(Arg, Args...));
  template <typename C, typename Ret, typename Arg, typename... Args>
  static Arg helper(Ret (C::*)(Arg, Args...) const);
  typedef decltype(helper(&Functor::operator())) first_argument_type;
};
template <typename Compare,
          typename T = typename functor_traits<Compare>::first_argument_type>
priority_queue<T, vector<T>, Compare> make_priority_queue(Compare comp) {
  return priority_queue<T, vector<T>, Compare>(move(comp));
}

int main() {
  for (int h, w; cin >> h >> w && h | w;) {
    vector<vector<int>> grid(h, vector<int>(w));
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++)
        cin >> grid[i][j];
    int si, sj, gi, gj;
    cin >> si >> sj >> gi >> gj;

    auto pq = make_priority_queue(
        [](tuple<Die, int, int, int> a, tuple<Die, int, int, int> b) {
          return get<3>(a) > get<3>(b);
        });
    pq.emplace(Die(), si, sj, 0);
    set<tuple<Die, int, int>> vis;
    while (pq.size()) {
      Die d;
      int i, j, c;
      tie(d, i, j, c) = pq.top();
      pq.pop();
      if (vis.count(make_tuple(d, i, j)))
        continue;
      vis.insert(make_tuple(d, i, j));
      if (i == gi && j == gj) {
        cout << c << endl;
        break;
      }
      function<void(Die &)> turns[] = {&Die::TurnU, &Die::TurnD, &Die::TurnL,
                                       &Die::TurnR};
      for (int k = 0; k < 4; k++) {
        int ni = i + "\xff\x1\0\0"[k], nj = j + "\0\0\xff\x1"[k];
        if (0 <= ni && ni < h && 0 <= nj && nj < w) {
          Die t = d;
          turns[k](t);
          pq.emplace(t, ni, nj, c + t.faces[5] * grid[ni][nj]);
        }
      }
    }
  }
}