Problem p01449 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <cstring>

using namespace std;

#define all(c) c.begin(), c.end()
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
#define until(b) while (not(b))

typedef long long ll;
typedef vector<int> vi;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &val) {
  os << "[ ";
  for (typename vector<T>::const_iterator it = val.begin(); it != val.end();
       ++it) {
    os << *it << " ";
  }
  os << "]";
  return os;
}

int solve(const vector<int> &p) {
  int N = p.size();
  queue<int> where;
  vector<char> used(N, false);

  where.push(0);
  for (int go = 0;; go++) {
    queue<int> next;
    while (not where.empty()) {
      int w = where.front();
      if (w >= N - 1)
        return go;
      where.pop();
      if (used[w])
        continue;
      used[w] = true;
      for (int a = 1; a <= min(6, N - 1 - w); a++) {
        int x = w + a;
        while (not used[x] and p[x] != 0) {
          used[x] = true;
          x += p[x];
        }
        if (not used[x] and p[x] == 0) {
          next.push(x);
        }
      }
    }
    where = next;
  }
  return -1;
}

int main() {
  int N;
  cin >> N;
  vector<int> p(N);
  rep(i, N) cin >> p[i];
  cout << solve(p) << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <cstring>

using namespace std;

#define all(c) c.begin(), c.end()
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
#define until(b) while (not(b))

typedef long long ll;
typedef vector<int> vi;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &val) {
  os << "[ ";
  for (typename vector<T>::const_iterator it = val.begin(); it != val.end();
       ++it) {
    os << *it << " ";
  }
  os << "]";
  return os;
}

int solve(const vector<int> &p) {
  int N = p.size();
  queue<int> where;
  vector<char> used(N, false);

  where.push(0);
  for (int go = 0;; go++) {
    queue<int> next;
    while (not where.empty()) {
      int w = where.front();
      if (w == N - 1)
        return go;
      where.pop();
      if (used[w])
        continue;
      for (int a = 1; a <= min(6, N - 1 - w); a++) {
        int x = w + a;
        while (not used[x] and p[x] != 0) {
          used[x] = true;
          x += p[x];
        }
        if (not used[x] and p[x] == 0) {
          next.push(x);
        }
      }
    }
    where = next;
  }
  return -1;
}

int main() {
  int N;
  cin >> N;
  vector<int> p(N);
  rep(i, N) cin >> p[i];
  cout << solve(p) << endl;
  return 0;
}