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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-8;
const int inf = 1e8;
int f[6][6] = {{-1, 3, 5, 2, 4, -1}, {4, -1, 1, 6, -1, 3},
               {2, 6, -1, -1, 1, 5}, {5, 1, -1, -1, 6, 2},
               {3, -1, 6, 1, -1, 4}, {-1, 4, 2, 5, 3, -1}};
int main() {
  int n;
  while (cin >> n, n) {
    vvp in(110, vp(110));
    rep(i, n) {
      int a, b;
      cin >> a >> b;
      if (!i) {
        in[55][55] = pii(a, 1);
        continue;
      }
      int c = f[a - 1][b - 1];
      int x = 55, y = 55;
      while (1) {
        // cout<<a<<" "<<b<<" "<<c<<endl;
        // cout<<x<<" "<<y<<endl;
        int ma = -1;
        if (in[x + 1][y].second < in[x][y].second && b > 3)
          ma = max(ma, b);
        if (in[x][y + 1].second < in[x][y].second && c > 3)
          ma = max(ma, c);
        if (in[x - 1][y].second < in[x][y].second && 7 - b > 3)
          ma = max(ma, 7 - b);
        if (in[x][y - 1].second < in[x][y].second && 7 - c > 3)
          ma = max(ma, 7 - c);
        if (ma == -1)
          break;
        int na, nb, nc;
        if (b == ma) {
          na = 7 - b, nb = a, nc = c;
          x++;
        } else if (c == ma) {
          na = 7 - c, nb = b, nc = a;
          y++;
        } else if (7 - b == ma) {
          na = b, nb = 7 - a, nc = c;
          x--;
        } else {
          na = c, nb = b, nc = 7 - a;
          y--;
        }
        a = na;
        b = nb;
        c = nc;
      }
      in[x][y].second++;
      in[x][y].first = a;

      /*      vi out(7);
      rep(k,110)rep(j,110)out[in[k][j].first]++;
      rep(k,5)cout<<out[k+1]<<" ";
      cout<<out[6]<<endl;*/
    }
    vi out(7);
    rep(i, 110) rep(j, 110) out[in[i][j].first]++;
    rep(i, 5) cout << out[i + 1] << " ";
    cout << out[6] << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-8;
const int inf = 1e8;
int f[6][6] = {{-1, 3, 5, 4, 2, -1}, {4, -1, 1, 6, -1, 3},
               {2, 6, -1, -1, 1, 5}, {5, 1, -1, -1, 6, 2},
               {3, -1, 6, 1, -1, 4}, {-1, 4, 2, 5, 3, -1}};
int main() {
  int n;
  while (cin >> n, n) {
    vvp in(110, vp(110));
    rep(i, n) {
      int a, b;
      cin >> a >> b;
      if (!i) {
        in[55][55] = pii(a, 1);
        continue;
      }
      int c = f[a - 1][b - 1];
      int x = 55, y = 55;
      while (1) {
        // cout<<a<<" "<<b<<" "<<c<<endl;
        //	cout<<x<<" "<<y<<endl;
        int ma = -1;
        if (in[x + 1][y].second < in[x][y].second && b > 3)
          ma = max(ma, b);
        if (in[x][y + 1].second < in[x][y].second && c > 3)
          ma = max(ma, c);
        if (in[x - 1][y].second < in[x][y].second && 7 - b > 3)
          ma = max(ma, 7 - b);
        if (in[x][y - 1].second < in[x][y].second && 7 - c > 3)
          ma = max(ma, 7 - c);
        if (ma == -1)
          break;
        int na, nb, nc;
        if (b == ma) {
          na = 7 - b, nb = a, nc = c;
          x++;
        } else if (c == ma) {
          na = 7 - c, nb = b, nc = a;
          y++;
        } else if (7 - b == ma) {
          na = b, nb = 7 - a, nc = c;
          x--;
        } else {
          na = c, nb = b, nc = 7 - a;
          y--;
        }
        a = na;
        b = nb;
        c = nc;
      }
      in[x][y].second++;
      in[x][y].first = a;

      /*      vi out(7);
      rep(k,110)rep(j,110)out[in[k][j].first]++;
      rep(k,5)cout<<out[k+1]<<" ";
      cout<<out[6]<<endl;*/
    }
    vi out(7);
    rep(i, 110) rep(j, 110) out[in[i][j].first]++;
    rep(i, 5) cout << out[i + 1] << " ";
    cout << out[6] << endl;
  }
}