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

#include <array>
#include <cstdlib>
#include <iostream>
#include <set>

using namespace std;

typedef array<array<signed char, 3>, 3> Die;

int main() {
  for (int x, y; cin >> x >> y, x;) {
    char g[3][3];
    bool nz = false;
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 3; j++) {
        cin >> g[i][j];
        if (g[i][j] == 'E') {
          nz |= i != y - 1 || j != x - 1;
        } else {
          nz |= g[i][j] != 'W';
        }
      }
    }
    if (!nz) {
      cout << 0 << endl;
    } else {
      set<Die> s[32];
      Die d{};
      d[y - 1][x - 1] = -1;
      s[0].insert(d);
      for (int i = 0; i <= 30; i++) {
        //	cout<<"c = "<<s[i].size()<<endl;
        for (auto e : s[i]) {
          int ey, ex;
          for (int k = 0; k < 3; k++) {
            for (int j = 0; j < 3; j++) {
              if (e[k][j] == -1) {
                ey = k;
                ex = j;
              }
            }
          }
          if (i < 30) {
            static int dy[] = {-1, 0, 1, 0};
            static int dx[] = {0, 1, 0, -1};
            for (int j = 0; j < 4; j++) {
              int ny = ey + dy[j];
              int nx = ex + dx[j];
              if (0 <= ny && ny < 3 && 0 <= nx && nx < 3) {
                auto n = e;
                n[ny][nx] = -1;
                n[ey][ex] = (e[ny][nx] + ((e[ny][nx] % 2 ^ j % 2) ? 1 : 5)) % 6;

                bool f = false;
                int c = 0;
                int gy, gx;
                for (int jj = 0; jj < 3; jj++) {
                  for (int k = 0; k < 3; k++) {
                    if (n[jj][k] == -1) {
                      f |= g[jj][k] != 'E';
                    } else {
                      f |= g[jj][k] != "WBRWBR"[n[jj][k]];
                    }
                    c += g[jj][k] != 'E' &&
                         (n[jj][k] == -1 || g[jj][k] != "WBRWBR"[n[jj][k]]);
                    if (g[jj][k] == 'E') {
                      gy = jj;
                      gx = k;
                    }
                  }
                }
                if (!f) {
                  cout << i + 1 << endl;
                  goto next;
                }
                if (max(c, abs(ny - gy) + abs(nx - gx)) + i + 1 > 30)
                  continue;
                s[i + 1].insert(n);
              }
            }
          }
        }
        s[i].clear();
      }
      cout << -1 << endl;
    next:;
    }
  }
}

Error: TLE

Fixed submission:

#include <array>
#include <cstdlib>
#include <iostream>
#include <set>

using namespace std;

typedef array<array<signed char, 3>, 3> Die;

int main() {
  for (int x, y; cin >> x >> y, x;) {
    char g[3][3];
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 3; j++) {
        cin >> g[i][j];
      }
    }
    set<Die> s[32], u;
    Die d{};
    d[y - 1][x - 1] = -1;
    s[0].insert(d);
    u.insert(d);
    for (int i = 0; i <= 30; i++) {
      for (auto e : s[i]) {
        bool f = false;
        int ey, ex;
        int c = 0;
        int gy, gx;
        for (int j = 0; j < 3; j++) {
          for (int k = 0; k < 3; k++) {
            if (e[j][k] == -1) {
              f |= g[j][k] != 'E';
              ey = j;
              ex = k;
            } else {
              f |= g[j][k] != "WBRWBR"[e[j][k]];
            }
            c += g[j][k] != 'E' &&
                 (e[j][k] == -1 || g[j][k] != "WBRWBR"[e[j][k]]);
            if (g[j][k] == 'E') {
              gy = j;
              gx = k;
            }
          }
        }
        if (!f) {
          cout << i << endl;
          goto next;
        }
        if (max(c, abs(ey - gy) + abs(ex - gx)) + i > 30)
          continue;
        if (i < 30) {
          static int dy[] = {-1, 0, 1, 0};
          static int dx[] = {0, 1, 0, -1};
          for (int j = 0; j < 4; j++) {
            int ny = ey + dy[j];
            int nx = ex + dx[j];
            if (0 <= ny && ny < 3 && 0 <= nx && nx < 3) {
              auto n = e;
              n[ny][nx] = -1;
              n[ey][ex] = (e[ny][nx] + ((e[ny][nx] % 2 ^ j % 2) ? 1 : 5)) % 6;
              //	      if(u.insert(n).second){
              s[i + 1].insert(n);
              //	      }
            }
          }
        }
      }
      s[i].clear();
    }
    cout << -1 << endl;
  next:;
  }
}