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

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define LOG(...) printf(__VA_ARGS__)
// #define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define RSORT(c) sort((c).rbegin(), (c).rend())
#define CLR(a) memset((a), 0, sizeof(a))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};

int main() {
  int n;
  cin >> n;
  vector<int> mass(n);
  REP(i, n)
  cin >> mass[i];
  vector<int> memo(n, 999999);
  memo[0] = 0;
  queue<int> Q;
  Q.push(0);
  set<int> se;
  while (!Q.empty()) {
    int q = Q.front();
    Q.pop();
    for (int i = 1; i < 7; i++) {
      if (q + i >= n)
        break;
      int q2 = q;

      se.insert(q2);
      q2 += i;
      while (1) {
        if (!EXIST(se, q2)) {
          se.insert(q2);
          if (mass[q2] == 0 && memo[q2] > memo[q] + 1) {
            memo[q2] = memo[q] + 1;
            Q.push(q2);
            break;
          } else
            q2 += mass[q2];
        } else
          break;
      }
    }
  }
  cout << memo[n - 1] << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define LOG(...) printf(__VA_ARGS__)
// #define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define RSORT(c) sort((c).rbegin(), (c).rend())
#define CLR(a) memset((a), 0, sizeof(a))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};

int main() {
  int n;
  cin >> n;
  vector<int> mass(n);
  REP(i, n)
  cin >> mass[i];
  vector<int> memo(n, 999999);
  memo[0] = 0;
  queue<int> Q;
  Q.push(0);
  while (!Q.empty()) {
    int q = Q.front();
    Q.pop();
    for (int i = 1; i < 7; i++) {
      if (q + i >= n)
        break;
      int q2 = q;
      set<int> se;
      se.insert(q2);
      q2 += i;
      while (1) {
        if (!EXIST(se, q2)) {
          se.insert(q2);
          if (mass[q2] == 0 && memo[q2] > memo[q] + 1) {
            memo[q2] = memo[q] + 1;
            Q.push(q2);
            break;
          } else
            q2 += mass[q2];
        } else
          break;
      }
    }
  }
  cout << memo[n - 1] << endl;
}