Problem p00762 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
int n;
int dy[] = {1, 0, -1, 0};
int dx[] = {0, 1, 0, -1};
int t[300][300], u[300][300];

void roll(int d[], int dir) {
  int tmp[6], z;
  for (int i = 1; i <= 6; i++) {
    tmp[d[i]] = i;
  }

  if (dir == 0) {
    z = tmp[0];
    tmp[0] = tmp[3];
    tmp[3] = tmp[5];
    tmp[5] = tmp[1];
    tmp[1] = z;
    for (int i = 0; i < 6; i++)
      d[tmp[i]] = i;
  } else if (dir == 1) {
    z = tmp[0];
    tmp[0] = tmp[4];
    tmp[4] = tmp[5];
    tmp[5] = tmp[2];
    tmp[2] = z;
    for (int i = 0; i < 6; i++)
      d[tmp[i]] = i;
  } else if (dir == 2) {
    roll(d, 0);
    roll(d, 0);
    roll(d, 0);
  } else if (dir == 3) {
    roll(d, 1);
    roll(d, 1);
    roll(d, 1);
  }
}

void rec(int y, int x, int d[]) {
  int dir, ny, nx;

  for (int i = 6; i >= 4; i--) {
    dir = d[i] - 1;
    ny = y + dy[dir], nx = x + dx[dir];
    if (dir < 0 || 4 <= dir)
      continue;
    if (ny < 0 || nx < 0)
      continue;
    if (ny > 201 || nx > 201)
      continue;

    if (u[ny][nx] < u[y][x]) {
      roll(d, dir);
      rec(ny, nx, d);
      return;
    }
  }

  for (int i = 1; i <= 6; i++)
    if (d[i] == 0)
      t[y][x] = i;
  u[y][x]++;
}

void init() {
  for (int i = 0; i < 300; i++)
    for (int j = 0; j < 300; j++)
      t[i][j] = u[i][j] = 0;
}

void printf() {
  cout << "=============================" << endl;
  for (int i = 147; i <= 152; i++) {
    for (int j = 147; j <= 152; j++) {
      cout << t[i][j] << ' ';
    }
    cout << endl;
  }
  cout << "=============================" << endl;
}
int main() {
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    init();
    for (int i = 0; i < n; i++) {
      int a, b, c;
      cin >> a >> b;
      if (a == 1) {
        if (b == 2)
          c = 3;
        else if (b == 3)
          c = 5;
        else if (b == 4)
          c = 2;
        else if (b == 5)
          c = 4;
      } else if (a == 2) {
        if (b == 1)
          c = 4;
        else if (b == 3)
          c = 1;
        else if (b == 4)
          c = 6;
        else if (b == 6)
          c = 3;
      } else if (a == 3) {
        if (b == 1)
          c = 2;
        else if (b == 2)
          c = 6;
        else if (b == 5)
          c = 1;
        else if (b == 6)
          c = 5;
      } else if (a == 4) {
        if (b == 1)
          c = 5;
        else if (b == 2)
          c = 1;
        else if (b == 5)
          c = 6;
        else if (b == 6)
          c = 1;
      } else if (a == 5) {
        if (b == 1)
          c = 3;
        else if (b == 3)
          c = 6;
        else if (b == 4)
          c = 1;
        else if (b == 6)
          c = 4;
      } else {
        if (b == 2)
          c = 4;
        else if (b == 3)
          c = 2;
        else if (b == 4)
          c = 5;
        else if (b == 5)
          c = 3;
      }

      int d[7];
      //  cout<<a<<b<<c<<endl;
      d[a] = 0;
      d[b] = 1;
      d[c] = 2;
      d[7 - b] = 3;
      d[7 - c] = 4;
      d[7 - a] = 5;
      rec(150, 150, d);

      // printf();
    }
    int ans[7] = {};

    for (int i = 0; i < 300; i++)
      for (int j = 0; j < 300; j++)
        ans[t[i][j]]++;

    for (int i = 0; i < 6; i++) {
      if (i)
        cout << ' ';
      cout << ans[i + 1];
    }
    cout << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
int n;
int dy[] = {1, 0, -1, 0};
int dx[] = {0, 1, 0, -1};
int t[300][300], u[300][300];

void roll(int d[], int dir) {
  int tmp[6], z;
  for (int i = 1; i <= 6; i++) {
    tmp[d[i]] = i;
  }

  if (dir == 0) {
    z = tmp[0];
    tmp[0] = tmp[3];
    tmp[3] = tmp[5];
    tmp[5] = tmp[1];
    tmp[1] = z;
    for (int i = 0; i < 6; i++)
      d[tmp[i]] = i;
  } else if (dir == 1) {
    z = tmp[0];
    tmp[0] = tmp[4];
    tmp[4] = tmp[5];
    tmp[5] = tmp[2];
    tmp[2] = z;
    for (int i = 0; i < 6; i++)
      d[tmp[i]] = i;
  } else if (dir == 2) {
    roll(d, 0);
    roll(d, 0);
    roll(d, 0);
  } else if (dir == 3) {
    roll(d, 1);
    roll(d, 1);
    roll(d, 1);
  }
}

void rec(int y, int x, int d[]) {
  int dir, ny, nx;

  for (int i = 6; i >= 4; i--) {
    dir = d[i] - 1;
    ny = y + dy[dir], nx = x + dx[dir];
    if (dir < 0 || 4 <= dir)
      continue;
    if (ny < 0 || nx < 0)
      continue;
    if (ny > 201 || nx > 201)
      continue;

    if (u[ny][nx] < u[y][x]) {
      roll(d, dir);
      rec(ny, nx, d);
      return;
    }
  }

  for (int i = 1; i <= 6; i++)
    if (d[i] == 0)
      t[y][x] = i;
  u[y][x]++;
}

void init() {
  for (int i = 0; i < 300; i++)
    for (int j = 0; j < 300; j++)
      t[i][j] = u[i][j] = 0;
}

void printf() {
  cout << "=============================" << endl;
  for (int i = 147; i <= 152; i++) {
    for (int j = 147; j <= 152; j++) {
      cout << t[i][j] << ' ';
    }
    cout << endl;
  }
  cout << "=============================" << endl;
}
int main() {
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    init();
    for (int i = 0; i < n; i++) {
      int a, b, c;
      cin >> a >> b;
      if (a == 1) {
        if (b == 2)
          c = 3;
        else if (b == 3)
          c = 5;
        else if (b == 4)
          c = 2;
        else if (b == 5)
          c = 4;
      } else if (a == 2) {
        if (b == 1)
          c = 4;
        else if (b == 3)
          c = 1;
        else if (b == 4)
          c = 6;
        else if (b == 6)
          c = 3;
      } else if (a == 3) {
        if (b == 1)
          c = 2;
        else if (b == 2)
          c = 6;
        else if (b == 5)
          c = 1;
        else if (b == 6)
          c = 5;
      } else if (a == 4) {
        if (b == 1)
          c = 5;
        else if (b == 2)
          c = 1;
        else if (b == 5)
          c = 6;
        else if (b == 6)
          c = 2;
      } else if (a == 5) {
        if (b == 1)
          c = 3;
        else if (b == 3)
          c = 6;
        else if (b == 4)
          c = 1;
        else if (b == 6)
          c = 4;
      } else {
        if (b == 2)
          c = 4;
        else if (b == 3)
          c = 2;
        else if (b == 4)
          c = 5;
        else if (b == 5)
          c = 3;
      }

      int d[7];
      //  cout<<a<<b<<c<<endl;
      d[a] = 0;
      d[b] = 1;
      d[c] = 2;
      d[7 - b] = 3;
      d[7 - c] = 4;
      d[7 - a] = 5;
      rec(150, 150, d);

      // printf();
    }
    int ans[7] = {};

    for (int i = 0; i < 300; i++)
      for (int j = 0; j < 300; j++)
        ans[t[i][j]]++;

    for (int i = 0; i < 6; i++) {
      if (i)
        cout << ' ';
      cout << ans[i + 1];
    }
    cout << endl;
  }
  return 0;
}