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

#include <cstdio>
#include <iostream>
#include <string.h>
using namespace std;

int main() {
  // black 1
  // white 2
  int board[7][7][7];
  int height[7][7];
  int N, M, P;
  while (cin >> N >> M >> P && N != 0 || M != 0 || P != 0) {
    memset(board, -1, sizeof(board));
    memset(height, 0, sizeof(height));
    int ansColor = -1;
    int ansP = -1;
    int a = 0;
    bool flag = false;
    for (a = 0; a < P; a++) {
      int X, Y;
      cin >> X >> Y;
      X--;
      Y--;
      if (a % 2 == 0) {
        board[Y][X][height[Y][X]] = 1;
      } else {
        board[Y][X][height[Y][X]] = 2;
      }

      int color = board[Y][X][height[Y][X]];
      for (int dx = -1; dx <= 1; dx++) {
        for (int dy = -1; dy <= 1; dy++) {
          for (int dz = -1; dz <= 1; dz++) {
            if (dx == 0 && dy == 0 && dz == 0)
              continue;
            int nx = X + dx;
            int ny = Y + dy;
            int nz = height[Y][X] + dz;
            int cnt = 1;

            while (nx >= 0 && nx < N && ny >= 0 && ny < N && nz >= 0 &&
                   nz < N) {
              if (board[ny][nx][nz] != color)
                break;
              cnt++;
              nx += dx;
              ny += dy;
              nz += dz;
            }

            nx = X - dx;
            ny = Y - dy;
            nz = height[Y][X] - dz;
            while (nx >= 0 && nx < N && ny >= 0 && ny < N && nz >= 0 &&
                   nz < N) {
              if (board[ny][nx][nz] != color)
                break;
              cnt++;
              nx -= dx;
              ny -= dy;
              nz -= dz;
            }

            if (cnt >= M && !flag) {
              ansP = a + 1;
              ansColor = color;
              flag = true;
              break;
            }
          }
          if (flag)
            break;
        }
        if (flag)
          break;
      }
      if (flag)
        break;
      height[Y][X]++;
    }
    int X, Y;
    for (int b = a + 1; b < P; b++)
      cin >> X >> Y;
    if (ansColor == 1) {
      cout << "Black " << ansP << endl;
    } else if (ansColor == 2) {
      cout << "White " << ansP << endl;
    } else {
      cout << "Draw" << endl;
    }
  }
}

Error: WA

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
int dx[] = {0, 1, -1};
int dy[] = {0, 1, -1};
int dz[] = {0, 1, -1};

int main() {
  int n, m, p;
  while (cin >> n >> m >> p && n != 0 || m != 0 || p != 0) {
    int board[8][8][8];
    int height[8][8];
    for (int i = 0; i < 8; i++) {
      for (int j = 0; j < 8; j++) {
        for (int k = 0; k < 8; k++) {
          board[i][j][k] = 0;
        }
      }
    }

    for (int i = 0; i < 8; i++) {
      for (int j = 0; j < 8; j++) {
        height[i][j] = 0;
      }
    }
    int ansColor = 0;
    int ansNumber = 0;
    bool flag = false;
    for (int i = 0; i < p; i++) {
      int x, y;
      cin >> x >> y;
      int z = height[y][x];
      if (i % 2 == 0) {
        board[y][x][z] = 1;
      } else {
        board[y][x][z] = 2;
      }

      for (int a = 0; a < 3; a++) {
        for (int b = 0; b < 3; b++) {
          for (int c = 0; c < 3; c++) {
            if (a == 0 && b == 0 && c == 0)
              continue;
            int vx = x + dx[a];
            int vy = y + dy[b];
            int vz = z + dz[c];
            int cnt = 0;
            int pre = 2; // white
            if (i % 2 == 0) {
              pre = 1; // black
            }
            while (vx >= 0 && vy >= 0 && vz >= 0 && vx <= n && vy <= n &&
                   vz <= n && pre == board[vy][vx][vz]) {
              cnt++;
              vx += dx[a];
              vy += dy[b];
              vz += dz[c];
            }

            if (cnt >= m - 1) {
              ansColor = board[y][x][z];
              ansNumber = i + 1;
              flag = true;
              break;
            }
          }
          if (flag)
            break;
        }
        if (flag)
          break;
      }
      if (flag)
        break;
      height[y][x]++;
    }
    if (ansColor == 0) {
      cout << "Draw" << endl;
    } else if (ansColor == 1) {
      cout << "Black " << ansNumber << endl;
    } else {
      cout << "White " << ansNumber << endl;
    }
  }
}