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

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#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;

class Data {
public:
  int x, y, b, d;
  Data(int x0, int y0, int b0, int d0) {
    x = x0;
    y = y0;
    b = b0; // êÊÌÚ
    d = d0; // ü«
  }
};

int main() {
  int dy[] = {1, 0, 0};
  int dx[] = {0, 1, -1};
  int db[][3] = {{5, 1, -1}, {-5, 5, 6}, {-6, 5, -5}, {-5, -1, 1}};

  queue<Data> q1, q2;
  q1.push(Data(0, 0, 0, 0));
  vector<vector<vector<vector<int>>>> minCost(
      203, vector<vector<vector<int>>>(
               203, vector<vector<int>>(20, vector<int>(3, -1))));
  minCost[101][101][0][0] = 0;

  int cost = 0;
  for (;;) {
    if (q1.empty()) {
      q1 = q2;
      q2 = queue<Data>();
      ++cost;
      if (cost == 100)
        break;
    }
    int x0 = q1.front().x;
    int y0 = q1.front().y;
    int b0 = q1.front().b;
    int d0 = q1.front().d;
    q1.pop();

    for (int i = 0; i < 3; ++i) {
      int x = x0 + dx[i] * ((x0 + y0) % 2 ? -1 : 1);
      int y = y0 + dy[i] * ((x0 + y0) % 2 ? -1 : 1);
      int tmp = db[b0 / 5][(d0 + i) % 3];
      int b = (b0 / 5 + tmp / 5) * 5 + (b0 % 5 + tmp % 5 + 5) % 5;
      int d = (find(db[b / 5], db[b / 5] + 3, -tmp) - db[b / 5] - i + 3) % 3;

      if (minCost[x + 101][y + 101][b][d] == -1) {
        minCost[x + 101][y + 101][b][d] = cost + 1;
        q2.push(Data(x, y, b, d));
      }
    }
  }

  for (;;) {
    int x, y, n;
    cin >> x >> y >> n;
    if (x == 0 && y == 0 && n == 0)
      return 0;

    int ret = INT_MAX;
    for (int i = 0; i < 3; ++i) {
      if (minCost[x + 101][y + 101][n][i] != -1)
        ret = min(ret, minCost[x + 101][y + 101][n][i]);
    }
    cout << ret << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#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;

class Data {
public:
  int x, y, b, d;
  Data(int x0, int y0, int b0, int d0) {
    x = x0;
    y = y0;
    b = b0; // êÊÌÚ
    d = d0; // ü«
  }
};

int main() {
  int dy[] = {1, 0, 0};
  int dx[] = {0, 1, -1};
  int db[][3] = {{5, 1, -1}, {-5, 5, 6}, {-6, 5, -5}, {-5, -1, 1}};

  for (;;) {
    int gx, gy, n;
    cin >> gx >> gy >> n;
    if (gx == 0 && gy == 0 && n == 0)
      return 0;

    queue<Data> q1, q2;
    q1.push(Data(0, 0, 0, 0));
    vector<vector<vector<vector<bool>>>> check(
        203, vector<vector<vector<bool>>>(
                 203, vector<vector<bool>>(20, vector<bool>(3, false))));
    check[101][101][0][0] = true;

    int ret = 0;
    for (;;) {
      if (q1.empty()) {
        q1 = q2;
        q2 = queue<Data>();
        ++ret;
      }
      int x0 = q1.front().x;
      int y0 = q1.front().y;
      int b0 = q1.front().b;
      int d0 = q1.front().d;
      q1.pop();

      if (x0 == gx && y0 == gy && b0 == n) {
        cout << ret << endl;
        break;
      }

      for (int i = 0; i < 3; ++i) {
        int x = x0 + dx[i] * ((x0 + y0) % 2 ? -1 : 1);
        int y = y0 + dy[i] * ((x0 + y0) % 2 ? -1 : 1);
        int tmp = db[b0 / 5][(d0 + i) % 3];
        int b = (b0 / 5 + tmp / 5) * 5 + (b0 % 5 + tmp % 5 + 5) % 5;
        int d = (find(db[b / 5], db[b / 5] + 3, -tmp) - db[b / 5] - i + 3) % 3;

        if (!check[x + 101][y + 101][b][d]) {
          check[x + 101][y + 101][b][d] = true;
          q2.push(Data(x, y, b, d));
        }
      }
    }
  }
}