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

#include <algorithm>
#include <iostream>
#include <string>

using namespace std;

// step % 2, left, right, last_foot(0:1=L:R)
int dp[2][9][9][2];

const int INF = 100000000;

int main() {
  while (true) {
    string line;
    cin >> line;
    if (line == "#") {
      break;
    }
    if (line.size() < 2) {
      cout << 0 << endl;
      continue;
    }
    int panel = line[0] - '1';
    for (int j = 0; j < 9; ++j) {
      for (int k = 0; k < 9; ++k) {
        for (int l = 0; l < 2; ++l) {
          dp[0][j][k][l] = INF;
        }
      }
    }
    for (int i = 0; i < 9; ++i) {
      dp[0][panel][i][0] = 0;
      dp[0][i][panel][1] = 0;
    }
    int answer = INF;
    for (int i = 1; i < line.size(); ++i) {
      answer = INF;
      panel = line[i] - '1';
      for (int j = 0; j < 9; ++j) {
        for (int k = 0; k < 9; ++k) {
          for (int l = 0; l < 2; ++l) {
            dp[i % 2][j][k][l] = INF;
          }
        }
      }
      for (int l = 0; l < 9; ++l) {
        for (int r = 0; r < 9; ++r) {
          // move left
          if (panel % 3 <= r % 3 && r != panel) {
            int res = dp[i % 2][panel][r][0];
            res = min(res, dp[(i - 1) % 2][l][r][0] + 1);
            res = min(res, dp[(i - 1) % 2][l][r][1]);
            answer = min(answer, res);
            dp[i % 2][panel][r][0] = res;
          }
          // move right
          if (panel % 3 >= l % 3 && l != panel) {
            int res = dp[i % 2][l][panel][1];
            res = min(res, dp[(i - 1) % 2][l][r][0]);
            res = min(res, dp[(i - 1) % 2][l][r][1] + 1);
            answer = min(answer, res);
            dp[i % 2][l][panel][1] = res;
          }
        }
      }
    }
    cout << answer << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>

using namespace std;

// step, left, right, last_foot(0:1=L:R)
int dp[100001][8][8][2];

const int INF = 100000000;

int main() {
  while (true) {
    string line;
    cin >> line;
    if (line == "#") {
      break;
    }
    if (line.size() <= 2) {
      cout << 0 << endl;
      continue;
    }
    for (int i = 0; i < 100001; ++i) {
      for (int j = 0; j < 8; ++j) {
        for (int k = 0; k < 8; ++k) {
          for (int l = 0; l < 2; ++l) {
            dp[i][j][k][l] = INF;
          }
        }
      }
    }
    int panel = line[0] - '1';
    dp[0][panel][0][0] = 0;
    dp[0][0][panel][1] = 0;
    int answer = INF;
    for (int i = 1; i < line.size(); ++i) {
      answer = INF;
      panel = line[i] - '1';
      for (int l = 0; l < 8; ++l) {
        for (int r = 0; r < 8; ++r) {
          // move left
          if (panel % 3 <= r % 3) {
            int res = dp[i][panel][r][0];
            res = min(res, dp[i - 1][l][r][0] + 1);
            res = min(res, dp[i - 1][l][r][1]);
            answer = min(answer, res);
            dp[i][panel][r][0] = res;
          }
          // move right
          if (panel % 3 >= l % 3) {
            int res = dp[i][l][panel][1];
            res = min(res, dp[i - 1][l][r][0]);
            res = min(res, dp[i - 1][l][r][1] + 1);
            answer = min(answer, res);
            dp[i][l][panel][1] = res;
          }
        }
      }
    }
    cout << answer << endl;
  }
  return 0;
}