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

#include <cmath>
#include <cstring>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;
#define rep(i, b) for (int i = 0; i < b; i++)
int n, m, p;
/*int dx[17] = {-1,0,1, -1,1, -1,0,1,
              -1,0,1, -1,0,1, -1,0,1};
int dy[17] = {-1,-1,-1, 0,0, 1,1,1,
              -1,-1,-1, 0,0,0, 1,1,1};
int dz[17] = {0,0,0, 0,0, 0,0,0,
              1,1,1, 1,1,1, 1,1,1};*/
int dx[13] = {-1, 0, 1, 1, -1, 0, 1, 1, -1, 0, 1, 1, 0};
int dy[13] = {1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0};
int dz[13] = {-1, -1, -1, -1, 0, 0, 0, 0, 1, 1, 1, 1, 1};

string t[7][7][7];
int exist[7][7];
string c;

int search(int x, int y, int z, int d) {
  // bool res = false;
  // if( x >= n || y >= n || z >= exist[x][y] ) return 0;
  if (0 <= x && x < n && 0 <= y && y < n && 0 <= z && z < exist[x][y] &&
      t[x][y][z] == c) {
    // if( cnt == m ) return true;
    int nx = x + dx[d];
    int ny = y + dy[d];
    int nz = z + dz[d];
    // res = search(nx,ny,nz,cnt+1);
    // cout << x << ' ' << y << ' ' << z << endl;
    return search(nx, ny, nz, d) + 1;
  }
  return 0;
}
int search2(int x, int y, int z, int d) {
  // bool res = false;
  // if( x >= n || y >= n || z >= exist[x][y] ) return 0;
  if (0 <= x && x < n && 0 <= y && y < n && 0 <= z && z < exist[x][y] &&
      t[x][y][z] == c) {
    // if( cnt == m ) return true;
    int nx = x - dx[d];
    int ny = y - dy[d];
    int nz = z - dz[d];
    // res = search(nx,ny,nz,cnt+1);
    return search2(nx, ny, nz, d) + 1;
  }
  return 0;
}

int main() {
  while (cin >> n >> m >> p, n) {
    string ans = "Draw";
    int ans_t;
    memset(exist, 0, sizeof(exist));
    /*rep(i, n){
        rep(j, n){
            cout << exist[i][j] << '\\t';
        }
        cout << endl;
    }*/
    rep(a, p) {
      int x, y, z;
      cin >> x >> y;
      z = ++exist[x - 1][y - 1];
      // cout << z << endl;
      if (a % 2 == 0)
        t[x - 1][y - 1][z - 1] = c = "Black";
      else
        t[x - 1][y - 1][z - 1] = c = "White";
      if (ans == "Draw") {
        /*rep(i, n) rep(j, n) rep(k, exist[i][j]) rep(l, 7){
            d = l;
            if( search(i,j,k,1) ){
                ans = c;
                ans_t = a+1;
            }
        }*/
        rep(i, 13) {
          int cnt = search(x - 1, y - 1, z - 1, i);
          cnt += search2(x - 1, y - 1, z - 1, i) - 1;
          // cout << i << ' ' << cnt << endl;
          if (cnt >= m) {
            ans = c;
            ans_t = a + 1;
          }
        }
      }
    }
    /*rep(i, n){
        rep(j, n){
            cout << t[i][j][0] << '\\t';
            //cout << "(" << i << "," << j << ")" << ": ";
        }
        cout << endl;
    }*/
    // cout << 'c';
    if (ans == "Draw") {
      cout << ans << endl;
    } else {
      cout << ans << ' ' << ans_t << endl;
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <cmath>
#include <cstring>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;
#define rep(i, b) for (int i = 0; i < b; i++)
int n, m, p;
/*int dx[17] = {-1,0,1, -1,1, -1,0,1,
              -1,0,1, -1,0,1, -1,0,1};
int dy[17] = {-1,-1,-1, 0,0, 1,1,1,
              -1,-1,-1, 0,0,0, 1,1,1};
int dz[17] = {0,0,0, 0,0, 0,0,0,
              1,1,1, 1,1,1, 1,1,1};*/
int dx[7] = {0, 1, 1, 0, 1, 1, 0};
int dy[7] = {1, 1, 0, 1, 1, 0, 0};
int dz[7] = {0, 0, 0, 1, 1, 1, 1};

string t[7][7][7];
int exist[7][7];
string c;
int d;

bool search(int x, int y, int z, int cnt) {
  int nx = x + dx[d];
  int ny = y + dy[d];
  int nz = z + dz[d];
  bool res = false;
  if (t[x][y][z] == c && nx < 7 && ny < 7 && z < exist[x][y]) {
    if (cnt == m)
      return true;
    res = search(nx, ny, nz, cnt + 1);
  }
  return res;
}

int main() {
  while (cin >> n >> m >> p, n) {
    string ans = "Draw";
    int ans_t = 0;
    memset(exist, 0, sizeof(exist));
    /*rep(i, n){
        rep(j, n){
            cout << exist[i][j] << '\\t';
        }
        cout << endl;
    }*/
    rep(a, p) {
      int x, y, z;
      cin >> x >> y;
      z = ++exist[x - 1][y - 1];
      // cout << z << endl;
      if (a % 2 == 0)
        t[x - 1][y - 1][z - 1] = c = "Black";
      else
        t[x - 1][y - 1][z - 1] = c = "White";
      rep(i, n) rep(j, n) rep(k, exist[i][j]) rep(l, 7) {
        d = l;
        if (search(i, j, k, 1) && ans == "Draw") {
          ans = c;
          ans_t = a + 1;
        }
      }
    }
    /*rep(i, n){
        rep(j, n){
            cout << t[i][j][0] << '\\t';
            //cout << "(" << i << "," << j << ")" << ": ";
        }
        rep(k, n){
            cout << t[i][j][k] << '\\t';
        }
        cout << endl;
    }*/
    // cout << 'c';
    if (ans == "Draw") {
      cout << ans << endl;
    } else {
      cout << ans << ' ' << ans_t << endl;
    }
  }
  return 0;
}