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

#include <iostream>
using namespace std;
struct po {
  int v, h;
};
int dice[7][4] = {
    {0, 0, 0, 0} // 0
    ,
    {3, 5, 4, 2} // 1
    ,
    {4, 6, 3, 1} // 2
    ,
    {6, 5, 1, 2} // 3
    ,
    {5, 6, 2, 1} // 4
    ,
    {3, 6, 4, 1} // 5
    ,
    {4, 5, 3, 2} // 6
};
po mp[50][50];

void check() {
  int cnt[7] = {};
  for (int i = 0; i < 50; i++)
    for (int j = 0; j < 50; j++)
      cnt[mp[i][j].v]++;

  for (int i = 1; i <= 6; i++) {
    cout << cnt[i];
    if (i != 6)
      cout << " ";
  }
  cout << endl;
}

void make(int top, int front) {
  int nx = 25, ny = 25;
  int H = mp[ny][nx].h + 1;
  int flg[7] = {};
  int dx[] = {0, 1, 0, -1}, dy[] = {-1, 0, 1, 0};
  while (1) {
    int k, fsite, maxn = -1;
    for (int i = 0; i < 4; i++) {
      if (flg[dice[top][i]] == 0 && maxn < dice[top][i])
        maxn = dice[top][i], k = i;
      if (dice[top][i] == front)
        fsite = i;
    }
    if (maxn == -1 || maxn < 4)
      break;

    int dir;
    if (fsite - k == 1 || fsite - k == -3)
      dir = 3;
    else if (fsite - k == 2 || fsite - k == -2)
      dir = 0;
    else if (fsite - k == 3 || fsite - k == -1)
      dir = 1;
    else
      dir = 2;

    if (mp[ny + dy[dir]][nx + dx[dir]].h + 1 < H) {
      nx += dx[dir], ny += dy[dir];
      if (dir == 0)
        front = 7 - top;
      if (dir == 2)
        front = top;
      top = 7 - maxn;
      H = mp[ny][nx].h + 1;
      flg[4] = flg[5] = flg[6] = 0;
    } else
      flg[maxn] = 1;
  }
  mp[ny][nx].v = top, mp[ny][nx].h = H;
}

int main() {
  while (1) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    for (int i = 0; i < 50; i++)
      for (int j = 0; j < 50; j++)
        mp[i][j].h = mp[i][j].v = 0;

    for (int q = 0; q < n; q++) {
      int top, front;
      cin >> top >> front;
      make(top, front);
    }
    check();
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;
struct po {
  int v, h;
};
int dice[7][4] = {
    {0, 0, 0, 0} // 0
    ,
    {3, 5, 4, 2} // 1
    ,
    {4, 6, 3, 1} // 2
    ,
    {6, 5, 1, 2} // 3
    ,
    {5, 6, 2, 1} // 4
    ,
    {3, 6, 4, 1} // 5
    ,
    {4, 5, 3, 1} // 6
};
po mp[50][50];

void check() {
  int cnt[7] = {};
  for (int i = 0; i < 50; i++)
    for (int j = 0; j < 50; j++)
      cnt[mp[i][j].v]++;

  for (int i = 1; i <= 6; i++) {
    cout << cnt[i];
    if (i != 6)
      cout << " ";
  }
  cout << endl;
}

void make(int top, int front) {
  int nx = 25, ny = 25;
  int H = mp[ny][nx].h + 1;
  int flg[7] = {};
  int dx[] = {0, 1, 0, -1}, dy[] = {-1, 0, 1, 0};
  while (1) {
    int k, fsite, maxn = -1;
    for (int i = 0; i < 4; i++) {
      if (flg[dice[top][i]] == 0 && maxn < dice[top][i])
        maxn = dice[top][i], k = i;
      if (dice[top][i] == front)
        fsite = i;
    }
    if (maxn == -1 || maxn < 4)
      break;

    int dir;
    if (fsite - k == 1 || fsite - k == -3)
      dir = 3;
    else if (fsite - k == 2 || fsite - k == -2)
      dir = 0;
    else if (fsite - k == 3 || fsite - k == -1)
      dir = 1;
    else if (fsite - k == 0)
      dir = 2;

    if (mp[ny + dy[dir]][nx + dx[dir]].h + 1 < H) {
      nx += dx[dir], ny += dy[dir];
      if (dir == 0)
        front = 7 - top;
      if (dir == 2)
        front = top;
      top = 7 - maxn;
      H = mp[ny][nx].h + 1;
      flg[4] = flg[5] = flg[6] = 0;
    } else
      flg[maxn] = 1;
  }
  mp[ny][nx].v = top, mp[ny][nx].h = H;
}

int main() {
  while (1) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    for (int i = 0; i < 50; i++)
      for (int j = 0; j < 50; j++)
        mp[i][j].h = mp[i][j].v = 0;

    for (int q = 0; q < n; q++) {
      int top, front;
      cin >> top >> front;
      make(top, front);
    }
    check();
  }
  return 0;
}