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

#include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
int n, m, na, nb, nc, tyo, place[16], ans;
int cup[3][16];
int c1();
int c2();
int main() {
  int ans1, ans2;

  while (1) {
    for (int i = 0; i < 3; ++i) {
      for (int j = 0; j < 16; ++j)
        cup[i][j] = 0;
    }
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    cin >> na;
    for (int i = 0; i < na; ++i)
      cin >> cup[0][i];
    cin >> nb;
    for (int i = 0; i < nb; ++i)
      cin >> cup[1][i];
    cin >> nc;
    for (int i = 0; i < nc; ++i)
      cin >> cup[2][i];
    /*
    if (cup[0][0] == 1)place[0] = 0;
    else if (cup[2][0] == 1)place[0] = 2;
    else if (cup[0][0] == 0)place[0] = 0;
    else if (cup[2][0] == 0)place[0] = 2;
    else if (cup[0][0] > cup[2][0])place[0] = 0;
    else place[0] = 2;
    */
    for (int p = 0; p < 2; ++p) {
      tyo = 0;
      if (p == 0)
        place[0] = 0;
      else
        place[0] = 2;
      int np;
      if (place[0] == 0)
        np = 2;
      else
        np = 0;
      for (int k = 1; k < 16; ++k) {
        place[k] = place[0];
      }
      bool bo = false;
      int at, ar;
      for (int i = 1; i <= n; ++i) {
        for (int j = 0; j < na; ++j) {
          if (cup[0][j] == i) {
            at = 0;
            ar = j;
            bo = true;
            break;
          }
        }
        for (int j = 0; j < nb; ++j) {
          if (bo)
            break;
          if (cup[1][j] == i) {
            at = 1;
            ar = j;
            bo = true;
            break;
          }
        }
        for (int j = 0; j < nc; ++j) {
          if (bo)
            break;
          if (cup[2][j] == i) {
            at = 2;
            ar = j;
            bo = true;
            break;
          }
        }
        bo = false;
        if (at == place[i]) {
        } else {
          if (at == 1) {
            tyo += pow(3, n - i);
            for (int k = i + 1; k < 16; ++k) {
              if (place[k] == 0)
                place[k] = 2;
              else
                place[k] = 0;
            }
          } else {
            tyo += 2 * (pow(3, n - i));
          }
        }
      }
      if (p == 0)
        ans1 = tyo;
      else
        ans2 = tyo;
    }
    if (m < min(ans1, ans2))
      cout << -1 << endl;
    else
      cout << min(ans1, ans2) << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
void input();
void solve();
void saiki(int, int, int, int, int);
void narabi();
int px, py;
int n, m, o, na, nb, nc, ans = 0, ch;
pair<int, int> at;
bool sw = false;
int place = -1;
int cup[3][10001] = {0};
pair<int, int> maxi;
int main() {
  while (1) {
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    input();
    solve();
    if (ans < m) {
      cout << ans << endl;
    } else {
      cout << -1 << endl;
    }
    ans = 0;
  }
  return 0;
}
void saiki(int i, int targetx, int targety, int tox, int toy) {
  sw = false;
  while (1) {
    if (cup[targetx][targety + 1] != 0) {
      saiki(i + 1, targetx, targety + 1, tox, toy);
    }

    if (cup[tox][toy] > cup[targetx][targety]) {
      int i = 0, count = 0;
      while (1) {
        if (cup[1][i] != 0) {
          count++;
        } else
          break;
        i++;
      }
      saiki(i + 1, targetx, targety, 1, count);
      if (i == 0) {
        sw = true;
      }
    }
    swap(cup[targetx][targety], cup[tox][toy]);
    if (targetx > tox) {
      ans += targetx - tox;
    } else {
      ans += tox - targetx;
    }
    if (sw) {
      int i = 0, count = 0;
      while (1) {
        if (cup[1][i] != 0) {
          count++;
        } else
          break;
        i++;
      }
      targetx = 1;
      targety = count - 1;
      sw = false;
    }
    if (cup[px][py] == ch || i != 0)
      break;
  }
  return;
}
void solve() {
  bool bo;
  // placeの確定
  if (cup[0][0] == 1) {
    place = 0;
  } else if (cup[2][0] == 1) {
    place = 2;
  } else if (cup[0][0] == 0) {
    place = 0;
  } else if (cup[2][0] == 0) {
    place = 2;
  } else {
    if (na > nc) {
      place = 0;
    } else
      place = 2;
  }
  int sp, u;
  if (place == 0) {
    sp = 2;
    u = nc;
  } else {
    sp = 0;
    u = na;
  }
  while (1) {
    for (int target = 1; target <= n; ++target) {
      bo = false;
      for (int i = 0; i < 3; ++i) {
        for (int j = 0; j < 10001; ++j) {
          if (cup[i][j] == target) {
            // cout<<i<<" "<<j<<endl;
            bo = true;
            at.first = i;
            at.second = j;
            break;
          }
          if (cup[i][j] == 0)
            break;
        }
        if (bo)
          break;
      }
      if (place == at.first)
        continue;
      int i = 0, count = 0;
      while (1) {
        if (cup[place][i] != 0)
          count++;
        else
          break;
        i++;
      }
      // cout<<at.first<<" "<<at.second<<" "<<place<<" "<<count<<endl;
      ch = target;
      px = place;
      py = count;
      saiki(0, at.first, at.second, place, count);
    }
    if (cup[1][0] == 0 && cup[sp][0] == 0)
      break;
  }
}
void input() {
  for (int i = 0; i < 3; ++i) {
    for (int j = 0; j < 16; ++j) {
      cup[i][j] = 0;
    }
  }
  cin >> na;
  for (int i = 0; i < na; ++i) {
    cin >> cup[0][i];
  }
  cin >> nb;
  for (int i = 0; i < nb; ++i) {
    cin >> cup[1][i];
  }
  cin >> nc;
  for (int i = 0; i < nc; ++i) {
    cin >> cup[2][i];
  }
  return;
}