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

#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
using namespace std;

bool isValid(int foot[2]) {
  const static int px[10] = {0, -1, 0, 1, -1, 0, 1, -1, 0, 1};
  return px[foot[1]] - px[foot[0]] >= 0;
}

const int INF = 1 << 24;
string line;
int T[100001][2][2][10];

int solve() {
  if (line.size() <= 2)
    return 0;
  fill(T[0][0][0], T[100001][0][0], INF);
  int foot[2];
  foot[0] = line[0] - '0';
  foot[1] = line[1] - '0';
  if (isValid(foot))
    T[2][1][0][foot[0]] = 0;
  foot[1] = line[0] - '0';
  foot[0] = line[1] - '0';
  if (isValid(foot))
    T[2][0][0][foot[1]] = 0;

  for (int i = 3; i <= line.size(); ++i) {
    for (int j = 0; j < 2; ++j) {
      for (int k = 0; k < 2; ++k) {
        for (int l = 1; l <= 9; ++l) {
          if (l == 5)
            continue;
          foot[j] = line[i - 1] - '0';
          if (k == 0) {
            foot[1 - j] = line[i - 2] - '0';
            if (isValid(foot)) {
              T[i][j][k][foot[1 - j]] =
                  min(T[i][j][k][foot[1 - j]], T[i - 1][1 - j][0][l]);
              T[i][j][k][foot[1 - j]] =
                  min(T[i][j][k][foot[1 - j]], T[i - 1][1 - j][1][l]);
            }
          } else {
            foot[1 - j] = l;
            if (isValid(foot)) {
              T[i][j][k][l] = min(T[i][j][k][l], T[i - 1][j][0][l] + 1);
              T[i][j][k][l] = min(T[i][j][k][l], T[i - 1][j][1][l] + 1);
            }
          }
        }
      }
    }
  }
  int res = INF;
  for (int i = 0; i < 2; ++i) {
    for (int j = 0; j < 2; ++j) {
      for (int l = 1; l <= 9; ++l) {
        if (l == 5)
          continue;
        res = min(res, T[line.size()][i][j][l]);
      }
    }
  }
  return res;
}

int main() {
  while (getline(cin, line) && line != "#") {
    cout << solve() << endl;
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
using namespace std;

struct State {
  int step, foot[2], before, cost;
  const static int px[10];
  State() {}
  State(int step, int left, int right, int before, int cost)
      : step(step), before(before), cost(cost) {
    foot[0] = left;
    foot[1] = right;
  }
  bool operator<(const State &s) const { return cost > s.cost; }

  bool isValid() { return px[foot[1]] - px[foot[0]] >= 0; }
};
const int State::px[10] = {0, -1, 0, 1, -1, 0, 1, -1, 0, 1};

int solve(string line) {
  if (line.size() <= 2)
    return 0;
  priority_queue<State> que;
  State s;
  s = State(2, line[0] - '0', line[1] - '0', 1, 0);
  if (s.isValid())
    que.push(s);
  s = State(2, line[1] - '0', line[0] - '0', 0, 0);
  if (s.isValid())
    que.push(s);

  while (!que.empty()) {
    if (que.top().step == line.size())
      return que.top().cost;

    s = que.top();
    s.foot[1 - s.before] = line[s.step++] - '0';
    s.before = 1 - s.before;
    if (s.isValid())
      que.push(s);

    s = que.top();
    s.foot[s.before] = line[s.step++] - '0';
    ++s.cost;
    if (s.isValid())
      que.push(s);

    que.pop();
  }
}

int main() {
  string line;
  while (getline(cin, line) && line != "#") {
    cout << solve(line) << endl;
  }
}