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

#include <cstdio>
#include <iostream>
#include <vector>

using namespace std;

typedef pair<int, int> P;

struct dice {
  int front;
  int back;
  int right;
  int left;
  int top;
  int bottom;
};

void turnf(dice *d) {
  int k = (*d).front;
  (*d).front = (*d).top;
  (*d).top = (*d).back;
  (*d).back = (*d).bottom;
  (*d).bottom = k;
}

void turnb(dice *d) {
  int k = (*d).front;
  (*d).front = (*d).bottom;
  (*d).bottom = (*d).back;
  (*d).back = (*d).top;
  (*d).top = k;
}

void turnr(dice *d) {
  int k = (*d).top;
  (*d).top = (*d).left;
  (*d).left = (*d).bottom;
  (*d).bottom = (*d).right;
  (*d).right = k;
}

void turnl(dice *d) {
  int k = (*d).top;
  (*d).top = (*d).right;
  (*d).right = (*d).bottom;
  (*d).bottom = (*d).left;
  (*d).left = k;
}

void turnturn(dice *d) {
  int k = (*d).right;
  (*d).right = (*d).front;
  (*d).front = (*d).left;
  (*d).left = (*d).back;
  (*d).back = k;
}

void dicedice(dice *d) {
  dice dd;
  dd.top = 1;
  dd.bottom = 6;
  dd.front = 3;
  dd.back = 4;
  dd.right = 2;
  dd.left = 5;
  for (int i = 0; i < 4; i++) {
    if ((*d).top == dd.top) {
      break;
    } else {
      turnf(&dd);
    }
  }
  for (int i = 0; i < 4; i++) {
    if ((*d).top == dd.top) {
      break;
    } else {
      turnr(&dd);
    }
  }
  for (int i = 0; i < 4; i++) {
    if ((*d).front == dd.front) {
      break;
    } else {
      turnturn(&dd);
    }
  }
  (*d).right = dd.right;
  (*d).left = dd.left;
}

int main() {
  int n;
  while (cin >> n) {
    if (n == 0) {
      break;
    }
    int t, f;
    vector<vector<P>> data(250, vector<P>(250, P(0, 0)));
    int sx = 125;
    int sy = 125;
    int dx[] = {1, 0, -1, 0};
    int dy[] = {0, 1, 0, -1};
    for (int i = 0; i < n; i++) {
      cin >> t >> f;
      dice d;
      d.top = t;
      d.bottom = 7 - t;
      d.front = f;
      d.back = 7 - f;
      dicedice(&d);
      // printf("%d %d %d %d %d %d\\n", d.top, d.bottom, d.front, d.back,
      // d.right, d.left);
      int x, y;
      x = sx;
      y = sy;
      while (true) {
        bool h = true;
        int kp = 0;
        int k = -1;
        int hh = data[y][x].second + 1;
        for (int j = 0; j < 4; j++) {
          if (data[y + dy[j]][x + dx[j]].second < hh - 1) {
            if (j == 0) {
              if (d.left > 3) {
                h = false;
                if (d.left > kp) {
                  kp = d.left;
                  k = j;
                }
              }
            } else if (j == 1) {
              if (d.back > 3) {
                h = false;
                if (d.back > kp) {
                  kp = d.back;
                  k = j;
                }
              }
            } else if (j == 2) {
              if (d.right > 3) {
                h = false;
                if (d.right > kp) {
                  kp = d.right;
                  k = j;
                }
              }
            } else {
              if (d.front > 3) {
                h = false;
                if (d.front > kp) {
                  kp = d.front;
                  k = j;
                }
              }
            }
          }
        }
        if (h) {
          data[y][x].first = d.top;
          data[y][x].second++;
          // cout << y << ' ' << x << ' ' << d.top << endl;
          break;
        } else {
          x += dx[k];
          y += dy[k];
          if (k == 0) {
            turnl(&d);
          } else if (k == 1) {
            turnb(&d);
          } else if (k == 2) {
            turnr(&d);
          } else {
            turnf(&d);
          }
        }
      }
    }
    int ddd[7] = {0};
    for (int i = 0; i < 250; i++) {
      for (int j = 0; j < 250; j++) {
        if (data[i][j].first != 0) {
          ddd[data[i][j].first]++;
        }
      }
    }
    /*cout << 1;
    for (int i = 1; i < 6; i++) {
            cout << ' ' << i+1;
    }*/
    cout << ddd[1];
    for (int i = 1; i < 6; i++) {
      cout << ' ' << ddd[i + 1];
    }
    cout << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <cstdio>
#include <iostream>
#include <vector>

using namespace std;

typedef pair<int, int> P;

struct dice {
  int front;
  int back;
  int right;
  int left;
  int top;
  int bottom;
};

void turnf(dice *d) {
  int k = (*d).front;
  (*d).front = (*d).top;
  (*d).top = (*d).back;
  (*d).back = (*d).bottom;
  (*d).bottom = k;
}

void turnb(dice *d) {
  int k = (*d).front;
  (*d).front = (*d).bottom;
  (*d).bottom = (*d).back;
  (*d).back = (*d).top;
  (*d).top = k;
}

void turnr(dice *d) {
  int k = (*d).top;
  (*d).top = (*d).left;
  (*d).left = (*d).bottom;
  (*d).bottom = (*d).right;
  (*d).right = k;
}

void turnl(dice *d) {
  int k = (*d).top;
  (*d).top = (*d).right;
  (*d).right = (*d).bottom;
  (*d).bottom = (*d).left;
  (*d).left = k;
}

void turnturn(dice *d) {
  int k = (*d).right;
  (*d).right = (*d).front;
  (*d).front = (*d).left;
  (*d).left = (*d).back;
  (*d).back = k;
}

void dicedice(dice *d) {
  dice dd;
  dd.top = 1;
  dd.bottom = 6;
  dd.front = 3;
  dd.back = 4;
  dd.right = 2;
  dd.left = 5;
  for (int i = 0; i < 4; i++) {
    if ((*d).top == dd.top) {
      break;
    } else {
      turnf(&dd);
    }
  }
  for (int i = 0; i < 4; i++) {
    if ((*d).top == dd.top) {
      break;
    } else {
      turnr(&dd);
    }
  }
  for (int i = 0; i < 4; i++) {
    if ((*d).front == dd.front) {
      break;
    } else {
      turnturn(&dd);
    }
  }
  (*d).right = dd.right;
  (*d).left = dd.left;
}

int main() {
  int n;
  while (cin >> n) {
    if (n == 0) {
      break;
    }
    int t, f;
    vector<vector<P>> data(250, vector<P>(250, P(0, 0)));
    int sx = 125;
    int sy = 125;
    int dx[] = {1, 0, -1, 0};
    int dy[] = {0, 1, 0, -1};
    for (int i = 0; i < n; i++) {
      cin >> t >> f;
      dice d;
      d.top = t;
      d.bottom = 7 - t;
      d.front = f;
      d.back = 7 - f;
      dicedice(&d);
      // printf("%d %d %d %d %d %d\\n", d.top, d.bottom, d.front, d.back,
      // d.right, d.left);
      int x, y;
      x = sx;
      y = sy;
      while (true) {
        bool h = true;
        int kp = 0;
        int k = -1;
        int hh = data[y][x].second + 1;
        for (int j = 0; j < 4; j++) {
          if (data[y + dy[i]][x + dx[i]].second < hh - 1) {
            if (i == 0) {
              if (d.left > 3) {
                h = false;
                if (d.left > kp) {
                  kp = d.left;
                  k = i;
                }
              }
            } else if (i == 1) {
              if (d.back > 3) {
                h = false;
                if (d.back > kp) {
                  kp = d.back;
                  k = i;
                }
              }
            } else if (i == 2) {
              if (d.right > 3) {
                h = false;
                if (d.right > kp) {
                  kp = d.right;
                  k = i;
                }
              }
            } else {
              if (d.front > 3) {
                h = false;
                if (d.front > kp) {
                  kp = d.front;
                  k = i;
                }
              }
            }
          }
        }
        if (h) {
          data[y][x].first = d.top;
          data[y][x].second++;
          break;
        } else {
          x += dx[k];
          y += dy[k];
          if (k == 0) {
            turnl(&d);
          } else if (k == 1) {
            turnb(&d);
          } else if (k == 2) {
            turnr(&d);
          } else {
            turnf(&d);
          }
        }
      }
    }
    int ddd[7] = {0};
    for (int i = 0; i < 250; i++) {
      for (int j = 0; j < 250; j++) {
        if (data[i][j].first != 0) {
          ddd[data[i][j].first]++;
        }
      }
    }
    /*cout << 1;
    for (int i = 1; i < 6; i++) {
            cout << ' ' << i+1;
    }*/
    cout << ddd[1];
    for (int i = 1; i < 6; i++) {
      cout << ' ' << ddd[i + 1];
    }
    cout << endl;
  }
  return 0;
}