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

#include <algorithm>
#include <complex>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)

typedef long long int ll;
typedef double D;     // ??§?¨?????????????double???long double?????????
typedef complex<D> P; // Point
typedef pair<P, P> L; // Line
typedef vector<P> VP;

const int INF = (1 << 30) - 1;
const D EPS = 1e-9;

#define X real()
#define Y imag()
#define LE(n, m) ((n) < (m) + EPS)
#define GE(n, m) ((n) + EPS > (m))
#define EQ(n, m) (abs((n) - (m)) < EPS)

int dfs(vector<int> &ps, vector<int> &d, vector<bool> &visited, int v) {
  if (visited[v])
    return d[v];

  visited[v] = true;
  if (ps[v] == 0)
    return d[v] = v;

  return d[v] = dfs(ps, d, visited, v + ps[v]);
}

int bfs(vector<int> &d, vector<int> &dp) {
  int N = d.size();
  queue<int> q;
  dp[0] = 0;
  q.push(0);

  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (int i = 1; i <= 6; i++) {
      if (N - 1 <= v + i || d[v + i] == N - 1)
        return dp[v] + 1;

      if (d[v + i] >= 0 && dp[d[v + i]] == INF) {
        dp[d[v + i]] = dp[v] + 1;
        q.push(d[v + i]);
      }
    }
  }

  // No Implement
  return -1;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int N;
  cin >> N;
  vector<int> ps(N);
  vector<int> d(N, -1);
  vector<int> dp(N, INF);
  vector<bool> visited(N, false);

  for (auto &p : ps)
    cin >> p;

  for (int i = 0; i < N; i++)
    d[i] = dfs(ps, d, visited, i);

  cout << bfs(d, dp) << endl;

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <complex>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)

typedef long long int ll;
typedef double D;     // ??§?¨?????????????double???long double?????????
typedef complex<D> P; // Point
typedef pair<P, P> L; // Line
typedef vector<P> VP;

const int INF = (1 << 30) - 1;
const D EPS = 1e-9;

#define X real()
#define Y imag()
#define LE(n, m) ((n) < (m) + EPS)
#define GE(n, m) ((n) + EPS > (m))
#define EQ(n, m) (abs((n) - (m)) < EPS)

int dfs(vector<int> &ps, vector<int> &d, vector<bool> visited, int v) {
  if (visited[v])
    return d[v];

  visited[v] = true;
  if (ps[v] == 0)
    return d[v] = v;

  return d[v] = dfs(ps, d, visited, v + ps[v]);
}

int bfs(vector<int> &d, vector<int> &dp) {
  int N = d.size();
  queue<int> q;
  dp[0] = 0;
  q.push(0);

  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (int i = 1; i <= 6; i++) {
      if (N - 1 <= v + i || d[v + i] == N - 1)
        return dp[v] + 1;

      if (d[v + i] >= 0 && dp[d[v + i]] == INF) {
        dp[d[v + i]] = dp[v] + 1;
        q.push(d[v + i]);
      }
    }
  }

  // No Implement
  return -1;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int N;
  cin >> N;
  vector<int> ps(N);
  vector<int> d(N, -1);
  vector<int> dp(N, INF);
  vector<bool> visited(N, false);

  for (auto &p : ps)
    cin >> p;

  for (int i = 0; i < N; i++)
    d[i] = dfs(ps, d, visited, i);

  cout << bfs(d, dp) << endl;

  return 0;
}