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

#include <bits/stdc++.h>

using namespace std;

struct Dice {
  // int x, y;
  int l, r, f, b, d, u;

  void RollN() {
    //  --y;
    int buff = d;
    d = f;
    f = u;
    u = b;
    b = buff;
  }

  void RollS() {
    // ++y;
    int buff = d;
    d = b;
    b = u;
    u = f;
    f = buff;
  }

  void RollL() // ----->
  {
    int buff = f;
    f = l;
    l = b;
    b = r;
    r = buff;
  }

  void RollR() // <------
  {
    int buff = f;
    f = r;
    r = b;
    b = l;
    l = buff;
  }

  void RollE() // .o -> o.
  {
    // --x;
    int buff = d;
    d = l;
    l = u;
    u = r;
    r = buff;
  }

  void RollW() // o. -> .o
  {
    //  ++x;
    int buff = d;
    d = r;
    r = u;
    u = l;
    l = buff;
  }

  vector<Dice> makeDice() {
    vector<Dice> ret;
    for (int i = 0; i < 6; i++) {
      Dice d(*this);
      if (i == 1)
        d.RollN();
      if (i == 2)
        d.RollS();
      if (i == 3)
        d.RollS(), d.RollS();
      if (i == 4)
        d.RollL();
      if (i == 5)
        d.RollR();
      for (int j = 0; j < 4; j++) {
        ret.emplace_back(d);
        d.RollE();
      }
    }
    return (ret);
  }
};

int latte[3][3], malta[3][3];
set<int> st;
Dice dice[3][3][3];
vector<Dice> beet;

void dfs(int idx) {
  if (idx == 27) {
    int ret = 0;
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 3; j++) {
        ret += dice[i][2][j].r;
      }
    }
    st.emplace(ret);
    return;
  }
  int i = idx / 3 % 3, j = idx % 3, k = idx / 9;
  for (auto &s : beet) {
    if (k == 0 && latte[i][j] && latte[i][j] != s.u)
      continue;
    if (i == 2 && malta[k][j] && malta[k][j] != s.f)
      continue;
    if (i && dice[i - 1][j][k].b + s.f != 7)
      continue;
    if (j && dice[i][j - 1][k].l + s.r != 7)
      continue;
    if (k && dice[i][j][k - 1].d + s.u != 7)
      continue;
    dice[i][j][k] = s;
    dfs(idx + 1);
  }
}

int main() {
  beet = ((Dice){4, 3, 2, 5, 6, 1}).makeDice();

  int T;
  cin >> T;
  while (T--) {
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 3; j++) {
        cin >> latte[i][j];
      }
    }
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 3; j++) {
        cin >> malta[i][j];
      }
    }
    dfs(0);
    bool first = false;
    if (st.empty()) {
      cout << 0 << endl;
    } else {
      for (auto &s : st) {
        if (first++)
          cout << " ";
        cout << s;
      }
      cout << endl;
      st.clear();
    }
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

struct Dice {
  // int x, y;
  int l, r, f, b, d, u;

  void RollF() {
    //  --y;
    int buff = d;
    d = f;
    f = u;
    u = b;
    b = buff;
  }

  void RollB() {
    // ++y;
    int buff = d;
    d = b;
    b = u;
    u = f;
    f = buff;
  }

  void RollL() {
    // --x;
    int buff = d;
    d = l;
    l = u;
    u = r;
    r = buff;
  }

  void RollR() {
    //  ++x;
    int buff = d;
    d = r;
    r = u;
    u = l;
    l = buff;
  }

  void RollClock() {
    int buff = d;
    d = r;
    r = u;
    u = l;
    l = buff;
  }

  void RollCounterClock() {
    int buff = d;
    d = l;
    l = u;
    u = r;
    r = buff;
  }

  vector<Dice> makeDice() {
    vector<Dice> ret;
    for (int i = 0; i < 6; i++) {
      Dice d(*this);
      if (i == 1)
        d.RollClock();
      if (i == 2)
        d.RollCounterClock();
      if (i == 3)
        d.RollClock(), d.RollClock();
      if (i == 4)
        d.RollL();
      if (i == 5)
        d.RollR();
      for (int j = 0; j < 4; j++) {
        ret.emplace_back(d);
        d.RollF();
      }
    }
    return (ret);
  }
};

int latte[3][3], malta[3][3];
set<int> st;
Dice dice[3][3][3];
vector<Dice> beet;

void dfs(int idx) {
  if (idx == 27) {
    int ret = 0;
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 3; j++) {
        ret += dice[i][2][j].r;
      }
    }
    st.emplace(ret);
    return;
  }
  int i = idx / 3 % 3, j = idx % 3, k = idx / 9;
  for (auto &s : beet) {
    dice[i][j][k] = s;
    if (k == 0 && latte[i][j] && latte[i][j] != dice[i][j][k].u)
      continue;
    if (i == 2 && malta[k][j] && malta[k][j] != dice[i][j][k].f)
      continue;
    if (i && dice[i - 1][j][k].b + dice[i][j][k].f != 7)
      continue;
    if (j && dice[i][j - 1][k].l + dice[i][j][k].r != 7)
      continue;
    if (k && dice[i][j][k - 1].d + dice[i][j][k].u != 7)
      continue;
    dfs(idx + 1);
  }
}

int main() {
  beet = ((Dice){4, 3, 2, 5, 6, 1}).makeDice();

  int T;
  cin >> T;
  while (T--) {
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 3; j++) {
        cin >> latte[i][j];
      }
    }
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 3; j++) {
        cin >> malta[i][j];
      }
    }
    dfs(0);
    bool first = false;
    if (st.empty()) {
      cout << 0 << endl;
    } else {
      for (auto &s : st) {
        if (first++)
          cout << " ";
        cout << s;
      }
      cout << endl;
      st.clear();
    }
  }
}