Problem p00426 (C++) — buggy submission, verdict: Time Limit Exceeded

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

struct Data {
  int dep, lastmove[2];
  stack<int> s[3];
};
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;
    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;
    first.lastmove[0] = -1;
    first.lastmove[1] = -1;
    q.push(first);
    flag = false;
    while (!q.empty()) {
      q_c = q.front();
      q.pop();
      /*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.s[i].empty()) {
          if (i == 1) {
            cntempty = 0;
            break;
          }
          cntempty++;
        }
      }
      if (cntempty == 1) {
        printf("%d\\n", q_c.dep);
        flag = true;
        break;
      }
      q_d = q_c;
      state = 0;
      for (int i = 0; i < 3; i++) {
        while (!q_d.s[i].empty()) {
          cup = q_d.s[i].top();
          q_d.s[i].pop();
          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.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_d.lastmove[0] = i;
          q_d.lastmove[1] = nt;
          q.push(q_d);
        }
      }
    }
    if (!flag)
      printf("-1\\n");
  }
  return 0;
}

Error: TLE

Fixed submission:

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

struct Data {
  int dep;
  stack<int> s[3];
};
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].push(input);
      }
    }
    first.dep = 0;
    q.push(first);
    flag = false;
    while (!q.empty()) {
      q_c = q.front();
      q.pop();
      /*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.s[i].empty()) {
          if (i == 1) {
            cntempty = 0;
            break;
          }
          cntempty++;
        }
      }
      if (cntempty == 1) {
        printf("%d\\n", q_c.dep);
        flag = true;
        break;
      }
      q_d = q_c;
      state = 0;
      for (int i = 0; i < 3; i++) {
        while (!q_d.s[i].empty()) {
          cup = q_d.s[i].top();
          q_d.s[i].pop();
          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_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;
}