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

#include <bits/stdc++.h>
using namespace std;
/*{{{*/ // template
#define REP(i, n) for (int i = 0; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1 << 29
#define LINF LLONG_MAX / 3
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define ALL(v) (v).begin(), (v).end()
#define all(v) ALL(v)
#define debug(x) cerr << #x << ":" << x << endl
#define debug2(x, y) cerr << #x << "," << #y ":" << x << "," << y << endl
// struct fin{ fin(){ cin.tie(0); ios::sync_with_stdio(false); } } fin_;
struct Double {
  double d;
  explicit Double(double x) : d(x) {}
};
ostream &operator<<(ostream &os, const Double x) {
  os << fixed << setprecision(20) << x.d;
  return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}

constexpr ll mod = 1e9 + 7;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
/*}}}*/

class Dice {
public:
  vector<int> d;
  Dice(int u, int f) : d(6) {
    d[0] = u;
    d[1] = f;
    d[3] = 7 - u;
    d[4] = 7 - f;
    d[2] = getside(u, f);
    d[5] = 7 - d[2];
  }

  int getside(int u, int f) {
    int side[6][4] = {{2, 3, 5, 4}, {1, 4, 6, 3}, {1, 2, 6, 5},
                      {1, 5, 6, 2}, {1, 3, 6, 4}, {2, 4, 5, 3}};
    for (int i = 0; i < 4; i++) {
      if (side[u - 1][i] == f)
        return side[u - 1][(i + 1) % 4];
    }
  }

  Dice move(int n) {
    if (n == 0) {
      return Dice(d[4], d[0]);
    } else if (n == 1) {
      return Dice(d[5], d[1]);
    } else if (n == 2) {
      return Dice(d[1], d[3]);
    } else if (n == 3) {
      return Dice(d[2], d[1]);
    } else {
      while (1) {
      }
      assert(false);
    }
  }

  int get(int i) {
    if (i == 0)
      return d[1];
    else if (i == 1)
      return d[2];
    else if (i == 2)
      return d[4];
    else if (i == 3)
      return d[5];
  }
};

void fall(int x, int y, Dice dice, vvi &high, vvi &table) {
  // cerr << "Fall (" << x << "," << y << ") side : " << dice.d << endl;
  bool f = false;
  [&]() {
    for (int i = 6; i >= 4; i--)
      for (int j = 0; j < 4; j++) {
        if (i == dice.get(j) && high[x][y] - high[x + dx[j]][y + dy[j]] > 0) {
          // cerr << "Face --> i=" << i << " get(j):" << dice.get(j) << endl;
          fall(x + dx[j], y + dy[j], dice.move(j), high, table);
          f = true;
          return;
        }
      }
  }();
  if (!f) {
    high[x][y]++;
    table[x][y] = dice.d[0];
  }
}

void solve(int n) {
  vector<vector<int>> high(200, vector<int>(200, 0));
  vector<vector<int>> table(200, vector<int>(200, 0));
  const int x = 100, y = 100;

  rep(loop, n) {
    //        cerr << "loop"<<loop<<" ------------------" << endl;
    int u, f;
    cin >> u >> f;
    Dice dice(u, f);

    fall(x, y, dice, high, table);

    // cerr << "table ... " << endl;
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        // cerr << table[x+i][y+j] << " ";
      }
      // cerr << endl;
    }

    // cerr << "high ... " << endl;
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        // cerr << high[x+i][y+j] << " ";
      }
      // cerr << endl;
    }
  }

  vector<int> sum(7, 0);
  for (int i = 0; i < 200; i++)
    for (int j = 0; j < 200; j++) {
      sum[table[i][j]]++;
    }

  for (int i = 1; i < 6; i++)
    cout << sum[i] << " ";
  cout << sum[6] << endl;
}

int main() {
  int n;
  while (cin >> n) {
    if (n == 0)
      break;
    solve(n);
  }
}

Error: 0
loop0 ------------------
Fall (100,100) side : [6,4,5,1,3,2,]
table ... 
0 0 0 0 0 
0 0 0 0 0 
0 0 6 0 0 
0 0 0 0 0 
0 0 0 0 0 
high ... 
0 0 0 0 0 
0 0 0 0 0 
0 0 1 0 0 
0 0 0 0 0 
0 0 0 0 0 
loop1 ------------------
Fall (100,100) side : [6,4,5,1,3,2,]
Face --> i=5 get(j):5
Fall (100,101) side : [2,4,6,5,3,1,]
table ... 
0 0 0 0 0 
0 0 0 0 0 
0 0 6 2 0 
0 0 0 0 0 
0 0 0 0 0 
high ... 
0 0 0 0 0 
0 0 0 0 0 
0 0 1 1 0 
0 0 0 0 0 
0 0 0 0 0 
loop2 ------------------
Fall (100,100) side : [6,4,5,1,3,2,]
Face --> i=4 get(j):4
Fall (101,100) side : [3,6,5,4,1,2,]
table ... 
0 0 0 0 0 
0 0 0 0 0 
0 0 6 2 0 
0 0 3 0 0 
0 0 0 0 0 
high ... 
0 0 0 0 0 
0 0 0 0 0 
0 0 1 1 0 
0 0 1 0 0 
0 0 0 0 0 
loop3 ------------------
Fall (100,100) side : [6,4,5,1,3,2,]
table ... 
0 0 0 0 0 
0 0 0 0 0 
0 0 6 2 0 
0 0 3 0 0 
0 0 0 0 0 
high ... 
0 0 0 0 0 
0 0 0 0 0 
0 0 2 1 0 
0 0 1 0 0 
0 0 0 0 0 
loop0 ------------------
Fall (100,100) side : [5,3,6,2,4,1,]
table ... 
0 0 0 0 0 
0 0 0 0 0 
0 0 5 0 0 
0 0 0 0 0 
0 0 0 0 0 
high ... 
0 0 0 0 0 
0 0 0 0 0 
0 0 1 0 0 
0 0 0 0 0 
0 0 0 0 0 
loop1 ------------------
Fall (100,100) side : [5,3,6,2,4,1,]
Face --> i=6 get(j):6
Fall (100,101) side : [1,3,5,6,4,2,]
table ... 
0 0 0 0 0 
0 0 0 0 0 
0 0 5 1 0 
0 0 0 0 0 
0 0 0 0 0 
high ... 
0 0 0 0 0 
0 0 0 0 0 
0 0 1 1 0 
0 0 0 0 0 
0 0 0 0 0 
loop0 ------------------
Fall (100,100) side : [4,2,1,3,5,6,]
table ... 
0 0 0 0 0 
0 0 0 0 0 
0 0 4 0 0 
0 0 0 0 0 
0 0 0 0 0 
high ... 
0 0 0 0 0 
0 0 0 0 0 
0 0 1 0 0 
0 0 0 0 0 
0 0 0 0 0 
loop1 ------------------
Fall (100,100) side : [4,2,1,3,5,6,]
Face --> i=6 get(j):6
Fall (100,99) side : [1,2,3,6,5,4,]
table ... 
0 0 0 0 0 
0 0 0 0 0 
0 1 4 0 0 
0 0 0 0 0 
0 0 0 0 0 
high ... 
0 0 0 0 0 
0 0 0 0 0 
0 1 1 0 0 
0 0 0 0 0 
0 0 0 0 0 
loop2 ------------------
Fall (100,100) side : [4,2,1,3,5,6,]
Face --> i=5 get(j):5
Fall (99,100) side : [2,3,1,5,4,6,]
table ... 
0 0 0 0 0 
0 0 2 0 0 
0 1 4 0 0 
0 0 0 0 0 
0 0 0 0 0 
high ... 
0 0 0 0 0 
0 0 1 0 0 
0 1 1 0 0 
0 0 0 0 0 
0 0 0 0 0 
loop3 ------------------
Fall (100,100) side : [4,2,1,3,5,6,]
table ... 
0 0 0 0 0 
0 0 2 0 0 
0 1 4 0 0 
0 0 0 0 0 
0 0 0 0 0 
high ... 
0 0 0 0 0 
0 0 1 0 0 
0 1 2 0 0 
0 0 0 0 0 
0 0 0 0 0 
loop4 ------------------
Fall (100,100) side : [4,2,1,3,5,6,]
Face --> i=6 get(j):6
Fall (100,99) side : [1,2,3,6,5,4,]
Face --> i=5 get(j):5
Fall (99,99) side : [2,6,3,5,1,4,]
table ... 
0 0 0 0 0 
0 2 2 0 0 
0 1 4 0 0 
0 0 0 0 0 
0 0 0 0 0 
high ... 
0 0 0 0 0 
0 1 1 0 0 
0 1 2 0 0 
0 0 0 0 0 
0 0 0 0 0 
loop5 ------------------
Fall (100,100) side : [4,2,1,3,5,6,]
Face --> i=6 get(j):6
Fall (100,99) side : [1,2,3,6,5,4,]
Face --> i=4 get(j):4
Fall (100,98) side : [3,2,6,4,5,1,]
table ... 
0 0 0 0 0 
0 2 2 0 0 
3 1 4 0 0 
0 0 0 0 0 
0 0 0 0 0 
high ... 
0 0 0 0 0 
0 1 1 0 0 
1 1 2 0 0 
0 0 0 0 0 
0 0 0 0 0 
loop6 ------------------
Fall (100,100) side : [4,2,1,3,5,6,]
Face --> i=6 get(j):6
Fall (100,99) side : [1,2,3,6,5,4,]
table ... 
0 0 0 0 0 
0 2 2 0 0 
3 1 4 0 0 
0 0 0 0 0 
0 0 0 0 0 
high ... 
0 0 0 0 0 
0 1 1 0 0 
1 2 2 0 0 
0 0 0 0 0 
0 0 0 0 0 
loop7 ------------------
Fall (100,100) side : [4,2,1,3,5,6,]
Face --> i=5 get(j):5
Fall (99,100) side : [2,3,1,5,4,6,]
Face --> i=4 get(j):4
Fall (98,100) side : [3,5,1,4,2,6,]
table ... 
0 0 3 0 0 
0 2 2 0 0 
3 1 4 0 0 
0 0 0 0 0 
0 0 0 0 0 
high ... 
0 0 1 0 0 
0 1 1 0 0 
1 2 2 0 0 
0 0 0 0 0 
0 0 0 0 0 
loop0 ------------------
Fall (100,100) side : [4,6,2,3,1,5,]
table ... 
0 0 0 0 0 
0 0 0 0 0 
0 0 4 0 0 
0 0 0 0 0 
0 0 0 0 0 
high ... 
0 0 0 0 0 
0 0 0 0 0 
0 0 1 0 0 
0 0 0 0 0 
0 0 0 0 0 
loop1 ------------------
Fall (100,100) side : [1,5,4,6,2,3,]
Face --> i=5 get(j):5
Fall (101,100) side : [2,1,4,5,6,3,]
table ... 
0 0 0 0 0 
0 0 0 0 0 
0 0 4 0 0 
0 0 2 0 0 
0 0 0 0 0 
high ... 
0 0 0 0 0 
0 0 0 0 0 
0 0 1 0 0 
0 0 1 0 0 
0 0 0 0 0 
loop2 ------------------
Fall (100,100) side : [2,3,1,5,4,6,]
Face --> i=6 get(j):6
Fall (100,99) side : [1,3,5,6,4,2,]
table ... 
0 0 0 0 0 
0 0 0 0 0 
0 1 4 0 0 
0 0 2 0 0 
0 0 0 0 0 
high ... 
0 0 0 0 0 
0 0 0 0 0 
0 1 1 0 0 
0 0 1 0 0 
0 0 0 0 0 
loop3 ------------------
Fall (100,100) side : [5,3,6,2,4,1,]
Face --> i=6 get(j):6
Fall (100,101) side : [1,3,5,6,4,2,]
table ... 
0 0 0 0 0 
0 0 0 0 0 
0 1 4 1 0 
0 0 2 0 0 
0 0 0 0 0 
high ... 
0 0 0 0 0 
0 0 0 0 0 
0 1 1 1 0 
0 0 1 0 0 
0 0 0 0 0 
loop4 ------------------
Fall (100,100) side : [2,4,6,5,3,1,]
table ... 
0 0 0 0 0 
0 0 0 0 0 
0 1 2 1 0 
0 0 2 0 0 
0 0 0 0 0 
high ... 
0 0 0 0 0 
0 0 0 0 0 
0 1 2 1 0 
0 0 1 0 0 
0 0 0 0 0 
loop5 ------------------
Fall (100,100) side : [4,2,1,3,5,6,]
Face --> i=6 get(j):6
Fall (100,99) side : [1,2,3,6,5,4,]
Face --> i=5 get(j):5
Fall (99,99) side : [2,6,3,5,1,4,]
table ... 
0 0 0 0 0 
0 2 0 0 0 
0 1 2 1 0 
0 0 2 0 0 
0 0 0 0 0 
high ... 
0 0 0 0 0 
0 1 0 0 0 
0 1 2 1 0 
0 0 1 0 0 
0 0 0 0 0 


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
/*{{{*/ // template
#define REP(i, n) for (int i = 0; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1 << 29
#define LINF LLONG_MAX / 3
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define ALL(v) (v).begin(), (v).end()
#define all(v) ALL(v)
#define debug(x) cerr << #x << ":" << x << endl
#define debug2(x, y) cerr << #x << "," << #y ":" << x << "," << y << endl
// struct fin{ fin(){ cin.tie(0); ios::sync_with_stdio(false); } } fin_;
struct Double {
  double d;
  explicit Double(double x) : d(x) {}
};
ostream &operator<<(ostream &os, const Double x) {
  os << fixed << setprecision(20) << x.d;
  return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}

constexpr ll mod = 1e9 + 7;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
/*}}}*/

class Dice {
public:
  vector<int> d;
  Dice(int u, int f) : d(6) {
    d[0] = u;
    d[1] = f;
    d[3] = 7 - u;
    d[4] = 7 - f;
    d[2] = getside(u, f);
    d[5] = 7 - d[2];
  }

  int getside(int u, int f) {
    int side[6][4] = {{2, 3, 5, 4}, {1, 4, 6, 3}, {1, 2, 6, 5},
                      {1, 5, 6, 2}, {1, 3, 6, 4}, {2, 4, 5, 3}};
    for (int i = 0; i < 4; i++) {
      if (side[u - 1][i] == f)
        return side[u - 1][(i + 1) % 4];
    }
  }

  Dice move(int n) {
    if (n == 0) {
      return Dice(d[4], d[0]);
    } else if (n == 1) {
      return Dice(d[5], d[1]);
    } else if (n == 2) {
      return Dice(d[1], d[3]);
    } else if (n == 3) {
      return Dice(d[2], d[1]);
    } else {
      while (1) {
      }
      assert(false);
    }
  }

  int get(int i) {
    if (i == 0)
      return d[1];
    else if (i == 1)
      return d[2];
    else if (i == 2)
      return d[4];
    else if (i == 3)
      return d[5];
  }
};

void fall(int x, int y, Dice dice, vvi &high, vvi &table) {
  cerr << "Fall (" << x << "," << y << ") side : " << dice.d << endl;
  bool f = false;
  [&]() {
    for (int i = 6; i >= 4; i--)
      for (int j = 0; j < 4; j++) {
        if (i == dice.get(j) && high[x][y] - high[x + dx[j]][y + dy[j]] > 0) {
          cerr << "Face --> i=" << i << " get(j):" << dice.get(j) << endl;
          fall(x + dx[j], y + dy[j], dice.move(j), high, table);
          f = true;
          return;
        }
      }
  }();
  if (!f) {
    high[x][y]++;
    table[x][y] = dice.d[0];
  }
}

void solve(int n) {
  vector<vector<int>> high(200, vector<int>(200, 0));
  vector<vector<int>> table(200, vector<int>(200, 0));
  const int x = 100, y = 100;

  rep(loop, n) {
    cerr << "loop" << loop << " ------------------" << endl;
    int u, f;
    cin >> u >> f;
    Dice dice(u, f);

    fall(x, y, dice, high, table);

    cerr << "table ... " << endl;
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        cerr << table[x + i][y + j] << " ";
      }
      cerr << endl;
    }

    cerr << "high ... " << endl;
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        cerr << high[x + i][y + j] << " ";
      }
      cerr << endl;
    }
  }

  vector<int> sum(7, 0);
  for (int i = 0; i < 200; i++)
    for (int j = 0; j < 200; j++) {
      sum[table[i][j]]++;
    }

  for (int i = 1; i < 6; i++)
    cout << sum[i] << " ";
  cout << sum[6] << endl;
}

int main() {
  int n;
  while (cin >> n) {
    if (n == 0)
      break;
    solve(n);
  }
}