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

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<VVI> VVVI;
typedef vector<VVVI> VVVVI;

typedef vector<LL> VLL;
typedef vector<VLL> VVLL;

typedef vector<double> VD;
typedef vector<VD> VDD;

typedef vector<string> VS;
typedef vector<VS> VVS;

typedef vector<bool> VB;
typedef vector<VB> VVB;

typedef pair<int, int> PII;
typedef vector<PII> VPII;

#define fst first
#define snd second
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define ALL(x) (x).begin(), (x).end()
#define RANGE(x, y, maxX, maxY)                                                \\
  (0 <= (x) && 0 <= (y) && (x) < (maxX) && y < (maxY))
#define REP(i, init, N) for (int i = (init); i < (int)(N); i++)

const int INF = 0x3f3f3f3f;
const int DX[] = {-1, 0, 1, -1, 0, 1, -1, 0, 1};
const int DY[] = {1, 1, 1, 0, 0, 0, -1, -1, -1};

string qs;

bool ok(int l, int r) {
  int lx = DX[l];
  int rx = DX[r];
  return lx <= rx;
}

PII move(int l, int r, bool is_l, int q) {
  int nl = is_l ? (q - 1) : l;
  int nr = is_l ? r : (q - 1);
  return MP(nl, nr);
}

int solve() {
  int N = qs.size();

  // dp[step][l][r][before_is_l] = min_invalid_step
  VVVVI dp(2, VVVI(9, VVI(9, VI(2, INF))));
  dp[0][0][8][0] = 0;
  dp[0][0][8][1] = 0;

  REP(n, 0, N) {
    dp[(n + 1) % 2] = VVVI(9, VVI(9, VI(2, INF)));
    REP(l, 0, 9) REP(r, 0, 9) REP(b_is_l, 0, 2) {
      if (dp[n % 2][l][r][b_is_l] == INF)
        continue;
      int cnt = dp[n % 2][l][r][b_is_l];
      int q = qs[n] - '0';

      REP(is_l, 0, 2) {
        PII nt = move(l, r, is_l, q);
        if (!ok(nt.fst, nt.snd))
          continue;

        dp[(n + 1) % 2][nt.fst][nt.snd][is_l] =
            min(dp[(n + 1) % 2][nt.fst][nt.snd][is_l], cnt + (b_is_l == is_l));
      }
    }
  }

  int ans = INF;
  REP(l, 0, 9) REP(r, 0, 9) REP(is_l, 0, 2) {
    ans = min(ans, dp[N % 2][l][r][is_l]);
  }

  return ans;
}

int main(void) {
  while (cin >> qs, qs != "#") {
    cout << solve() << endl;
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<VVI> VVVI;
typedef vector<VVVI> VVVVI;

typedef vector<LL> VLL;
typedef vector<VLL> VVLL;

typedef vector<double> VD;
typedef vector<VD> VDD;

typedef vector<string> VS;
typedef vector<VS> VVS;

typedef vector<bool> VB;
typedef vector<VB> VVB;

typedef pair<int, int> PII;
typedef vector<PII> VPII;

#define fst first
#define snd second
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define ALL(x) (x).begin(), (x).end()
#define RANGE(x, y, maxX, maxY)                                                \\
  (0 <= (x) && 0 <= (y) && (x) < (maxX) && y < (maxY))
#define REP(i, init, N) for (int i = (init); i < (int)(N); i++)

const int INF = 0x3f3f3f3f;
const int DX[] = {-1, 0, 1, -1, 0, 1, -1, 0, 1};
const int DY[] = {1, 1, 1, 0, 0, 0, -1, -1, -1};

string qs;

bool ok(int l, int r) {
  int lx = DX[l];
  int rx = DX[r];
  return lx <= rx;
}

PII move(int l, int r, bool is_l, int q) {
  int nl = is_l ? (q - 1) : l;
  int nr = is_l ? r : (q - 1);
  return MP(nl, nr);
}

int solve() {
  int N = qs.size();

  // dp[step][l][r][before_is_l] = min_invalid_step
  VVVVI dp(N + 1, VVVI(9, VVI(9, VI(2, INF))));
  dp[0][0][8][0] = 0;
  dp[0][0][8][1] = 0;

  REP(n, 0, N) REP(l, 0, 9) REP(r, 0, 9) REP(b_is_l, 0, 2) {
    if (dp[n][l][r][b_is_l] == INF)
      continue;
    int cnt = dp[n][l][r][b_is_l];
    int q = qs[n] - '0';

    REP(is_l, 0, 2) {
      PII nt = move(l, r, is_l, q);
      if (!ok(nt.fst, nt.snd))
        continue;

      dp[n + 1][nt.fst][nt.snd][is_l] =
          min(dp[n + 1][nt.fst][nt.snd][is_l], cnt + (b_is_l == is_l));
    }
  }

  int ans = INF;
  REP(l, 0, 9) REP(r, 0, 9) REP(is_l, 0, 2) {
    ans = min(ans, dp[N][l][r][is_l]);
  }

  return ans;
}

int main(void) {
  while (cin >> qs, qs != "#") {
    cout << solve() << endl;
  }

  return 0;
}