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

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

vector<string> rotate(int n, const vector<string> &s) {
  if (n == 0)
    return s;

  vector<string> t(3, string(3, ' '));
  for (int i = 0; i < 3; ++i) {
    for (int j = 0; j < 3; ++j) {
      t[j][2 - i] = s[i][j];
    }
  }
  return rotate(n - 1, t);
}

class Dice {
public:
  vector<vector<string>> pip; // 東南西北上下の目
  Dice() { pip.assign(6, vector<string>(3, string(3, ' '))); }
  void roll(int d) { // 東南西北右左に回転
    int dir[][4] = {{4, 2, 5, 0}, {4, 3, 5, 1}, {4, 0, 5, 2},
                    {4, 1, 5, 3}, {0, 3, 2, 1}, {0, 1, 2, 3}};
    vector<string> tmp = pip[dir[d][0]];
    for (int i = 0; i < 3; ++i)
      pip[dir[d][i]] = pip[dir[d][i + 1]];
    pip[dir[d][3]] = tmp;

    if (d == 0) {
      pip[1] = rotate(3, pip[1]);
      pip[2] = rotate(2, pip[2]);
      pip[3] = rotate(1, pip[3]);
      pip[5] = rotate(2, pip[5]);
    } else if (d == 1) {
      pip[0] = rotate(1, pip[0]);
      pip[2] = rotate(3, pip[2]);
    } else if (d == 2) {
      pip[0] = rotate(2, pip[0]);
      pip[1] = rotate(1, pip[1]);
      pip[3] = rotate(3, pip[3]);
      pip[5] = rotate(2, pip[5]);
    } else {
      pip[0] = rotate(3, pip[0]);
      pip[2] = rotate(1, pip[2]);
    }
  }
  bool operator==(const Dice &d) const { return pip == d.pip; }
};

int solve(Dice d0) {
  set<vector<vector<string>>> s;
  queue<vector<vector<string>>> q;
  s.insert(d0.pip);
  q.push(d0.pip);

  int ret = 0;
  for (;;) {
    int n = q.size();
    while (--n >= 0) {
      Dice d;
      d.pip = q.front();
      q.pop();

      int tmp = 0;
      for (int i = 0; i < 3; ++i) {
        if (d.pip[1][2][i] == '*')
          tmp |= 1;
        if (d.pip[3][0][i] == '*')
          tmp |= 2;
      }
      if (tmp == 3)
        return ret;

      for (int i = 0; i < 4; ++i) {
        Dice d2 = d;
        d2.roll(i);
        if (s.find(d2.pip) == s.end()) {
          s.insert(d2.pip);
          q.push(d2.pip);
        }
      }
    }
    ++ret;
  }
}

int main() {
  int to[] = {1, 0, 3, 2, 4, 5};

  for (;;) {
    Dice d;
    for (int i = 0; i < 6; ++i) {
      for (int j = 0; j < 3; ++j) {
        cin >> d.pip[to[i]][j];
        if (d.pip[to[i]][j] == "#")
          return 0;
      }
    }
    d.pip[0] = rotate(3, d.pip[0]);
    d.pip[2] = rotate(1, d.pip[2]);
    d.pip[3] = rotate(2, d.pip[3]);

    cout << solve(d) << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

vector<string> rotate(int n, const vector<string> &s) {
  if (n == 0)
    return s;

  vector<string> t(3, string(3, ' '));
  for (int i = 0; i < 3; ++i) {
    for (int j = 0; j < 3; ++j) {
      t[j][2 - i] = s[i][j];
    }
  }
  return rotate(n - 1, t);
}

class Dice {
public:
  vector<vector<string>> pip; // 東南西北上下の目
  Dice() { pip.assign(6, vector<string>(3, string(3, ' '))); }
  void roll(int d) { // 東南西北右左に回転
    int dir[][4] = {{4, 2, 5, 0}, {4, 3, 5, 1}, {4, 0, 5, 2},
                    {4, 1, 5, 3}, {0, 3, 2, 1}, {0, 1, 2, 3}};
    vector<string> tmp = pip[dir[d][0]];
    for (int i = 0; i < 3; ++i)
      pip[dir[d][i]] = pip[dir[d][i + 1]];
    pip[dir[d][3]] = tmp;

    if (d == 0) {
      pip[1] = rotate(3, pip[1]);
      pip[2] = rotate(2, pip[2]);
      pip[3] = rotate(1, pip[3]);
      pip[5] = rotate(2, pip[5]);
    } else if (d == 1) {
      pip[0] = rotate(1, pip[0]);
      pip[2] = rotate(3, pip[2]);
    } else if (d == 2) {
      pip[0] = rotate(2, pip[0]);
      pip[1] = rotate(1, pip[1]);
      pip[3] = rotate(3, pip[3]);
      pip[5] = rotate(2, pip[5]);
    } else {
      pip[0] = rotate(3, pip[0]);
      pip[2] = rotate(1, pip[2]);
    }
  }
  bool operator==(const Dice &d) const { return pip == d.pip; }
};

int solve(Dice d0) {
  set<vector<vector<string>>> s;
  queue<vector<vector<string>>> q;
  s.insert(d0.pip);
  q.push(d0.pip);

  int ret = 0;
  for (;;) {
    int n = q.size();
    while (--n >= 0) {
      Dice d;
      d.pip = q.front();
      q.pop();

      int tmp = 0;
      for (int i = 0; i < 3; ++i) {
        if (d.pip[1][2][i] == '*')
          tmp |= 1;
        if (d.pip[3][0][i] == '*')
          tmp |= 2;
      }
      if (tmp == 3)
        return ret;

      for (int i = 0; i < 4; ++i) {
        Dice d2 = d;
        d2.roll(i);
        if (s.find(d2.pip) == s.end()) {
          s.insert(d2.pip);
          q.push(d2.pip);
        }
      }
    }
    ++ret;
  }
}

int main() {
  int to[] = {1, 0, 3, 2, 4, 5};

  for (;;) {
    Dice d;
    for (int i = 0; i < 6; ++i) {
      for (int j = 0; j < 3; ++j) {
        cin >> d.pip[to[i]][j];
        if (d.pip[to[i]][j] == "#")
          return 0;
      }
    }
    d.pip[0] = rotate(1, d.pip[0]);
    d.pip[2] = rotate(3, d.pip[2]);
    d.pip[3] = rotate(2, d.pip[3]);
    d.pip[5] = rotate(2, d.pip[5]);

    cout << solve(d) << endl;
  }
}