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

#include <cstdio>
#include <iostream>
#include <queue>
#include <stack>
using namespace std;

struct Data {
  int dep, lastmove[2], s[3][15], top[15];
  Data() {
    for (int i = 0; i < 3; i++) {
      top[i] = -1;
    }
  }
};
bool memo[14348908] = {};

int int_pow(int n, int m) {
  int ans = 1;
  while (m--) {
    ans *= n;
  }
  return ans;
}

int main() {
  int hoge, n;
  while (cin >> hoge >> n, n || hoge) {
    for (int i = 0; i < 14348908; i++)
      memo[i] = false;
    Data q_c, q_d, first;
    int m, input, nt, cup, cntempty, state, nowdep;
    bool flag;
    int situ[2] = {-1, 1};
    queue<Data> q;
    for (int i = 0; i < 3; i++) {
      cin >> m;
      for (int j = 0; j < m; j++) {
        cin >> input;
        first.s[i][first.top[i] + 1] = input;
        first.top[i]++;
      }
    }
    first.dep = 0;
    first.lastmove[0] = -1;
    first.lastmove[1] = -1;
    q.push(first);
    flag = false;
    while (!q.empty()) {
      q_c = q.front();
      q.pop();
      // printf("%d\\n",q_c.dep);
      if (q_c.dep % 1000 == 0) {
        // printf("%d\\n",q_c.dep);
      }
      // for(int i=0;i<3;i++) printf("%d ",!q_c.s[i].empty());
      // puts("");
      if (q_c.dep == n)
        break;
      cntempty = 0;
      for (int i = 0; i < 3; i++) {
        if (q_c.top[i] != -1) {
          if (i == 1) {
            cntempty = 0;
            break;
          }
          cntempty++;
        }
      }
      if (cntempty == 1) {
        printf("%d\\n", q_c.dep);
        flag = true;
        break;
      }
      state = 0;
      for (int i = 0; i < 3; i++) {
        for (int j = 0; j <= q_c.top[i]; j++) {
          cup = q_c.s[i][j];
          state += i * int_pow(3, cup - 1);
        }
      }
      if (memo[state])
        continue;
      memo[state] = true;
      // printf("%d\\n",state);
      for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 2; j++) {
          nt = i + situ[j];
          if (nt < 0 || 3 <= nt)
            continue;
          q_d = q_c;
          if ((q_c.lastmove[1] == i && q_c.lastmove[0] == nt) ||
              q_d.top[i] == -1)
            continue;
          cup = q_d.s[i][q_d.top[i]];
          q_d.top[i]--;
          if (q_d.top[nt] != -1 && cup < q_d.s[nt][q_d.top[nt]])
            continue;
          q_d.s[nt][q_d.top[nt] + 1] = cup;
          q_d.top[nt]++;
          q_d.dep++;
          q_d.lastmove[0] = i;
          q_d.lastmove[1] = nt;
          q.push(q_d);
        }
      }
    }
    if (!flag)
      printf("-1\\n");
  }
  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <iostream>
#include <queue>
#include <stack>
using namespace std;

struct Data {
  int dep;
  stack<int> s[3];
};

int main() {
  int hoge, n;
  while (cin >> hoge >> n, n || hoge) {
    Data q_c, q_d, first;

    int m, input, nt, cup, cntempty;

    bool flag;
    int situ[2] = {-1, 1};
    queue<Data> q;
    for (int i = 0; i < 3; i++) {
      cin >> m;
      for (int j = 0; j < m; j++) {
        cin >> input;
        first.s[i].push(input);
      }
    }
    first.dep = 0;
    q.push(first);
    flag = false;
    while (!q.empty()) {
      q_c = q.front();
      q.pop();
      // for(int i=0;i<3;i++) printf("%d ",!q_c.s[i].empty());
      // puts("");
      if (q_c.dep == n)
        break;
      cntempty = 0;
      for (int i = 0; i < 3; i++) {
        if (!q_c.s[i].empty()) {
          if (i == 1) {
            cntempty = 0;
            break;
          }
          cntempty++;
        }
      }
      if (cntempty == 1) {
        printf("%d\\n", q_c.dep);
        flag = true;
        break;
      }
      for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 2; j++) {
          nt = i + situ[j];
          if (nt < 0 || 3 <= nt)
            continue;
          q_d = q_c;
          if (q_d.s[i].empty())
            continue;
          cup = q_d.s[i].top();
          q_d.s[i].pop();
          if (!q_d.s[nt].empty() && cup < q_d.s[nt].top())
            continue;
          q_d.s[nt].push(cup);
          q_d.dep++;
          q.push(q_d);
        }
      }
    }
    if (!flag)
      printf("-1\\n");
  }
  return 0;
}