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

#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#ifdef __GXX_EXPERIMENTAL_CXX0X__
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif

#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#ifdef __GXX_EXPERIMENTAL_CXX0X__
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <system_error>
#include <thread>
#include <tuple>
#include <type_traits>
#include <typeindex>
#include <unordered_map>
#include <unordered_set>
#endif

#include <array>

using namespace std;

int data[100002] = {};

int main() {
  int N;
  cin >> N;
  vector<int> D;
  D.resize(N + 1);
  for (size_t i = 0; i < N; i++) {
    cin >> D[i];
  }
  data[0] = 1;
  for (size_t i = 1; i <= N * 2; i++) {
    for (size_t j = 0; j <= N; j++) {
      if (data[j] == i && D[j] == 0) {
        stack<int> S;
        if (j + 6 >= N - 1) {
          if (data[N - 1] == 0) {
            cout << data[j] << endl;
          } else {
            cout << data[N - 1] - 1 << endl;
          }
          return 0;
        }
        for (size_t k = 1; k <= 6 && k + j <= N; k++) {
          if (data[k + j] == 0) {
            S.push(k + j);
            data[k + j] = data[j] + 1;
          }
        }
        while (S.size()) {
          int x = S.top();
          S.pop();
          if (data[x + D[x]] == 0) {
            S.push(x + D[x]);
            data[x + D[x]] = data[x];
          }
        }
      }
    }
  }
  // cout << data[N - 1] - 1 << endl;
}

Error: TLE

Fixed submission:

#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#ifdef __GXX_EXPERIMENTAL_CXX0X__
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif

#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#ifdef __GXX_EXPERIMENTAL_CXX0X__
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <system_error>
#include <thread>
#include <tuple>
#include <type_traits>
#include <typeindex>
#include <unordered_map>
#include <unordered_set>
#endif

#include <array>

using namespace std;

int data[100002] = {};

int main() {
  int N;
  cin >> N;
  vector<int> D;
  D.resize(N + 1);
  for (size_t i = 0; i < N; i++) {
    cin >> D[i];
  }
  data[0] = 1;
  for (size_t i = 1; i <= N * 2; i++) {
    for (size_t j = 0; j <= N; j++) {
      if (data[j] == i && D[j] == 0) {
        stack<int> S;
        for (size_t k = 1; k <= 6 && k + j <= N; k++) {
          if (data[k + j] == 0) {
            S.push(k + j);
            data[k + j] = data[j] + 1;
          }
        }
        while (S.size()) {
          int x = S.top();
          S.pop();
          if (data[x + D[x]] == 0) {
            S.push(x + D[x]);
            data[x + D[x]] = data[x];
          }
        }
      }
    }
  }

  cout << data[N - 1] - 1 << endl;
}