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

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

enum DIE { TOP, FRONT, RIGHT, LEFT, BACK, BOTTOM };
enum DIR { E, N, S, W, CLOCKWISE };

void rotate(vector<int> &die, DIR dir) {
  if (dir == E) {
    swap(die[TOP], die[RIGHT]);
    swap(die[TOP], die[BOTTOM]);
    swap(die[TOP], die[LEFT]);
  }
  if (dir == N) {
    swap(die[TOP], die[BACK]);
    swap(die[TOP], die[BOTTOM]);
    swap(die[TOP], die[FRONT]);
  }
  if (dir == S) {
    swap(die[TOP], die[FRONT]);
    swap(die[TOP], die[BOTTOM]);
    swap(die[TOP], die[BACK]);
  }
  if (dir == W) {
    swap(die[TOP], die[LEFT]);
    swap(die[TOP], die[BOTTOM]);
    swap(die[TOP], die[RIGHT]);
  }
  --die[BOTTOM];
}

bool possible(vector<int> die, DIR dir, int sum) {
  rotate(die, dir);
  --sum;
  if (die[BOTTOM] < 0)
    return false;
  if (sum == 1)
    return die[TOP] == 0 && die[BOTTOM] == 0;
  int fb = die[FRONT] + die[BACK];
  int tb = die[TOP] + die[BOTTOM];
  int lr = die[LEFT] + die[RIGHT];
  return fb <= sum - fb + 1 && lr <= sum - lr + 1 && tb <= sum - tb;
}

void greedy(char res[30000 + 1], vector<int> die) {
  res[0] = 'X';
  res[1] = '\\0';
  int size = 0;
  int sum = accumulate(begin(die), end(die), 0);
  if (!possible(die, E, sum)) {
    res[0] = 'X';
    res[1] = '\\0';
    return;
  }
  while (0 < sum) {
    for (auto dir : {E, N, S, W})
      if (possible(die, dir, sum)) {
        rotate(die, dir);
        --sum;
        if (dir == E)
          res[size++] = 'E';
        else if (dir == N)
          res[size++] = 'N';
        else if (dir == S)
          res[size++] = 'S';
        else if (dir == W)
          res[size++] = 'W';
        break;
      }
  }
  res[size] = '\\0';
}

int main() {
  while (true) {
    vector<int> t(6);
    for (auto &i : t)
      cin >> i;
    if (all_of(begin(t), end(t), [](int i) { return i == 0; }))
      break;

    char ans[30000 + 1], res[30000 + 1];
    ans[0] = 'X';
    ans[1] = '\\0';
    sort(begin(t), end(t));
    do {
      greedy(res, t);
      if (strcmp(res, ans) < 0)
        swap(ans, res);
    } while (next_permutation(begin(t), end(t)));

    int p, q;
    cin >> p >> q;
    if (ans[0] == 'X')
      printf("impossible\\n");
    else {
      for (int i = p - 1; i < q; ++i)
        putchar(ans[i]);
      puts("");
    }
  }
}

Error: TLE

Fixed submission:

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

enum DIE { TOP, FRONT, RIGHT, LEFT, BACK, BOTTOM };
enum DIR { E, N, S, W, CLOCKWISE };
const string INF = "X";

void rotate(vector<int> &die, DIR dir) {
  if (dir == E) {
    swap(die[TOP], die[RIGHT]);
    swap(die[TOP], die[BOTTOM]);
    swap(die[TOP], die[LEFT]);
  }
  if (dir == N) {
    swap(die[TOP], die[BACK]);
    swap(die[TOP], die[BOTTOM]);
    swap(die[TOP], die[FRONT]);
  }
  if (dir == S) {
    swap(die[TOP], die[FRONT]);
    swap(die[TOP], die[BOTTOM]);
    swap(die[TOP], die[BACK]);
  }
  if (dir == W) {
    swap(die[TOP], die[LEFT]);
    swap(die[TOP], die[BOTTOM]);
    swap(die[TOP], die[RIGHT]);
  }
  --die[BOTTOM];
}

bool possible(vector<int> die, DIR dir, int sum) {
  rotate(die, dir);
  --sum;
  if (die[BOTTOM] < 0)
    return false;
  if (sum == 1)
    return die[TOP] == 0 && die[BOTTOM] == 0;
  int fb = die[FRONT] + die[BACK];
  int tb = die[TOP] + die[BOTTOM];
  int lr = die[LEFT] + die[RIGHT];
  return fb <= sum - fb + 1 && lr <= sum - lr + 1 && tb <= sum - tb;
}

string greedy(vector<int> die, const string &upper_bound) {
  string res;
  int sum = accumulate(begin(die), end(die), 0);
  if (!possible(die, E, sum))
    return INF;
  while (0 < sum) {
    for (auto dir : {E, N, S, W})
      if (possible(die, dir, sum)) {
        rotate(die, dir);
        --sum;
        if (dir == E)
          res += "E";
        else if (dir == N)
          res += "N";
        else if (dir == S)
          res += "S";
        else if (dir == W)
          res += "W";
        if (upper_bound < res)
          return INF;
        break;
      }
  }
  return res;
}

int main() {
  while (true) {
    vector<int> t(6);
    for (auto &i : t)
      cin >> i;
    if (all_of(begin(t), end(t), [](int i) { return i == 0; }))
      break;

    string ans = INF;
    sort(begin(t), end(t));
    do {
      ans = min(ans, greedy(t, ans));
    } while (next_permutation(begin(t), end(t)));

    int p, q;
    cin >> p >> q;
    cout << (ans == INF ? "impossible" : ans.substr(p - 1, q - p + 1)) + "\\n";
  }
}