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

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

#define all(c) (c).begin(), (c).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb(e) push_back(e)
#define mp(a, b) make_pair(a, b)
#define fr first
#define sc second

const int INF = 100000000;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
using namespace std;
typedef pair<int, int> P;
typedef long long ll;

int p[100005];
int n;
int sugo[100005];
bool flag[100005];

int bfs() {

  queue<P> que;
  // 位置,回数
  que.push(P(0, 0));

  while (que.size()) {
    P pos = que.front();
    que.pop();

    int now = pos.first;
    int count = pos.second;
    if (flag[now])
      continue;
    flag[now] = true;
    // printf("[debug] %d,%d\\n", now, count);

    if (now >= n - 1) {
      return count;
    }

    rep(i, 6) {
      if (sugo[now + i + 1] == -1)
        continue;
      que.push(P(sugo[now + i + 1], count + 1));
    }
  }

  return INF;
}

int main() {
  cin >> n;
  rep(i, n) cin >> p[i];
  rep(i, 100005) sugo[i] = INF;
  set<int> visited;

  rep(i, n) {
    if (sugo[i] != INF)
      continue;
    if (p[i] == 0) {
      sugo[i] = i;
    } else {
      int s = i;
      int now = i + p[i];
      visited.clear();
      while (1) {
        if (now == s) {
          sugo[i] = -1;
          now = -1;
          break;
        }
        if (visited.find(now) != visited.end()) {
          sugo[i] = -1;
          now = -1;
          break;
        }
        if (p[now] == 0) {
          sugo[i] = now;
          break;
        } else {
          visited.insert(now);
          now += p[now];
        }
      }
      for (auto &e : visited) {
        sugo[e] = now;
      }
    }
  }

  // rep(i,n) cout<<sugo[i]<<endl;
  cout << bfs() << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

#define all(c) (c).begin(), (c).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb(e) push_back(e)
#define mp(a, b) make_pair(a, b)
#define fr first
#define sc second

const int INF = 100000000;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
using namespace std;
typedef pair<int, int> P;
typedef long long ll;

int p[100005];
int n;
int sugo[100005];
bool flag[100005];

int bfs() {

  queue<P> que;
  // 位置,回数
  que.push(P(0, 0));

  while (que.size()) {
    P pos = que.front();
    que.pop();

    int now = pos.first;
    int count = pos.second;
    if (flag[now])
      continue;
    flag[now] = true;
    // printf("[debug] %d,%d\\n", now, count);

    if (now >= n - 1) {
      return count;
    }

    rep(i, 6) {
      if (sugo[now + i + 1] == -1)
        continue;
      que.push(P(sugo[now + i + 1], count + 1));
    }
  }

  return INF;
}

int main() {
  cin >> n;
  rep(i, n) cin >> p[i];
  rep(i, 100005) sugo[i] = INF;
  set<int> visited;

  rep(i, n) {
    if (sugo[i] != INF)
      continue;
    if (p[i] == 0) {
      sugo[i] = i;
    } else {
      int s = i;
      int now = i + p[i];
      visited.clear();
      while (1) {
        if (now == s) {
          sugo[i] = -1;
          break;
        }
        if (visited.find(now) != visited.end()) {
          sugo[i] = -1;
          break;
        }
        if (p[now] == 0) {
          sugo[i] = now;
          break;
        } else {
          visited.insert(now);
          now += p[now];
        }
      }
      if (sugo[i] != -1) {
        for (auto &e : visited) {
          sugo[e] = now;
        }
      }
    }
  }

  // rep(i,n) cout<<sugo[i]<<endl;
  cout << bfs() << endl;

  return 0;
}