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

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <string>
#include <vector>

#define X first
#define Y second
#define P first
#define S second
#define mp(x, y) make_pair(x, y)

using namespace std;
typedef ostringstream OSS;
typedef pair<int, int> PII;
typedef pair<PII, string> PIIS;

struct die_t {
  int top, front, right, left, back, bottom;
  die_t(int t, int f, int r, int l, int bk, int b)
      : top(t), front(f), right(r), left(l), back(bk), bottom(b) {}
  die_t(string dice) {
    top = dice[0] - '0';
    front = dice[1] - '0';
    right = dice[2] - '0';
    left = dice[3] - '0';
    back = dice[4] - '0';
    bottom = dice[5] - '0';
  }
  void rotate_swap(int &a, int &b, int &c, int &d) {
    swap(a, b);
    swap(b, c);
    swap(c, d);
  }
  void rotate_right(void) { rotate_swap(top, left, bottom, right); }
  void rotate_left(void) { rotate_swap(top, right, bottom, left); }
  void rotate_front(void) { rotate_swap(top, back, bottom, front); }
  void rotate_back(void) { rotate_swap(top, front, bottom, back); }
  void rotate_cw(void) { rotate_swap(back, left, front, right); }
  void rotate_cc(void) { rotate_swap(back, right, front, left); }
  bool rotate_set(int n, int t, int f, int r, int l, int bk, int b) {
    for (int i = 0; i < 6; ++i) {
      for (int j = 0; j < 4; ++j) {
        if (n == equal_num(t, f, r, l, bk, b)) {
          return true;
        }
        rotate_cw();
      }
      (i & 1 ? rotate_front() : rotate_right());
    }
    return false;
  }
  int equal_num(int t, int f, int r, int l, int bk, int b) {
    return (top == t) + (front == f) + (right == r) + (left == l) +
           (back == bk) + (bottom == b);
  }
  string toString(void) {
    OSS os;
    os << top << front << right << left << back << bottom;
    return os.str();
  }
};

const int INF = 1 << 29;
const int MAX_WH = 10;

int main(void) {
  vector<string> vs;
  die_t dd("123456");
  for (int i = 0; i < 6; ++i) {
    for (int j = 0; j < 4; ++j) {
      vs.push_back(dd.toString());
      dd.rotate_cw();
    }
    if (i & 1) {
      dd.rotate_front();
    } else {
      dd.rotate_right();
    }
  }

  int h, w, mat[MAX_WH][MAX_WH];

  while (cin >> h >> w, h | w) {
    map<string, int> M[MAX_WH][MAX_WH];
    for (int y = 0; y < h; ++y) {
      for (int x = 0; x < w; ++x) {
        cin >> mat[y][x];
        for (int i = 0, I = vs.size(); i < I; ++i) {
          M[y][x].insert(mp(vs[i], INF));
        }
      }
    }

    PII s, g;
    cin >> s.Y >> s.X;
    cin >> g.Y >> g.X;

    die_t dice("123456");
    queue<PIIS> Q;
    M[s.Y][s.X]["123456"] = 0;
    Q.push(mp(mp(s.X, s.Y), dice.toString()));

    while (!Q.empty()) {
      PIIS piis = Q.front();
      Q.pop();
      PII p = piis.P;
      string s = piis.S, s1, s2, s3, s4;
      die_t d1(s), d2(s), d3(s), d4(s);
      d1.rotate_right();
      s1 = d1.toString();
      d2.rotate_left();
      s2 = d2.toString();
      d3.rotate_front();
      s3 = d3.toString();
      d4.rotate_back();
      s4 = d4.toString();
      int now = M[p.Y][p.X][s];

      if (p.X + 1 < w &&
          M[p.Y][p.X + 1][s1] > now + mat[p.Y][p.X + 1] * d1.bottom) {
        M[p.Y][p.X + 1][s1] = now + mat[p.Y][p.X + 1] * d1.bottom;
        Q.push(mp(mp(p.X + 1, p.Y), s1));
      }
      if (p.X - 1 >= 0 &&
          M[p.Y][p.X - 1][s2] > now + mat[p.Y][p.X - 1] * d2.bottom) {
        M[p.Y][p.X - 1][s2] = now + mat[p.Y][p.X - 1] * d2.bottom;
        Q.push(mp(mp(p.X - 1, p.Y), s2));
      }
      if (p.Y + 1 < h &&
          M[p.Y + 1][p.X][s3] > now + mat[p.Y + 1][p.X] * d3.bottom) {
        M[p.Y + 1][p.X][s3] = now + mat[p.Y + 1][p.X] * d3.bottom;
        Q.push(mp(mp(p.X, p.Y + 1), s3));
      }
      if (p.Y - 1 >= 0 &&
          M[p.Y - 1][p.X][s4] > now + mat[p.Y - 1][p.X] * d4.bottom) {
        M[p.Y - 1][p.X][s4] = now + mat[p.Y - 1][p.X] * d4.bottom;
        Q.push(mp(mp(p.X, p.Y - 1), s4));
      }
    }

    int ans = INF;
    for (int i = 0, I = vs.size(); i < I; ++i) {
      ans = min(ans, M[g.Y][g.X][vs[i]]);
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <string>
#include <vector>

#define X first
#define Y second
#define P first
#define S second
#define mp(x, y) make_pair(x, y)

using namespace std;
typedef ostringstream OSS;
typedef pair<int, int> PII;
typedef pair<PII, string> PIIS;

struct die_t {
  int top, front, right, left, back, bottom;
  die_t(int t, int f, int r, int l, int bk, int b)
      : top(t), front(f), right(r), left(l), back(bk), bottom(b) {}
  die_t(string dice) {
    top = dice[0] - '0';
    front = dice[1] - '0';
    right = dice[2] - '0';
    left = dice[3] - '0';
    back = dice[4] - '0';
    bottom = dice[5] - '0';
  }
  void rotate_swap(int &a, int &b, int &c, int &d) {
    swap(a, b);
    swap(b, c);
    swap(c, d);
  }
  void rotate_right(void) { rotate_swap(top, left, bottom, right); }
  void rotate_left(void) { rotate_swap(top, right, bottom, left); }
  void rotate_front(void) { rotate_swap(top, back, bottom, front); }
  void rotate_back(void) { rotate_swap(top, front, bottom, back); }
  void rotate_cw(void) { rotate_swap(back, left, front, right); }
  void rotate_cc(void) { rotate_swap(back, right, front, left); }
  bool rotate_set(int n, int t, int f, int r, int l, int bk, int b) {
    for (int i = 0; i < 6; ++i) {
      for (int j = 0; j < 4; ++j) {
        if (n == equal_num(t, f, r, l, bk, b)) {
          return true;
        }
        rotate_cw();
      }
      (i & 1 ? rotate_front() : rotate_right());
    }
    return false;
  }
  int equal_num(int t, int f, int r, int l, int bk, int b) {
    return (top == t) + (front == f) + (right == r) + (left == l) +
           (back == bk) + (bottom == b);
  }
  string toString(void) {
    OSS os;
    os << top << front << right << left << back << bottom;
    return os.str();
  }
};

const int INF = 1 << 29;
const int MAX_WH = 10;

int main(void) {
  vector<string> vs;
  die_t dd("123456");
  for (int i = 0; i < 6; ++i) {
    for (int j = 0; j < 4; ++j) {
      vs.push_back(dd.toString());
      dd.rotate_cw();
    }
    if (i & 1) {
      dd.rotate_front();
    } else {
      dd.rotate_right();
    }
  }

  int h, w, mat[MAX_WH][MAX_WH];
  map<string, int> M[MAX_WH][MAX_WH];

  while (cin >> h >> w, h | w) {
    for (int y = 0; y < h; ++y) {
      for (int x = 0; x < w; ++x) {
        cin >> mat[y][x];
        M[y][x].clear();
        for (int i = 0, I = vs.size(); i < I; ++i) {
          M[y][x].insert(mp(vs[i], INF));
        }
      }
    }

    PII s, g;
    cin >> s.Y >> s.X;
    cin >> g.Y >> g.X;

    die_t dice("123456");
    queue<PIIS> Q;
    M[s.Y][s.X]["123456"] = 0;
    Q.push(mp(mp(s.X, s.Y), dice.toString()));

    while (!Q.empty()) {
      PIIS piis = Q.front();
      Q.pop();
      PII p = piis.P;
      string s = piis.S, s1, s2, s3, s4;
      die_t d1(s), d2(s), d3(s), d4(s);
      d1.rotate_right();
      s1 = d1.toString();
      d2.rotate_left();
      s2 = d2.toString();
      d3.rotate_front();
      s3 = d3.toString();
      d4.rotate_back();
      s4 = d4.toString();

      if (p.X + 1 < w && M[p.Y][p.X + 1][s1] >
                             M[p.Y][p.X][s] + mat[p.Y][p.X + 1] * d1.bottom) {
        M[p.Y][p.X + 1][s1] = M[p.Y][p.X][s] + mat[p.Y][p.X + 1] * d1.bottom;
        Q.push(mp(mp(p.X + 1, p.Y), d1.toString()));
      }
      if (p.X - 1 >= 0 && M[p.Y][p.X - 1][s2] >
                              M[p.Y][p.X][s] + mat[p.Y][p.X - 1] * d2.bottom) {
        M[p.Y][p.X - 1][s2] = M[p.Y][p.X][s] + mat[p.Y][p.X - 1] * d2.bottom;
        Q.push(mp(mp(p.X - 1, p.Y), d2.toString()));
      }
      if (p.Y + 1 < h && M[p.Y + 1][p.X][s3] >
                             M[p.Y][p.X][s] + mat[p.Y + 1][p.X] * d3.bottom) {
        M[p.Y + 1][p.X][s3] = M[p.Y][p.X][s] + mat[p.Y + 1][p.X] * d3.bottom;
        Q.push(mp(mp(p.X, p.Y + 1), d3.toString()));
      }
      if (p.Y - 1 >= 0 && M[p.Y - 1][p.X][s4] >
                              M[p.Y][p.X][s] + mat[p.Y - 1][p.X] * d4.bottom) {
        M[p.Y - 1][p.X][s4] = M[p.Y][p.X][s] + mat[p.Y - 1][p.X] * d4.bottom;
        Q.push(mp(mp(p.X, p.Y - 1), d4.toString()));
      }
    }

    int ans = INF;
    for (int i = 0, I = vs.size(); i < I; ++i) {
      ans = min(ans, M[g.Y][g.X][vs[i]]);
    }
    cout << ans << endl;
  }
  return 0;
}