Problem p01349 (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 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 RFOR(i, a, b) for (int i = (int)(b - 1); i >= (int)(a); --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 SQ(n) (n) * (n)
#define BIT(x, i) (((x) >> (i)) & 1)

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

int h, w, n;

bool update(vvc field, int X, int Y) {
  swap(field[Y][X], field[Y][X + 1]);

  bool update = true;
  while (update) {
    update = false;

    // drop
    REP(x, w) RREP(y, h) {
      if (field[y][x] == '.') {
        RFOR(yy, 0, y) {
          if (field[yy][x] != '.') {
            swap(field[y][x], field[yy][x]);
            update = true;
            break;
          }
        }
      }
    }
    LOG("droped\\n");
    REP(y, h) {
      REP(x, w) { LOG("%c", field[y][x]); }
      LOG("\\n");
    }
    LOG("---------\\n");

    // delete
    vvb dlfield(h, vb(w, false));
    REP(y, h) {
      REP(x, w - n + 1) {
        if (field[y][x] == '.')
          continue;
        bool flag = true;
        FOR(d, 1, n) {
          if (field[y][x] != field[y][x + d]) {
            flag = false;
            break;
          }
        }
        if (flag) {
          REP(d, n) dlfield[y][x + d] = true;
        }
      }
    }
    REP(x, w) {
      REP(y, h - n + 1) {
        if (field[y][x] == '.')
          continue;
        bool flag = true;
        FOR(d, 1, n) {
          if (field[y][x] != field[y + d][x]) {
            flag = false;
            break;
          }
        }
        if (flag) {
          REP(d, n) dlfield[y + d][x] = true;
        }
      }
    }
    LOG("deleted\\n");
    REP(y, h) {
      REP(x, w) {
        if (dlfield[y][x]) {
          update = true;
          field[y][x] = '.';
        }
      }
    }
    REP(y, h) {
      REP(x, w) { LOG("%c", field[y][x]); }
      LOG("\\n");
    }
    LOG("---------\\n");
  }

  int cnt = 0;
  REP(y, h) REP(x, w) {
    if (field[y][x] == '.')
      cnt++;
  }
  LOG("e:%d v:%d\\n", w * h, cnt);
  return cnt == w * h;
}

int main() {
  cin >> h >> w >> n;
  vvc field(h, vc(w));
  REP(y, h) REP(x, w) { cin >> field[y][x]; }

  RREP(y, h) REP(x, w - 1) {
    if (field[y][x] == field[y][x + 1])
      continue;
    if (update(field, x, y)) {
      cout << "YES" << endl;
      return 0;
    }
  }
  cout << "NO" << endl;
}

Error: 0
droped
......
...Y..
...Y..
RYRRYY
---------
deleted
......
...Y..
...Y..
RYRRYY
---------
e:24 v:16
droped
......
...Y..
...Y..
RRRYYY
---------
deleted
......
......
......
......
---------
e:24 v:24


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 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 RFOR(i, a, b) for (int i = (int)(b - 1); i >= (int)(a); --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 SQ(n) (n) * (n)
#define BIT(x, i) (((x) >> (i)) & 1)

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

int h, w, n;

bool update(vvc field, int X, int Y) {
  swap(field[Y][X], field[Y][X + 1]);

  bool update = true;
  while (update) {
    update = false;

    // drop
    REP(x, w) RREP(y, h) {
      if (field[y][x] == '.') {
        RFOR(yy, 0, y) {
          if (field[yy][x] != '.') {
            swap(field[y][x], field[yy][x]);
            update = true;
            break;
          }
        }
      }
    }
    LOG("droped\\n");
    REP(y, h) {
      REP(x, w) { LOG("%c", field[y][x]); }
      LOG("\\n");
    }
    LOG("---------\\n");

    // delete
    vvb dlfield(h, vb(w, false));
    REP(y, h) {
      REP(x, w - n + 1) {
        if (field[y][x] == '.')
          continue;
        bool flag = true;
        FOR(d, 1, n) {
          if (field[y][x] != field[y][x + d]) {
            flag = false;
            break;
          }
        }
        if (flag) {
          REP(d, n) dlfield[y][x + d] = true;
        }
      }
    }
    REP(x, w) {
      REP(y, h - n + 1) {
        if (field[y][x] == '.')
          continue;
        bool flag = true;
        FOR(d, 1, n) {
          if (field[y][x] != field[y + d][x]) {
            flag = false;
            break;
          }
        }
        if (flag) {
          REP(d, n) dlfield[y + d][x] = true;
        }
      }
    }
    LOG("deleted\\n");
    REP(y, h) {
      REP(x, w) {
        if (dlfield[y][x]) {
          field[y][x] = '.';
        }
      }
    }
    REP(y, h) {
      REP(x, w) { LOG("%c", field[y][x]); }
      LOG("\\n");
    }
    LOG("---------\\n");
  }

  int cnt = 0;
  REP(y, h) REP(x, w) {
    if (field[y][x] == '.')
      cnt++;
  }
  LOG("e:%d v:%d\\n", w * h, cnt);
  return cnt == w * h;
}

int main() {
  cin >> h >> w >> n;
  vvc field(h, vc(w));
  REP(y, h) REP(x, w) { cin >> field[y][x]; }

  RREP(y, h) REP(x, w - 1) {
    if (field[y][x] == field[y][x + 1])
      continue;
    if (update(field, x, y)) {
      cout << "YES" << endl;
      return 0;
    }
  }
  cout << "NO" << endl;
}