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

#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;

int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

class dice {
public:
  int surf[6];
  int t;
  int f;
  dice(){};

  vector<int> roll() {
    int direct[3] = {0};
    vector<int> res;

    for (int i = 1; i <= 4; i++) {
      // std::cout << "surf:" << surf[i] << std::endl;
      if (surf[i] >= 4) {
        direct[surf[i] - 4] = i;
      }
    }
    for (int i = 2; i >= 0; i--) {
      if (direct[i] != 0)
        res.push_back(direct[i] - 1);
    }
    return res;
  }
  void init(int top, int front) {
    t = top;
    f = front;
    surf[0] = top;
    surf[1] = front;
    surf[3] = 7 - front;
    surf[5] = 7 - top;
    if (surf[0] == 1) {
      switch (surf[1]) {
      case 2:
        surf[2] = 3;
        surf[4] = 4;
        break;
      case 3:
        surf[2] = 5;
        surf[4] = 2;
        break;
      case 5:
        surf[2] = 4;
        surf[4] = 3;
        break;
      case 4:
        surf[2] = 2;
        surf[4] = 5;
        break;
      }
    } else if (surf[0] == 2) {
      switch (surf[1]) {
      case 1:
        surf[2] = 4;
        surf[4] = 3;
        break;
      case 3:
        surf[2] = 1;
        surf[4] = 6;
        break;
      case 6:
        surf[2] = 3;
        surf[4] = 4;
        break;
      case 4:
        surf[2] = 6;
        surf[4] = 1;
        break;
      }
    } else if (surf[0] == 3) {
      switch (surf[1]) {
      case 1:
        surf[2] = 2;
        surf[4] = 5;
        break;
      case 2:
        surf[2] = 6;
        surf[4] = 1;
        break;
      case 6:
        surf[2] = 5;
        surf[4] = 2;
        break;
      case 5:
        surf[2] = 1;
        surf[4] = 6;
        break;
      }
    } else if (surf[0] == 4) {
      switch (surf[1]) {
      case 1:
        surf[2] = 5;
        surf[4] = 2;
        break;
      case 5:
        surf[2] = 6;
        surf[4] = 1;
        break;
      case 6:
        surf[2] = 2;
        surf[4] = 5;
        break;
      case 2:
        surf[2] = 1;
        surf[4] = 6;
        break;
      }
    } else if (surf[0] == 5) {
      switch (surf[1]) {
      case 1:
        surf[2] = 3;
        surf[4] = 4;
        break;
      case 3:
        surf[2] = 6;
        surf[4] = 1;
        break;
      case 6:
        surf[2] = 4;
        surf[4] = 3;
        break;
      case 4:
        surf[2] = 1;
        surf[4] = 6;
        break;
      }
    } else if (surf[0] == 6) {
      switch (surf[1]) {
      case 2:
        surf[2] = 4;
        surf[4] = 3;
        break;
      case 4:
        surf[2] = 5;
        surf[4] = 2;
        break;
      case 5:
        surf[2] = 3;
        surf[4] = 4;
        break;
      case 3:
        surf[2] = 2;
        surf[4] = 5;
        break;
      }
    }
  }
};
vector<dice> grid[222][222];

void drop(int top, int front, int x, int y) {
  // std::cout << x << " " << y << std::endl;
  dice d;
  d.init(top, front);
  grid[y][x].push_back(d);
  vector<int> direct = d.roll();
  bool flag = false;

  for (int i = 0; i < direct.size() && flag == false; i++) {
    int nx = x + dx[direct[i]], ny = y + dy[direct[i]];
    if (grid[ny][nx].size() + 1 < grid[y][x].size()) {
      flag = true;
      grid[y][x].pop_back();
      switch (direct[i]) {
      case 0:
        drop(d.surf[3], top, nx, ny);
        break;
      case 1:
        drop(d.surf[4], front, nx, ny);
        break;
      case 2:
        drop(d.surf[1], d.surf[5], nx, ny);
        break;
      case 3:
        drop(d.surf[2], front, nx, ny);
        break;
      }
    }
  }
}

int main(int argc, char *argv[]) {
  int n, top, front;
  vector<int> a;
  while (cin >> n, n) {
    for (int i = 0; i < 222; i++) {
      for (int j = 0; j < 222; j++) {
        grid[i][j].clear();
      }
    }
    for (int i = 0; i < n; i++) {
      cin >> top >> front;
      drop(top, front, 110, 110);
    }
    int ans[6] = {0};
    for (int i = 100; i < 125; i++) {
      for (int j = 100; j < 125; j++) {
        if (grid[i][j].empty()) {
          // std::cout << -1 << " ";
          continue;
        }
        // printf("%2d ", grid[i][j].back().surf[0]);
        ans[grid[i][j].back().surf[0] - 1]++;
      }
      // std::cout << std::endl;
    }
    for (int i = 0; i < 5; i++) {
      std::cout << ans[i] << " ";
    }
    std::cout << ans[5] << std::endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;

int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

class dice {
public:
  int surf[6];
  int t;
  int f;
  dice(){};

  vector<int> roll() {
    int direct[3] = {0};
    vector<int> res;

    for (int i = 1; i <= 4; i++) {
      // std::cout << "surf:" << surf[i] << std::endl;
      if (surf[i] == 4 || surf[i] || 5 || surf[i] == 6) {
        direct[surf[i] - 4] = i;
      }
    }
    for (int i = 2; i >= 0; i--) {
      if (direct[i] != 0)
        res.push_back(direct[i] - 1);
    }
    return res;
  }
  void init(int top, int front) {
    t = top;
    f = front;
    surf[0] = top;
    surf[1] = front;
    surf[3] = 7 - front;
    surf[5] = 7 - top;
    if (surf[0] == 1) {
      switch (surf[1]) {
      case 2:
        surf[2] = 3;
        surf[4] = 4;
        break;
      case 3:
        surf[2] = 5;
        surf[4] = 2;
        break;
      case 5:
        surf[2] = 4;
        surf[4] = 3;
        break;
      case 4:
        surf[2] = 2;
        surf[4] = 5;
        break;
      }
    } else if (surf[0] == 2) {
      switch (surf[1]) {
      case 1:
        surf[2] = 4;
        surf[4] = 3;
        break;
      case 3:
        surf[2] = 1;
        surf[4] = 6;
        break;
      case 6:
        surf[2] = 3;
        surf[4] = 4;
        break;
      case 4:
        surf[2] = 6;
        surf[4] = 1;
        break;
      }
    } else if (surf[0] == 3) {
      switch (surf[1]) {
      case 1:
        surf[2] = 2;
        surf[4] = 5;
        break;
      case 2:
        surf[2] = 6;
        surf[4] = 1;
        break;
      case 6:
        surf[2] = 5;
        surf[4] = 2;
        break;
      case 5:
        surf[2] = 1;
        surf[4] = 6;
        break;
      }
    } else if (surf[0] == 4) {
      switch (surf[1]) {
      case 1:
        surf[2] = 5;
        surf[4] = 2;
        break;
      case 5:
        surf[2] = 6;
        surf[4] = 1;
        break;
      case 6:
        surf[2] = 2;
        surf[4] = 5;
        break;
      case 2:
        surf[2] = 1;
        surf[4] = 6;
        break;
      }
    } else if (surf[0] == 5) {
      switch (surf[1]) {
      case 1:
        surf[2] = 3;
        surf[4] = 4;
        break;
      case 3:
        surf[2] = 6;
        surf[4] = 1;
        break;
      case 6:
        surf[2] = 4;
        surf[4] = 3;
        break;
      case 4:
        surf[2] = 1;
        surf[4] = 6;
        break;
      }
    } else if (surf[0] == 6) {
      switch (surf[1]) {
      case 2:
        surf[2] = 4;
        surf[4] = 3;
        break;
      case 4:
        surf[2] = 5;
        surf[4] = 2;
        break;
      case 5:
        surf[2] = 3;
        surf[4] = 4;
        break;
      case 3:
        surf[2] = 2;
        surf[4] = 5;
        break;
      }
    }
  }
};
vector<dice> grid[222][222];

void drop(int top, int front, int x, int y) {
  // std::cout << x << " " << y << std::endl;
  dice d;
  d.init(top, front);
  grid[y][x].push_back(d);
  vector<int> direct = d.roll();
  bool flag = false;
  // for (int i = 0; i < direct.size(); i++) {
  //   std::cout << direct[i] << " ";
  // }
  //  std::cout << std::endl;
  for (int i = 0; i < direct.size() && flag == false; i++) {
    int nx = x + dx[direct[i]], ny = y + dy[direct[i]];
    if (grid[ny][nx].size() + 1 < grid[y][x].size()) {
      flag = true;
      grid[y][x].pop_back();
      switch (direct[i]) {
      case 0:
        drop(d.surf[3], top, nx, ny);
        break;
      case 1:
        drop(d.surf[4], front, nx, ny);
        break;
      case 2:
        drop(d.surf[1], d.surf[5], nx, ny);
        break;
      case 3:
        drop(d.surf[2], front, nx, ny);
        break;
      }
    }
    if (flag)
      break;
  }
}

int main(int argc, char *argv[]) {
  int n, top, front;
  while (cin >> n, n) {
    memset(grid, 0, sizeof(grid));
    for (int i = 0; i < n; i++) {
      cin >> top >> front;
      drop(top, front, 101, 101);
    }
    int ans[6] = {0};
    for (int i = 0; i < 205; i++) {
      for (int j = 0; j < 205; j++) {
        if (grid[i][j].empty()) {
          // std::cout << -1 << " ";
          continue;
        }
        // std::cout << grid[i][j].first << " ";
        ans[grid[i][j].back().surf[0] - 1]++;
      }
      // std::cout << std::endl;
    }
    for (int i = 0; i < 5; i++) {
      std::cout << ans[i] << " ";
    }
    std::cout << ans[5] << std::endl;
  }

  return 0;
}