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

#include <bits/stdc++.h>

using namespace std;

int n, m, p;
int x[500], y[500];
int pegs[10][10][10];
int z_idx[10][10];

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

bool sequence(int x, int y, int z) {
  for (int i = 0; i < 13; i++) {
    int seq = 1;
    int nx = x, ny = y, nz = z;
    while (1) {
      nx += dx[i], ny += dy[i], nz += dz[i];
      if (nx < 0 || n <= nx || ny < 0 || n <= ny || nz < 0 || n <= nz)
        break;
      if (pegs[nx][ny][nz] != pegs[x][y][z])
        break;
      seq++;
    }
    nx = x, ny = y, nz = z;
    while (1) {
      nx -= dx[i], ny -= dy[i], nz -= dz[i];
      if (nx < 0 || n <= nx || ny < 0 || n <= ny || nz < 0 || n <= nz)
        break;
      if (pegs[nx][ny][nz] != pegs[x][y][z])
        break;
      seq++;
    }
    if (seq >= m)
      return true;
  }
  return false;
}

int simulation(int t) {
  if (t == p)
    return -1;
  pegs[x[t]][y[t]][z_idx[x[t]][y[t]]] = t % 2;
  if (sequence(x[t], y[t], z_idx[x[t]][y[t]]))
    return t;
  z_idx[x[t]][y[t]]++;
  simulation(t + 1);
}

int main() {
  while (cin >> n >> m >> p, n || m || p) {
    for (int i = 0; i < p; i++)
      cin >> x[i] >> y[i], x[i]--, y[i]--;
    memset(pegs, -1, sizeof(pegs));
    memset(z_idx, 0, sizeof(z_idx));
    int ans = simulation(0);
    if (ans == -1)
      cout << "Draw" << endl;
    if (ans % 2 == 0)
      cout << "Black " << ans + 1 << endl;
    if (ans % 2 == 1)
      cout << "White " << ans + 1 << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int n, m, p;
int x[100], y[100];
int pegs[10][10][10];
int z_idx[10][10];

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

bool sequence(int x, int y, int z) {
  for (int i = 0; i < 13; i++) {
    int seq = 1;
    int nx = x, ny = y, nz = z;
    while (1) {
      nx += dx[i], ny += dy[i], nz += dz[i];
      if (nx < 0 || n <= nx || ny < 0 || n <= ny || nz < 0 || n <= nz)
        break;
      if (pegs[nx][ny][nz] != pegs[x][y][z])
        break;
      seq++;
    }
    nx = x, ny = y, nz = z;
    while (1) {
      nx -= dx[i], ny -= dy[i], nz -= dz[i];
      if (nx < 0 || n <= nx || ny < 0 || n <= ny || nz < 0 || n <= nz)
        break;
      if (pegs[nx][ny][nz] != pegs[x][y][z])
        break;
      seq++;
    }
    if (seq >= m)
      return true;
  }
  return false;
}

int simulation(int t) {
  if (t == p)
    return -1;
  pegs[x[t]][y[t]][z_idx[x[t]][y[t]]] = t % 2;
  if (sequence(x[t], y[t], z_idx[x[t]][y[t]]))
    return t;
  z_idx[x[t]][y[t]]++;
  simulation(t + 1);
}

int main() {
  while (cin >> n >> m >> p, n || m || p) {
    for (int i = 0; i < p; i++)
      cin >> x[i] >> y[i], x[i]--, y[i]--;
    memset(pegs, -1, sizeof(pegs));
    memset(z_idx, 0, sizeof(z_idx));
    int ans = simulation(0);
    if (ans == -1)
      cout << "Draw" << endl;
    if (ans % 2 == 0)
      cout << "Black " << ans + 1 << endl;
    if (ans % 2 == 1)
      cout << "White " << ans + 1 << endl;
  }
  return 0;
}