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

#include <iostream>
#include <map>
#include <queue>
#include <stack>
using namespace std;
#define F first
#define S second
const int INF = 100000000;

/*
int ans;
unsigned int n,m;

struct state{
        stack<int> s[3];
        int count,from,to;
};

void bfs(stack<int> si[3],int f,int t){
        state hoge;
        for(int i=0 ;i<3; ++i) hoge.s[i]=si[i];
        hoge.count=0;
        hoge.from=f;
        hoge.to=t;
        queue<state> que;
        que.push(hoge);
        while(!que.empty()){
                state st=que.front();
                que.pop();
                //cout << st.count << endl;
                //if(st.count>m) continue;
                if(st.s[0].size()==n||st.s[2].size()==n){
                        ans=min(ans,st.count);
                        return;
                }
                if(!(st.from==1&&st.to==0)&&st.s[0].size()!=0&&(st.s[1].size()==0||st.s[0].top()>st.s[1].top())){
                        state ns=st;
                        ns.s[1].push(ns.s[0].top());
                        ns.s[0].pop();
                        ++ns.count;
                        ns.from=0;
                        ns.to=1;
                        que.push(ns);
                }
                if(!(st.from==0&&st.to==1)&&st.s[1].size()!=0&&(st.s[0].size()==0||st.s[1].top()>st.s[0].top())){
                        state ns=st;
                        ns.s[0].push(ns.s[1].top());
                        ns.s[1].pop();
                        ++ns.count;
                        ns.from=1;
                        ns.to=0;
                        que.push(ns);
                } else
if(!(st.from==2&&st.to==1)&&st.s[1].size()!=0&&(st.s[2].size()==0||st.s[1].top()>st.s[2].top())){
                        state ns=st;
                        ns.s[2].push(ns.s[1].top());
                        ns.s[1].pop();//
                        ++ns.count;
                        ns.from=1;
                        ns.to=2;
                        que.push(ns);
                }
                if(!(st.from==1&&st.to==2)&&st.s[2].size()!=0&&(st.s[1].size()==0||st.s[2].top()>st.s[1].top())){
                        state ns=st;
                        ns.s[1].push(ns.s[2].top());
                        ns.s[2].pop();
                        ++ns.count;
                        ns.from=2;
                        ns.to=1;
                        que.push(ns);
                }
        }
}

int pow(int a,int b){
        int res=1;
        for(int i=0; i<b; ++i){
                res*=a;
        }
        return res;
}

int main(){
        int pow3[16];
        for(int i=1; i<=15; ++i){
                pow3[i]=pow(3,i)-1;
        }
        while(cin >> n >> m){
        if(n==0&&m==0) break;
                ans=INF;
                stack<int> cup[3];
                for(int i=0; i<3; ++i){
                        int q;
                        cin >> q;
                        for(int j=0; j<q; ++j){
                                int tmp;
                                cin >> tmp;
                                cup[i].push(tmp);
                        }
                }
                bfs(cup,0,1);
                if(ans!=INF)bfs(cup,1,0);
                if(ans!=INF)bfs(cup,1,2);
                if(ans!=INF)bfs(cup,2,1);
                if(ans!=INF)ans=min(ans,pow3[n]-ans);
                if(ans!=INF) cout << ans << endl;
                else cout << -1 << endl;
        }
        return 0;
}
*/

int main() {
  int n, m;
  while (cin >> n >> m) {
    if (n == 0 && m == 0)
      break;
    vector<vector<int>> goal(3);
    for (int i = 0; i < 3; ++i) {
      int q;
      cin >> q;
      for (int j = 0; j < q; ++j) {
        int tmp;
        cin >> tmp;
        goal[i].push_back(tmp - 1);
      }
    }

    vector<vector<int>> field(3);
    for (int i = 0; i < n; ++i)
      field[0].push_back(i);

    int turn = 0;
    int prev_from = -1;
    int prev_to = -1;
    while (true) {
      if (field == goal)
        break;
      ++turn;
      for (int from = 0; from < 3; ++from) {
        if (field[from].size() == 0)
          continue;
        const int cup = field[from].back();
        const int to_1 = from - 1;
        const int to_2 = from + 1;
        if (!(from == prev_to && to_1 == prev_from) && to_1 >= 0 &&
            (field[to_1].size() == 0 || field[to_1].back() < cup)) {
          field[to_1].push_back(cup);
          field[from].pop_back();
          prev_from = from;
          prev_to = to_1;
          break;
        } else if (!(from == prev_to && to_2 == prev_from) && to_2 < 3 &&
                   (field[to_2].size() == 0 || field[to_2].back() < cup)) {
          field[to_2].push_back(cup);
          field[from].pop_back();
          prev_from = from;
          prev_to = to_2;
          break;
        }
      }
    }
    int MAXTURN = 1;
    for (int i = 0; i < n; ++i)
      MAXTURN *= 3;
    --MAXTURN;
    turn = min(turn, MAXTURN - turn);
    if (turn > m)
      cout << -1 << endl;
    else
      cout << turn << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <map>
#include <queue>
#include <stack>
using namespace std;
#define F first
#define S second
const int INF = 100000000;

int ans;
unsigned int n, m;
// vector<stack<int> > goal(3);

struct state {
  stack<int> s[3];
  int count, from, to;
  /*
  state(stack<int> hoge[3]){
          for(int i=0; i<3; ++i){
                  s[i]=hoge[i];
          }
  }
  */
};

void bfs(stack<int> si[3], int f, int t) {
  state hoge;
  for (int i = 0; i < 3; ++i)
    hoge.s[i] = si[i];
  hoge.count = 0;
  hoge.from = f;
  hoge.to = t;
  queue<state> que;
  que.push(hoge);
  while (!que.empty()) {
    state st = que.front();
    que.pop();
    // cout << st.count << endl;
    // if(st.count>m) continue;
    if (st.s[0].size() == n || st.s[2].size() == n) {
      ans = min(ans, st.count);
      return;
    }
    if (!(st.from == 1 && st.to == 0) && st.s[0].size() != 0 &&
        (st.s[1].size() == 0 || st.s[0].top() > st.s[1].top())) {
      state ns = st;
      ns.s[1].push(ns.s[0].top());
      ns.s[0].pop();
      ++ns.count;
      ns.from = 0;
      ns.to = 1;
      que.push(ns);
    }
    if (!(st.from == 0 && st.to == 1) && st.s[1].size() != 0 &&
        (st.s[0].size() == 0 || st.s[1].top() > st.s[0].top())) {
      state ns = st;
      ns.s[0].push(ns.s[1].top());
      ns.s[1].pop();
      ++ns.count;
      ns.from = 1;
      ns.to = 0;
      que.push(ns);
    } else if (!(st.from == 2 && st.to == 1) && st.s[1].size() != 0 &&
               (st.s[2].size() == 0 || st.s[1].top() > st.s[2].top())) {
      state ns = st;
      ns.s[2].push(ns.s[1].top());
      ns.s[1].pop(); //
      ++ns.count;
      ns.from = 1;
      ns.to = 2;
      que.push(ns);
    }
    if (!(st.from == 1 && st.to == 2) && st.s[2].size() != 0 &&
        (st.s[1].size() == 0 || st.s[2].top() > st.s[1].top())) {
      state ns = st;
      ns.s[1].push(ns.s[2].top());
      ns.s[2].pop();
      ++ns.count;
      ns.from = 2;
      ns.to = 1;
      que.push(ns);
    }
  }
}

/*
void dfs(stack<int> s[3],int count,pair<int,int> f){
        if(count>m) return;
        if(s[0].size()==n||s[2].size()==n){
                ans=min(ans,count);
                return;
        }
        //f.F -->f.S
        //01 10 12 21
        if(!(f.F==1&&f.S==0)&&s[0].size()!=0&&(s[1].size()==0||s[0].top()>s[1].top())){
                stack<int> ns[3];
                for(int i=0; i<3; ++i) ns[i]=s[i];
                ns[1].push(ns[0].top());
                ns[0].pop();
                dfs(ns,count+1,pair<int,int>(0,1));
        }
        if(!(f.F==0&&f.S==1)&&s[1].size()!=0&&(s[0].size()==0||s[1].top()>s[0].top())){
                stack<int> ns[3];
                for(int i=0; i<3; ++i) ns[i]=s[i];
                ns[0].push(ns[1].top());
                ns[1].pop();
                dfs(ns,count+1,pair<int,int>(1,0));
        }
        if(!(f.F==2&&f.S==1)&&s[1].size()!=0&&(s[2].size()==0||s[1].top()>s[2].top())){
                stack<int> ns[3];
                for(int i=0; i<3; ++i) ns[i]=s[i];
                ns[2].push(ns[1].top());
                ns[1].pop();
                dfs(ns,count+1,pair<int,int>(1,2));
        }
        if(!(f.F==1&&f.S==2)&&s[2].size()!=0&&(s[1].size()==0||s[2].top()>s[1].top())){
                stack<int> ns[3];
                for(int i=0; i<3; ++i) ns[i]=s[i];
                ns[1].push(ns[2].top());
                ns[2].pop();
                dfs(ns,count+1,pair<int,int>(2,1));
        }
}
*/

int pow(int a, int b) {
  int res = 1;
  for (int i = 0; i < b; ++i) {
    res *= a;
  }
  return res;
}

int main() {
  int pow3[16];
  for (int i = 1; i <= 15; ++i) {
    pow3[i] = pow(3, i) - 1;
  }
  while (cin >> n >> m) {
    if (n == 0 && m == 0)
      break;
    ans = INF;
    stack<int> cup[3];
    for (int i = 0; i < 3; ++i) {
      int q;
      cin >> q;
      for (int j = 0; j < q; ++j) {
        int tmp;
        cin >> tmp;
        cup[i].push(tmp);
        // cout << cup[i].top() << endl;
      }
      // cout << endl;
    }
    /*
    dfs(cup,0,pair<int,int>(0,1));
    dfs(cup,0,pair<int,int>(1,0));
    dfs(cup,0,pair<int,int>(1,2));
    dfs(cup,0,pair<int,int>(2,1));
    */
    bfs(cup, 0, 1);
    if (ans != INF)
      bfs(cup, 1, 0);
    if (ans != INF)
      bfs(cup, 1, 2);
    if (ans != INF)
      bfs(cup, 2, 1);
    // cout << pow3[n] << endl;
    // cout << ans << endl;
    if (ans != INF)
      ans = min(ans, pow3[n] - ans);
    if (ans != INF)
      cout << ans << endl;
    else
      cout << -1 << endl;
  }
  return 0;
}