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

#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
int t[6];
int p, q, all;
int b[6];
int d[6];
bool flag, flag2;
char res[30001];
char now[30001];
char str[4] = {'E', 'N', 'S', 'W'};

bool check() {
  if (d[0] < 0 || d[1] < 0 || d[2] < 0 || d[3] < 0 || d[4] < 0 || d[5] < 0)
    return false;
  if (d[0] + d[3] > d[1] + d[2] + d[4] + d[5])
    return false;
  if (d[1] + d[4] > d[0] + d[2] + d[3] + d[5] + 1)
    return false;
  if (d[2] + d[5] > d[0] + d[1] + d[3] + d[4] + 1)
    return false;
  return true;
}

bool check2() {
  for (int i = 0; i < q; i++) {
    if (res[i] > now[i])
      return true;
    if (now[i] > res[i])
      return false;
  }
  return false;
}

void roll(int v) {
  if (v == 0) {
    int sw = d[0];
    d[0] = d[2];
    d[2] = d[3];
    d[3] = d[5];
    d[5] = sw;
  }
  if (v == 2) {
    int sw = d[0];
    d[0] = d[1];
    d[1] = d[3];
    d[3] = d[4];
    d[4] = sw;
  }
  if (v == 1) {
    int sw = d[0];
    d[0] = d[4];
    d[4] = d[3];
    d[3] = d[1];
    d[1] = sw;
  }
  if (v == 3) {
    int sw = d[0];
    d[0] = d[5];
    d[5] = d[3];
    d[3] = d[2];
    d[2] = sw;
  }
}

void dfs(int rest, int power) {
  if (flag2)
    return;
  if (rest == 0) {
    flag2 = true;
    if (!flag) {
      flag = true;
      for (int i = 0; i < all; i++)
        res[i] = now[i];
    } else if (check2()) {
      for (int i = 0; i < all; i++)
        res[i] = now[i];
    }
  }
  for (int i = 0; i < 4; i++) {
    roll(i);
    d[3]--;
    if (check()) {
      now[all - rest] = str[i];
      if (power == 0) {
        if (str[i] == res[all - rest])
          dfs(rest - 1, 0);
        if (str[i] < res[all - rest])
          dfs(rest - 1, 1);
      } else
        dfs(rest - 1, 1);
    }
    d[3]++;
    if (i == 0)
      roll(3);
    if (i == 3)
      roll(0);
    if (i == 1)
      roll(2);
    if (i == 2)
      roll(1);
  }
}

int main(void) {
  for (int i = 0; i < 6; i++)
    b[i] = i;
  while (1) {
    all = 0;
    for (int i = 0; i < 6; i++) {
      scanf("%d", &t[i]);
      all += t[i];
    }
    if (all == 0)
      break;
    flag = false;
    scanf("%d %d", &p, &q);
    do {
      for (int i = 0; i < 6; i++) {
        d[i] = t[b[i]];
      }
      if (check()) {
        flag2 = false;
        if (!flag)
          dfs(all, 1);
        else
          dfs(all, 0);
      }
    } while (next_permutation(b, b + 6));
    if (!flag)
      printf("impossible\\n");
    else {
      for (int i = p - 1; i < q; i++) {
        printf("%c", res[i]);
      }
      printf("\\n");
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
int t[6];
int p, q, all;
int b[6];
int d[6];
bool flag, flag2;
char res[30001];
char now[30001];
char str[4] = {'E', 'N', 'S', 'W'};

bool check() {
  if (d[0] < 0 || d[1] < 0 || d[2] < 0 || d[3] < 0 || d[4] < 0 || d[5] < 0)
    return false;
  if (d[0] + d[3] > d[1] + d[2] + d[4] + d[5])
    return false;
  if (d[1] + d[4] > d[0] + d[2] + d[3] + d[5] + 1)
    return false;
  if (d[2] + d[5] > d[0] + d[1] + d[3] + d[4] + 1)
    return false;
  return true;
}

bool check2() {
  for (int i = 0; i < all; i++) {
    if (res[i] > now[i])
      return true;
    if (now[i] > res[i])
      return false;
  }
  return false;
}

void roll(int v) {
  if (v == 0) {
    int sw = d[0];
    d[0] = d[2];
    d[2] = d[3];
    d[3] = d[5];
    d[5] = sw;
  }
  if (v == 2) {
    int sw = d[0];
    d[0] = d[1];
    d[1] = d[3];
    d[3] = d[4];
    d[4] = sw;
  }
  if (v == 1) {
    int sw = d[0];
    d[0] = d[4];
    d[4] = d[3];
    d[3] = d[1];
    d[1] = sw;
  }
  if (v == 3) {
    int sw = d[0];
    d[0] = d[5];
    d[5] = d[3];
    d[3] = d[2];
    d[2] = sw;
  }
}

void dfs(int rest) {
  if (flag2)
    return;
  if (rest == 0) {
    flag2 = true;
    if (!flag) {
      flag = true;
      for (int i = 0; i < all; i++)
        res[i] = now[i];
    } else if (check2()) {
      for (int i = 0; i < all; i++)
        res[i] = now[i];
    }
  }
  for (int i = 0; i < 4; i++) {
    roll(i);
    d[3]--;
    if (check()) {
      now[all - rest] = str[i];
      dfs(rest - 1);
    }
    d[3]++;
    if (i == 0)
      roll(3);
    if (i == 3)
      roll(0);
    if (i == 1)
      roll(2);
    if (i == 2)
      roll(1);
  }
}

int main(void) {
  for (int i = 0; i < 6; i++)
    b[i] = i;
  while (1) {
    all = 0;
    for (int i = 0; i < 6; i++) {
      scanf("%d", &t[i]);
      all += t[i];
    }
    if (all == 0)
      break;
    flag = false;
    scanf("%d %d", &p, &q);
    do {
      for (int i = 0; i < 6; i++) {
        d[i] = t[b[i]];
      }
      if (check()) {
        flag2 = false;
        dfs(all);
      }
    } while (next_permutation(b, b + 6));
    if (!flag)
      printf("impossible\\n");
    else {
      for (int i = p - 1; i < q; i++) {
        printf("%c", res[i]);
      }
      printf("\\n");
    }
  }
  return 0;
}