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

#include <cstring>
#include <iostream>
#include <stack>
using namespace std;

int n, m, cnt;
int pos[20], tmpPos[20];

void copyPos(int *fromPos, int *toPos) {
  for (int i = 0; i < 20; i++) {
    toPos[i] = fromPos[i];
  }
}

void toC(int x);

void toA(int x) {
  if (pos[x] == 0) {
    return;
  }

  if (pos[x] == 2) {
    // xより大きいコップを全てAに移す
    for (int i = x + 1; i < n; i++) {
      toA(i);
      if (cnt > m)
        return;
    }

    // Bにコップxを移す
    cnt++;
    if (cnt > m)
      return;

    pos[x] = 1;
  }

  if (pos[x] == 1) {
    // xより大きいコップを全てCに移す
    for (int i = x + 1; i < n; i++) {
      toC(i);
      if (cnt > m)
        return;
    }

    // Aにコップxを移す
    cnt++;
    if (cnt > m)
      return;

    pos[x] = 0;
  }
}

void toC(int x) {
  if (pos[x] == 2) {
    return;
  }

  if (pos[x] == 0) {
    // xより大きいコップを全てCに移す
    for (int i = x + 1; i < n; i++) {
      toC(i);
      if (cnt > m)
        return;
    }

    // Bにコップxを移す
    cnt++;
    if (cnt > m)
      return;

    pos[x] = 1;
  }

  if (pos[x] == 1) {
    // xより大きいコップを全てAに移す
    for (int i = x + 1; i < n; i++) {
      toA(i);
      if (cnt > m)
        return;
    }

    // Cにコップxを移す
    cnt++;
    if (cnt > m)
      return;

    pos[x] = 2;
  }
}

void solve() {
  int res = m + 1;

  copyPos(pos, tmpPos);
  cnt = 0;

  for (int i = 0; i < n; i++) {
    toA(i);
    if (cnt > m)
      break;
  }

  res = min(res, cnt);
  copyPos(tmpPos, pos);
  cnt = 0;

  for (int i = 0; i < n; i++) {
    toC(i);
    if (cnt > m)
      break;
  }

  res = min(res, cnt);

  cout << (res == m + 1 ? -1 : res) << endl;
}

int main() {
  while (cin >> n >> m, n || m) {
    for (int i = 0; i < 3; i++) {
      int a;
      cin >> a;

      for (int j = 0; j < a; j++) {
        int x;
        cin >> x;
        x--;
        pos[x] = i;
      }
    }

    solve();
  }
}

Error: 0

Fixed submission:

#include <cmath>
#include <iostream>
#include <vector>
using namespace std;

int n, m, ans;
vector<vector<int>> inv, v;

void solve(int a, int b, int cost) {
  if (cost >= ans)
    return;
  if (inv == v) {
    ans = min(ans, cost);
    return;
  }

  for (int i = 0; i < 2; i++) {
    int popA = v[i].empty() ? -1 : v[i].back();
    int popB = v[i + 1].empty() ? -1 : v[i + 1].back();

    if (popA > popB && (i != b || i + 1 != a)) {
      v[i + 1].push_back(v[i].back());
      v[i].pop_back();
      solve(i, i + 1, cost + 1);
      v[i].push_back(v[i + 1].back());
      v[i + 1].pop_back();
    } else if (popB > popA && (i + 1 != b || i != a)) {
      v[i].push_back(v[i + 1].back());
      v[i + 1].pop_back();
      solve(i + 1, i, cost + 1);
      v[i + 1].push_back(v[i].back());
      v[i].pop_back();
    }
  }
}

int main(void) {
  while (cin >> n >> m && (n || m)) {
    inv.clear();
    v.clear();

    for (int i = 0; i < 3; i++) {
      vector<int> tmp;
      v.push_back(tmp);

      int r;
      cin >> r;
      while (r--) {
        int x;
        cin >> x;
        tmp.push_back(x);
      }
      inv.push_back(tmp);
    }

    for (int i = 1; i <= n; i++) {
      v[0].push_back(i);
    }

    ans = pow(3, n);
    solve(0, 0, 0);
    ans = ans == (int)pow(3, n) ? -1 : min(ans, (int)pow(3, n) - 1 - ans);
    ans = ans > m ? -1 : ans;
    cout << ans << endl;
  }

  return 0;
}