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

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
#define all(c) c.begin(), c.end()
#define INF 100000000
using namespace std;
typedef pair<int, int> P;
int n;
vector<int> dp;
vector<int> v;
vector<bool> no;
vector<bool> check;
int ans;
bool sh(int pos) {
  if (check[pos] || no[pos] == false) {
    return false;
  }
  if (pos >= n - 1 || v[pos] == 0 || no[pos] == true)
    return true;
  check[pos] = true;
  return no[pos] = sh(pos + v[pos]);
}

void ret(int pos, int cnt) {
  if (pos >= n - 1) {
    ans = min(cnt, ans);
    return;
  }
  if (dp[pos] != -1) {
    if (cnt >= dp[pos])
      return;
    dp[pos] = cnt;
  } else
    dp[pos] = cnt;

  if (v[pos] != 0) {
    ret(pos + v[pos], cnt);
    return;
  }
  for (int i = 1; i <= 6; i++) {
    if (pos + i <= n - 1 && no[pos + i])
      ret(pos + i, cnt + 1);
  }
}
int dfs() {
  queue<P> que;
  que.push(P(0, 0));
  int ans = INF;
  vector<int> d(n, INF);
  while (!que.empty()) {
    P pos = que.front();
    que.pop();
    // cout<<pos.first<<endl;
    if (d[pos.first] <= pos.second) {
      continue;
    }
    if (pos.first >= n - 1) {
      if (ans == INF) {
        ans = pos.second;
      } else {
        if (ans >= pos.second)
          ans = pos.second;
        else
          break;
      }
    }
    d[pos.first] = pos.second;
    if (v[pos.first] != 0) {
      que.push(P(v[pos.first] + pos.first, pos.second));
      continue;
    }
    for (int i = 1; i <= 6; i++) {
      if (i + pos.first <= n - 1 && no[i + pos.first]) {
        que.push(P(pos.first + i, pos.second + 1));
      }
    }
  }
  return ans;
}
int main() {
  cin >> n;
  no.resize(n);
  check.resize(n);
  v.resize(n);
  dp.resize(n);
  fill(all(no), true);

  fill(all(dp), -1);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }
  fill(all(check), false);
  for (int i = 1; i < n - 1; i++) {
    if (no[i]) {
      fill(all(check), false);
      no[i] = sh(i);
    }
  }
  // ret(0,0);

  cout << dfs() << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#define all(c) c.begin(), c.end()
#define INF 100000000
using namespace std;
int n;
vector<int> dp;
vector<int> v;
vector<bool> no;
vector<bool> check;
int ans;
bool sh(int pos) {
  if (check[pos] || no[pos] == false) {
    return false;
  }
  if (pos >= n - 1 || v[pos] == 0 || no[pos] == true)
    return true;
  check[pos] = true;
  return no[pos] = sh(pos + v[pos]);
}

void ret(int pos, int cnt) {
  if (pos >= n - 1) {
    ans = min(cnt, ans);
    return;
  }
  if (dp[pos] != -1) {
    if (cnt >= dp[pos])
      return;
    dp[pos] = cnt;
  } else
    dp[pos] = cnt;

  if (v[pos] != 0) {
    ret(pos + v[pos], cnt);
    return;
  }
  for (int i = 1; i <= 6; i++) {
    if (pos + i <= n - 1 && no[pos + i])
      ret(pos + i, cnt + 1);
  }
}

int main() {
  cin >> n;
  no.resize(n);
  check.resize(n);
  v.resize(n);
  dp.resize(n);
  fill(all(no), true);

  fill(all(dp), -1);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }
  fill(all(check), false);
  for (int i = 1; i < n - 1; i++) {
    if (no[i]) {
      fill(all(check), false);
      no[i] = sh(i);
    }
  }
  ans = INF;
  ret(0, 0);
  cout << ans << endl;
  return 0;
}