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

// g++ -std=c++11 temp.cpp
#include <bits/stdc++.h>
using namespace std;

#define For(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define Rep(i, n) For(i, 0, n)
#define Each(itr, v) for (auto itr : v)
#define pb(s) push_back(s)
#define mp(a, b) make_pair(a, b)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<P, int> PPI;

#define INF INT_MAX / 3;

#define MAX_N 100

int p, q;
int t[6];
string ans;

struct dice {
  int x, xx, y, yy, z, zz;
};
dice d;

void rotation(int *a, int *b, int *c, int *d) {
  int t = *a;
  *a = *b;
  *b = *c;
  *c = *d;
  *d = t;
}
void roll_e() { rotation(&d.x, &d.zz, &d.xx, &d.z); }
void roll_n() { rotation(&d.y, &d.zz, &d.yy, &d.z); }
void roll_s() { rotation(&d.y, &d.z, &d.yy, &d.zz); }
void roll_w() { rotation(&d.x, &d.z, &d.xx, &d.zz); }

// void roll_xy(){ rotation(&d.x,&d.y,&d.xx,&d.yy); }
void make_dice(int *t) {
  d.x = t[0];
  d.xx = t[1];
  d.y = t[2];
  d.yy = t[3];
  d.z = t[4];
  d.zz = t[5];
}

bool check() {
  if (d.x < 0 || d.xx < 0 || d.y < 0 || d.yy < 0 || d.z < 0 || d.zz < 0)
    return false;
  int sum = d.x + d.xx + d.y + d.yy + d.z + d.zz;

  if (2 * (d.z + d.zz) > sum)
    return false;
  if (2 * (d.y + d.yy) - 1 > sum)
    return false;
  if (2 * (d.x + d.xx) - 1 > sum)
    return false;
  return true;
}

string dfs(int n) {
  if (!check())
    return "impossible";
  // dbg(n);
  // dbg(d.x); dbg(d.xx); dbg(d.y); dbg(d.yy); dbg(d.z); dbg(d.zz);
  string ans = "";
  while (1) {
    if (n == 0) {
      if (d.x == 0 && d.xx == 0 && d.y == 0 && d.yy == 0 & d.z == 0 &&
          d.zz == 0)
        return ans;
      else
        break;
    }
    // if(d.x < 0 || d.xx < 0 || d.y < 0 || d.yy < 0 || d.z < 0 || d.zz < 0)
    // return "impossible"; if(!check()) return "impossible";

    roll_e();
    d.zz--;
    if (check()) {
      ans += "E";
      n--;
      continue;
    }
    d.zz++;
    roll_w();

    roll_n();
    d.zz--;
    if (check()) {
      ans += "N";
      n--;
      continue;
    }
    d.zz++;
    roll_s();

    roll_s();
    d.zz--;
    if (check()) {
      ans += "S";
      n--;
      continue;
    }
    d.zz++;
    roll_n();

    roll_w();
    d.zz--;
    if (check()) {
      ans += "W";
      n--;
      continue;
    }
    d.zz++;
    roll_e();
    break;
  }

  return "impossible";
}

int main(int argc, char const *argv[]) {
  while (1) {
    Rep(i, 6) scanf("%d", &t[i]);
    int sum = 0;
    Rep(i, 6) sum += t[i];
    if (t[5] == 0)
      break;
    scanf("%d %d", &p, &q);
    p--;
    string res = "Z";
    do {
      make_dice(t);
      if ((ans = dfs(sum)) != "impossible")
        res = min(ans, res);
    } while (next_permutation(t, t + 6));
    if (res == "Z")
      printf("impossible\\n");
    else
      cout << res.substr(p, q - p) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

// g++ -std=c++11 temp.cpp
#include <bits/stdc++.h>
using namespace std;

#define For(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define Rep(i, n) For(i, 0, n)
#define Each(itr, v) for (auto itr : v)
#define pb(s) push_back(s)
#define mp(a, b) make_pair(a, b)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<P, int> PPI;

#define INF INT_MAX / 3;

#define MAX_N 100

int p, q;
int t[6];
string ans;

struct dice {
  int x, xx, y, yy, z, zz;
};
dice d;

void rotation(int *a, int *b, int *c, int *d) {
  int t = *a;
  *a = *b;
  *b = *c;
  *c = *d;
  *d = t;
}
void roll_e() { rotation(&d.x, &d.zz, &d.xx, &d.z); }
void roll_n() { rotation(&d.y, &d.zz, &d.yy, &d.z); }
void roll_s() { rotation(&d.y, &d.z, &d.yy, &d.zz); }
void roll_w() { rotation(&d.x, &d.z, &d.xx, &d.zz); }

// void roll_xy(){ rotation(&d.x,&d.y,&d.xx,&d.yy); }
void make_dice(int *t) {
  d.x = t[0];
  d.xx = t[1];
  d.y = t[2];
  d.yy = t[3];
  d.z = t[4];
  d.zz = t[5];
}

bool check() {
  int sum = d.x + d.xx + d.y + d.yy + d.z + d.zz;

  if (2 * (d.z + d.zz) > sum)
    return false;
  if (2 * (d.y + d.yy) - 1 > sum)
    return false;
  if (2 * (d.x + d.xx) - 1 > sum)
    return false;
  return true;
}

string dfs(int n) {
  // dbg(n);
  // dbg(d.x); dbg(d.xx); dbg(d.y); dbg(d.yy); dbg(d.z); dbg(d.zz);
  if (n == 0) {
    if (d.x == 0 && d.xx == 0 && d.y == 0 && d.yy == 0 & d.z == 0 && d.zz == 0)
      return "";
    else
      return "impossible";
  }
  if (d.x < 0 || d.xx < 0 || d.y < 0 || d.yy < 0 || d.z < 0 || d.zz < 0)
    return "impossible";
  if (!check())
    return "impossible";
  string res;

  roll_e();
  d.zz--;
  if ((res = dfs(n - 1)) != "impossible")
    return "E" + res;
  d.zz++;
  roll_w();

  roll_n();
  d.zz--;
  if ((res = dfs(n - 1)) != "impossible")
    return "N" + res;
  d.zz++;
  roll_s();

  roll_s();
  d.zz--;
  if ((res = dfs(n - 1)) != "impossible")
    return "S" + res;
  d.zz++;
  roll_n();

  roll_w();
  d.zz--;
  if ((res = dfs(n - 1)) != "impossible")
    return "W" + res;
  d.zz++;
  roll_e();

  return "impossible";
}

int main(int argc, char const *argv[]) {
  while (1) {
    Rep(i, 6) scanf("%d", &t[i]);
    int sum = 0;
    Rep(i, 6) sum += t[i];
    if (t[5] == 0)
      break;
    scanf("%d %d", &p, &q);
    p--;
    string res = "Z";
    do {
      make_dice(t);
      if ((ans = dfs(sum)) != "impossible")
        res = min(ans, res);
    } while (next_permutation(t, t + 6));
    if (res == "Z")
      printf("impossible\\n");
    else
      cout << res.substr(p, q - p) << endl;
  }
  return 0;
}