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

#include "bits/stdc++.h"
using namespace std;

// #define int long long
#define DBG 0
#define dump(o)                                                                \\
  if (DBG) {                                                                   \\
    cerr << #o << " " << o << endl;                                            \\
  }
#define dumpc(o)                                                               \\
  if (DBG) {                                                                   \\
    cerr << #o;                                                                \\
    for (auto &e : (o))                                                        \\
      cerr << " " << e;                                                        \\
    cerr << endl;                                                              \\
  }
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define each(it, c) for (auto it = (c).begin(); it != (c).end(); it++)
#define all(c) c.begin(), c.end()
const int INF =
    sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9 + 7);

class Field {
public:
  int H;
  int W;
  int n;
  vector<int> di = {1, 0};
  vector<int> dj = {0, 1};
  vector<vector<char>> v;
  Field(int H, int W, int n) : H(H), W(W), n(n), v(H, vector<char>(W, 0)){};
  bool isinrange(int i, int j) { return 0 <= i && i < H && 0 <= j && j < W; }
  bool deleteBlocks() {
    bool ret = false;
    do {
      dump("deleteBlocks before");
      show();
      vector<vector<char>> f(H, vector<char>(W, false));
      rep(i, 0, H) rep(j, 0, W) {
        if (v[i][j] == '.')
          continue;
        rep(k, 0, di.size()) {
          int ii = i, jj = j;
          int cnt = 0;
          while (isinrange(ii, jj) && v[i][j] == v[ii][jj]) {
            ii += di[k], jj += dj[k];
            cnt++;
          }
          if (cnt < n)
            continue;
          ii = i, jj = j;
          while (isinrange(ii, jj) && v[i][j] == v[ii][jj]) {
            f[ii][jj] = true;
            ii += di[k], jj += dj[k];
          }
        }
      }
      rep(i, 0, H) rep(j, 0, W) {
        if (f[i][j]) {
          v[i][j] = '.';
          ret = true;
        }
      }
      dump("deleteBlocks after");
      show();
    } while (fill());
    return ret;
  }

  bool fill() {
    bool ret = true;
    dump("fill before");
    show();
    vector<vector<char>> v2(H, vector<char>(W, '.'));
    rep(j, 0, W) {
      int ii = H - 1;
      rrep(i, 0, H) {
        if (v[i][j] == '.')
          continue;
        v2[ii][j] = v[i][j];
        ii--;
      }
    }
    if (v == v2)
      ret = false;
    v = v2;
    dump("fill after");
    show();
    return ret;
  }

  bool blockExists() {
    rep(i, 0, H) rep(j, 0, W) {
      if (v[i][j] == '.')
        continue;
      return true;
    }
    return false;
  }

  void show() {
    if (DBG == 0)
      return;
    rep(i, 0, v.size()) {
      cout << v[i][0];
      rep(j, 1, v[i].size()) { cout << " " << v[i][j]; }
      cout << endl;
    }
    cout << endl;
  }
};

int h, w, n;

signed main() {
  cin >> h >> w >> n;
  Field F(h, w, n);
  rep(i, 0, h) rep(j, 0, w) cin >> F.v[i][j];
  rep(i, 0, h) rep(j, 1, w) {
    Field F2 = F;
    char &a = F2.v[i][j];
    char &b = F2.v[i][j - 1];
    if (a != '.' || b != '.') {
      dump(j - 1);
      dump(j);
      swap(a, b);
      F2.fill();
      while (F2.deleteBlocks())
        ;
      if (F2.blockExists() == false) {
        cout << "YES" << endl;
        return 0;
      }
    }
  }
  cout << "NO" << endl;
  return 0;
}

Error: WA

Fixed submission:

#include "bits/stdc++.h"
using namespace std;

// #define int long long
#define DBG 1
#define dump(o)                                                                \\
  if (DBG) {                                                                   \\
    cerr << #o << " " << o << endl;                                            \\
  }
#define dumpc(o)                                                               \\
  if (DBG) {                                                                   \\
    cerr << #o;                                                                \\
    for (auto &e : (o))                                                        \\
      cerr << " " << e;                                                        \\
    cerr << endl;                                                              \\
  }
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define each(it, c) for (auto it = (c).begin(); it != (c).end(); it++)
#define all(c) c.begin(), c.end()
const int INF =
    sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9 + 7);

class Field {
public:
  int H;
  int W;
  int n;
  vector<int> di = {1, 0};
  vector<int> dj = {0, 1};
  vector<vector<char>> v;
  Field(int H, int W, int n) : H(H), W(W), n(n), v(H, vector<char>(W, 0)){};
  bool isinrange(int i, int j) { return 0 <= i && i < H && 0 <= j && j < W; }
  bool deleteBlocks() {
    bool ret = false;
    do {
      dump("deleteBlocks before");
      show();
      vector<vector<char>> f(H, vector<char>(W, false));
      rep(i, 0, H) rep(j, 0, W) {
        if (v[i][j] == '.')
          continue;
        rep(k, 0, di.size()) {
          int ii = i, jj = j;
          int cnt = 0;
          while (isinrange(ii, jj) && v[i][j] == v[ii][jj]) {
            ii += di[k], jj += dj[k];
            cnt++;
          }
          if (cnt < n)
            continue;
          ii = i, jj = j;
          while (isinrange(ii, jj) && v[i][j] == v[ii][jj]) {
            f[ii][jj] = true;
            ii += di[k], jj += dj[k];
          }
        }
      }
      rep(i, 0, H) rep(j, 0, W) {
        if (f[i][j]) {
          v[i][j] = '.';
          ret = true;
        }
      }
      dump("deleteBlocks after");
      show();
    } while (fill());
    return ret;
  }

  bool fill() {
    bool ret = true;
    dump("fill before");
    show();
    vector<vector<char>> v2(H, vector<char>(W, '.'));
    rep(j, 0, W) {
      int ii = H - 1;
      rrep(i, 0, H) {
        if (v[i][j] == '.')
          continue;
        v2[ii][j] = v[i][j];
        ii--;
      }
    }
    if (v == v2)
      ret = false;
    v = v2;
    dump("fill after");
    show();
    return ret;
  }

  bool blockExists() {
    rep(i, 0, H) rep(j, 0, W) {
      if (v[i][j] == '.')
        continue;
      return true;
    }
    return false;
  }

  void show() {
    if (DBG == 0)
      return;
    rep(i, 0, v.size()) {
      cout << v[i][0];
      rep(j, 1, v[i].size()) { cout << " " << v[i][j]; }
      cout << endl;
    }
    cout << endl;
  }
};

int h, w, n;

signed main() {
  cin >> h >> w >> n;
  Field F(h, w, n);
  rep(i, 0, h) rep(j, 0, w) cin >> F.v[i][j];
  rep(i, 0, h) rep(j, 1, w) {
    Field F2 = F;
    char &a = F2.v[i][j];
    char &b = F2.v[i][j - 1];
    if (a != '.' || b != '.') {
      dump(j - 1);
      dump(j);
      swap(a, b);
      while (F2.deleteBlocks())
        ;
      if (F2.blockExists() == false) {
        cout << "YES" << endl;
        return 0;
      }
    }
  }
  cout << "NO" << endl;
  return 0;
}