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

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

using Field = array<int, 16>;
using Event = vector<Field>;
struct State {
  int day;
  int pos;
  array<int, 4> f;
};
bool operator<(State lhs, State rhs) {
  return make_tuple(lhs.day, lhs.pos, lhs.f[0], lhs.f[1], lhs.f[2], lhs.f[3]) <
         make_tuple(rhs.day, rhs.pos, rhs.f[0], rhs.f[1], rhs.f[2], rhs.f[3]);
}
set<State> s;
int n;
int dfs(State st, const Event &e);
vector<vector<int>> nei = {
    {0, 1, 2, 4, 8},  {0, 1, 2, 5, 9},  {0, 1, 2, 6, 10}, {},
    {0, 4, 8, 5, 6},  {1, 4, 9, 5, 6},  {2, 4, 10, 5, 6}, {},
    {0, 4, 8, 9, 10}, {1, 5, 8, 9, 10}, {2, 6, 8, 9, 10}, {}};
int dfs(State st, const Event &e) {
  if (st.day == n)
    return true;
  if (s.count(st))
    return false;
  s.insert(st);
  for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 2; j++) {
      if (e[st.day][st.pos + i * 4 + j]) {
        return false;
      }
      int x = st.pos + i * 4 + j;
      if (x == 0)
        st.f[0] = -1;
      if (x == 3)
        st.f[1] = -1;
      if (x == 12)
        st.f[2] = -1;
      if (x == 15)
        st.f[3] = -1;
    }
  }
  for (int i = 0; i < 4; i++) {
    if (++st.f[i] >= 7)
      return false;
  }
  st.day++;
  int prep = st.pos;
  for (int i = 0; i < nei[prep].size(); i++) {
    st.pos = nei[prep][i];
    if (dfs(st, e))
      return true;
  }

  return false;
}
int solve(Event e) {
  State ini;
  ini.day = 0;
  ini.pos = 5;
  fill(ini.f.begin(), ini.f.end(), 0);
  return dfs(ini, e);
}
int main() {
  while (cin >> n, n) {
    s.clear();
    Event event(n);
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < 16; j++)
        cin >> event[i][j];
    }
    cout << solve(event) << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

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

using Field = array<int, 16>;
using Event = vector<Field>;
struct State {
  int day;
  int pos;
  array<int, 4> f;
};
bool operator<(State lhs, State rhs) {
  return make_tuple(lhs.day, lhs.pos, lhs.f[0], lhs.f[1], lhs.f[2], lhs.f[3]) <
         make_tuple(rhs.day, rhs.pos, rhs.f[0], rhs.f[1], rhs.f[2], rhs.f[3]);
}
set<State> s;
int n;
int dfs(State st, const Event &e);
vector<vector<int>> nei = {
    {0, 1, 2, 4, 8},  {0, 1, 2, 5, 9},  {0, 1, 2, 6, 10}, {},
    {0, 4, 8, 5, 6},  {1, 4, 9, 5, 6},  {2, 4, 10, 5, 6}, {},
    {0, 4, 8, 9, 10}, {1, 5, 8, 9, 10}, {2, 6, 8, 9, 10}, {}};
int dfs(State st, const Event &e) {
  if (st.day == n)
    return true;
  if (s.count(st))
    return false;
  for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 2; j++) {
      if (e[st.day][st.pos + i * 4 + j]) {
        return false;
      }
      int x = st.pos + i * 4 + j;
      if (x == 0)
        st.f[0] = -1;
      if (x == 3)
        st.f[1] = -1;
      if (x == 12)
        st.f[2] = -1;
      if (x == 15)
        st.f[3] = -1;
    }
  }
  for (int i = 0; i < 4; i++) {
    if (++st.f[i] >= 7)
      return false;
  }
  st.day++;
  int prep = st.pos;
  for (int i = 0; i < nei[prep].size(); i++) {
    st.pos = nei[prep][i];
    if (dfs(st, e))
      return true;
  }
  s.insert(st);
  return false;
}
int solve(Event e) {
  State ini;
  ini.day = 0;
  ini.pos = 5;
  fill(ini.f.begin(), ini.f.end(), 0);
  return dfs(ini, e);
}
int main() {
  while (cin >> n, n) {
    s.clear();
    Event event(n);
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < 16; j++)
        cin >> event[i][j];
    }
    cout << solve(event) << endl;
  }
  return 0;
}