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

#include <algorithm>
#include <cstring>
#include <iostream>
#include <stack>

#define INF (1 << 28)

using namespace std;

int n, m;
stack<int> p[3];

#define AB 1
#define BC 3

int move(int from, int to, int prev) {
  int c = p[from].top();
  p[to].push(c);
  p[from].pop();
  if (p[1].top() == 0 && (p[0].top() == 0 || p[2].top() == 0))
    return 1;
  if (prev == BC) {
    if (p[0].top() > p[1].top())
      return move(0, 1, AB) + 1;
    else if (p[0].top() < p[1].top())
      return move(1, 0, AB) + 1;
    return INF;
  } else if (prev == AB) {
    if (p[1].top() > p[2].top())
      return move(1, 2, BC) + 1;
    else if (p[1].top() < p[2].top())
      return move(2, 1, BC) + 1;
    return INF;
  }
}

int solve() {
  if (p[1].top() == 0 && (p[0].top() == 0 || p[2].top() == 0))
    return 0;
  int ans = INF;
  stack<int> backup[3];
  for (int i = 0; i < 3; i++)
    backup[i] = p[i];

  if (p[0].top() > p[1].top()) {
    ans = min(ans, move(0, 1, AB));
    for (int i = 0; i < 3; i++)
      p[i] = backup[i];
  } else {
    ans = min(ans, move(1, 0, AB));
    for (int i = 0; i < 3; i++)
      p[i] = backup[i];
  }
  if (p[1].top() > p[2].top()) {
    ans = min(ans, move(1, 2, BC));
    for (int i = 0; i < 3; i++)
      p[i] = backup[i];
  } else {
    ans = min(ans, move(2, 1, BC));
    for (int i = 0; i < 3; i++)
      p[i] = backup[i];
  }
  return ans;
}

int main() {
  while (cin >> n >> m, n || m) {
    for (int i = 0; i < 3; i++) {
      p[i] = stack<int>();
      p[i].push(0);
      int s;
      cin >> s;
      for (int j = 0; j < s; j++) {
        int c;
        cin >> c;
        p[i].push(c);
      }
    }
    int step = solve();
    cout << (step > m ? -1 : step) << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <deque>
#include <iostream>
#include <map>
#include <set>
using namespace std;

#define INF 1 << 28

deque<int> a, b, c;
int n, m;

inline bool finish() { return b.empty() && (a.empty() || c.empty()); }

// r: 残り移動可能回数
// prev: 前回の操作
//  1: A->B
//  2: B->A
//  3: C->B
//  4: B->C
int dfs(int r, int prev) {
  if (finish())
    return m - r;
  if (r == 0)
    return INF;
  int ret = INF;
  if (prev != 2 && !a.empty() && (b.empty() || a.back() > b.back())) {
    b.push_back(a.back());
    a.pop_back();
    ret = min(ret, dfs(r - 1, 1));
    a.push_back(b.back());
    b.pop_back();
  }
  if (prev != 1 && !b.empty() && (a.empty() || b.back() > a.back())) {
    a.push_back(b.back());
    b.pop_back();
    ret = min(ret, dfs(r - 1, 2));
    b.push_back(a.back());
    a.pop_back();
  }
  if (prev != 4 && !c.empty() && (b.empty() || c.back() > b.back())) {
    b.push_back(c.back());
    c.pop_back();
    ret = min(ret, dfs(r - 1, 3));
    c.push_back(b.back());
    b.pop_back();
  }
  if (prev != 3 && !b.empty() && (c.empty() || b.back() > c.back())) {
    c.push_back(b.back());
    b.pop_back();
    ret = min(ret, dfs(r - 1, 4));
    b.push_back(c.back());
    c.pop_back();
  }
  return ret;
}

int main() {
  while (cin >> n >> m) {
    if (n == 0 && m == 0)
      break;
    a.clear(), b.clear(), c.clear();
    int na, nb, nc;
    cin >> na;
    for (int i = 0; i < na; i++) {
      int _a;
      cin >> _a;
      a.push_back(_a);
    }
    cin >> nb;
    for (int i = 0; i < nb; i++) {
      int _b;
      cin >> _b;
      b.push_back(_b);
    }
    cin >> nc;
    for (int i = 0; i < nc; i++) {
      int _c;
      cin >> _c;
      c.push_back(_c);
    }
    int res = dfs(m, 0);
    if (res == INF)
      cout << -1 << endl;
    else
      cout << res << endl;
  }
  return 0;
}