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

#include <bits/stdc++.h>

using namespace std;

#define LOG(...) fprintf(stderr, __VA_ARGS__)
// #define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define RFOR(i, a, b) for (int i = (int)(b - 1); i >= (int)(a); --i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define RREP(i, n) for (int i = (int)(n - 1); i >= 0; --i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))
#define SZ(a) ((int)(a).size())
#define BIT(x, i) (((x) >> (i)) & 1)
#define SQ(x) ((x) * (x))

#define _GLIBCXX_DEBUG

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

int N, M, P;

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

int check_board(vector<vvi> &board) {
  REP(z, N) REP(y, N) REP(x, N) {
    REP(i, 16) {
      int sx = x + dx[i] * (M - 1), sy = y + dy[i] * (M - 1),
          sz = z + dz[i] * (M - 1);
      if (0 <= sx && sx < N && 0 <= sy && sy < N && 0 <= sz && sz < N) {
        int black = 0, white = 0;
        REP(j, M) {
          int sx = x + dx[i] * j, sy = y + dy[i] * j, sz = z + dz[i] * j;
          if (board[sz][sy][sx] == 1) {
            black++;
          } else if (board[sz][sy][sx] == 2) {
            white++;
          }
        }
        if (black == M) {
          return 1;
        } else if (white == M) {
          return 2;
        }
      }
    }
  }
  return 0;
}

int main() {
  while (cin >> N >> M >> P, N | M | P) {
    vector<vvi> board(N, vvi(N, vi(N)));
    vector<pii> p(P);
    REP(i, P) {
      int x, y;
      cin >> x >> y;
      x--;
      y--;
      p[i] = {x, y};
    }

    REP(i, P) {
      int x = p[i].first, y = p[i].second;
      REP(z, N) {
        if (board[z][y][x] == 0) {
          board[z][y][x] = i % 2 + 1;
          break;
        }
      }
      int retval = check_board(board);
      if (retval == 1) {
        printf("Black %d\\n", i + 1);
        goto END;
      } else if (retval == 2) {
        printf("White %d\\n", i + 1);
        goto END;
      }
    }
    printf("Draw\\n");
  END : {}
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define LOG(...) fprintf(stderr, __VA_ARGS__)
// #define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define RFOR(i, a, b) for (int i = (int)(b - 1); i >= (int)(a); --i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define RREP(i, n) for (int i = (int)(n - 1); i >= 0; --i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))
#define SZ(a) ((int)(a).size())
#define BIT(x, i) (((x) >> (i)) & 1)
#define SQ(x) ((x) * (x))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

int N, M, P;

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

int check_board(vector<vvi> &board) {
  REP(z, N) REP(y, N) REP(x, N) {
    REP(i, sizeof(dx)) {
      int sx = x + dx[i] * (M - 1), sy = y + dy[i] * (M - 1),
          sz = z + dz[i] * (M - 1);
      if (0 <= sx && sx < N && 0 <= sy && sy < N && 0 <= sz && sz < N) {
        int black = 0, white = 0;
        REP(j, M) {
          int sx = x + dx[i] * j, sy = y + dy[i] * j, sz = z + dz[i] * j;
          if (board[sz][sy][sx] == 1) {
            black++;
          } else if (board[sz][sy][sx] == 2) {
            white++;
          }
        }
        if (black == M) {
          return 1;
        } else if (white == M) {
          return 2;
        }
      }
    }
  }
  return 0;
}

int main() {
  while (cin >> N >> M >> P, N | M | P) {
    vector<vvi> board(N, vvi(N, vi(N)));
    vector<pii> p(P);
    REP(i, P) {
      int x, y;
      cin >> x >> y;
      x--;
      y--;
      p[i] = {x, y};
    }

    REP(i, P) {
      int x = p[i].first, y = p[i].second;
      REP(z, N) {
        if (board[z][y][x] == 0) {
          board[z][y][x] = i % 2 + 1;
          break;
        }
      }
      int retval = check_board(board);
      if (retval == 1) {
        printf("Black %d\\n", i + 1);
        goto END;
      } else if (retval == 2) {
        printf("White %d\\n", i + 1);
        goto END;
      }
    }
    printf("Draw\\n");
  END : {}
  }
}