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

#include <bits/stdc++.h>

using namespace std;

#define REP(i, a, b) for (int i = a; i < (int)b; i++)
#define rep(i, n) REP(i, 0, n)

inline bool inrange(int x, int y) {
  return (0 <= x && x < 8) && (0 <= y && y < 8);
}

// string F[8];
char F[10][10];
bool used[8][8];
int turn;

bool debug;

int dx, dy;
int dfs(int x, int y, char me, int cnt) {

  x += dx, y += dy;
  if (!inrange(x, y)) {
    return 0;
  }
  if (F[y][x] == '.') {
    return 0;
  }
  if (F[y][x] == me) {
    return cnt;
  }
  used[y][x] = 1;
  int r = dfs(x, y, me, cnt + 1);
  if (r == 0) {
    used[y][x] = 0;
  }
  return r;
}

#define DREP(i, a, b, c, d)                                                    \\
  for (int i = turn & 1 ? a : c; turn & 1 ? i >= b : i <= d;                   \\
       turn & 1 ? i-- : i++)

int main() {
  /*
  rep(i, 8) rep(j, 8){
    cin >> F[i][j];
  }
  //  */
  rep(i, 8) cin >> F[i];

  int ucnt = 0;
  for (turn = 0; turn <= 64; turn++) {
    int pl;
    bool u[8][8] = {};
    int mx = 0;
    DREP(i, 7, 0, 0, 7)
    DREP(j, 7, 0, 0, 7) {
      if (F[i][j] == '.') {
        rep(a, 8) rep(b, 8) used[a][b] = 0;
        used[i][j] = 1;
        int r = 0;
        for (dy = -1; dy < 2; dy++)
          for (dx = -1; dx < 2; dx++) {
            if (dx == 0 && dy == 0) {
              continue;
            }
            r += dfs(j, i, "ox"[turn & 1], 0);
          }

        if (mx < r) {
          memcpy(u, used, sizeof u);
          mx = r;
          pl = turn & 1;
        }
      }
    }
    if (mx > 0) {
      rep(i, 8) rep(j, 8) if (u[i][j]) { F[i][j] = "ox"[pl]; }

      ucnt = 0;
    } else {
      if (ucnt) {
        break;
      }
      ucnt++;
    }
  }

  rep(i, 8) {
    rep(j, 8) { cout << F[i][j]; }
    cout << endl;
  }

  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define REP(i, a, b) for (int i = a; i < (int)b; i++)
#define rep(i, n) REP(i, 0, n)

inline bool inrange(int x, int y) {
  return (0 <= x && x < 8) && (0 <= y && y < 8);
}

string F[10];
bool used[10][10];
int turn;

bool debug;

int dx, dy;
int dfs(int x, int y, char me, int cnt) {

  if (x == 4 && y == 2 && dx == 0 && dy == 1) {
    cerr << "pass\\n";
  }
  x += dx, y += dy;
  if (!inrange(x, y)) {
    return 0;
  }
  if (F[y][x] == '.') {
    return 0;
  }
  if (F[y][x] == me) {
    if (cnt)
      cerr << "hit " << cnt << "\\n";
    return cnt;
  }
  used[y][x] = 1;
  int r = dfs(x, y, me, cnt + 1);
  if (r == 0) {
    used[y][x] = 0;
  }
  return r;
}

#define DREP(i, a, b, c, d)                                                    \\
  for (int i = turn & 1 ? a : c; turn & 1 ? i >= b : i <= d;                   \\
       turn & 1 ? i-- : i++)

int main() {

  rep(i, 8) { cin >> F[i]; }

  int ucnt = 0;
  for (turn = 0; turn <= 64; turn++) {
    cerr << "ox"[turn & 1] << endl;
    int pl;
    bool u[8][8] = {};
    int mx = 0;
    DREP(i, 7, 0, 0, 7)
    DREP(j, 7, 0, 0, 7) {
      if (F[i][j] == '.') {
        rep(a, 8) rep(b, 8) used[a][b] = 0;
        used[i][j] = 1;
        int r = 0;
        for (dy = -1; dy < 2; dy++)
          for (dx = -1; dx < 2; dx++) {
            if (dx == 0 && dy == 0) {
              continue;
            }
            r += dfs(j, i, "ox"[turn & 1], 0);
          }

        if (mx < r) {
          cerr << "update mx ";
          memcpy(u, used, sizeof u);
          mx = r;
          cerr << mx << endl;
          pl = turn & 1;
        }
      }
    }
    if (mx > 0) {
      cerr << "update field\\n";
      rep(i, 8) rep(j, 8) if (u[i][j]) { F[i][j] = "ox"[pl]; }

      ucnt = 0;
    } else {
      if (ucnt) {
        break;
      }
      ucnt++;
    }
  }

  rep(i, 8) {
    rep(j, 8) { cout << F[i][j]; }
    cout << endl;
  }

  return 0;
}