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

#define DEBUG_ON_
#define CONDITION true
using namespace std; /*{{{*/

#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <sys/time.h>
#include <vector>

#define INF (1e9)
static const double PI = acos(-1.0);
static const double EPS = 1e-10;

typedef long long int LL;
typedef unsigned long long int ULL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<double> VD;
typedef vector<VD> VVD;
typedef vector<bool> VB;
typedef vector<VB> VVB;
typedef vector<char> VC;
typedef vector<VC> VVC;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef complex<double> P;

#define FOR(i, b, e) for (typeof(e) i = (b); i != (e); i < (e) ? ++i : --i)
#define REP(i, n) FOR(i, 0, n)

#define IFC(c)                                                                 \\
  if (c)                                                                       \\
    continue;
#define IFB(c)                                                                 \\
  if (c)                                                                       \\
    break;
#define IFR(c, r)                                                              \\
  if (c)                                                                       \\
    return r;

#define OPOVER(_op, _type) inline bool operator _op(const _type &t) const

#define arrsz(a) (sizeof(a) / sizeof(a[0]))

#define F first
#define S second
#define MP(a, b) make_pair(a, b)

#define SZ(a) ((LL)a.size())
#define PB(e) push_back(e)
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())

#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EACH(c, it)                                                            \\
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define EXIST(s, e) ((s).find(e) != (s).end())

#define BIT(n) (assert(n < 64), (1ULL << (n)))
#define BITOF(n, m) (assert(m < 64), ((ULL)(n) >> (m)&1))

#define RANGE(a, b, c) ((a) <= (b) && (b) <= (c))

#define PQ priority_queue
#define SC static_cast

#ifdef DEBUG_ON
#define dprt(fmt, ...)                                                         \\
  if (CONDITION)                                                               \\
  fprintf(stderr, fmt, ##__VA_ARGS__)
#define darr(a)                                                                \\
  if (CONDITION)                                                               \\
    copy((a), (a) + arrsz(a), ostream_iterator<int>(cerr, " "));               \\
  cerr << endl
#define darr_range(a, f, t)                                                    \\
  if (CONDITION)                                                               \\
    copy((a) + (f), (a) + (t), ostream_iterator<int>(cerr, " "));              \\
  cerr << endl
#define dvec(v)                                                                \\
  if (CONDITION)                                                               \\
    copy(ALL(v), ostream_iterator<char>(cerr, " "));                           \\
  cerr << endl
#define darr2(a, n, m)                                                         \\
  if (CONDITION)                                                               \\
    FOR(i, 0, (n)) { darr_range((a)[i], 0, (m)); }
#define dvec2(v)                                                               \\
  if (CONDITION)                                                               \\
    FOR(__i, 0, SZ(v)) { dvec((v)[__i]); }
#define WAIT()                                                                 \\
  if (CONDITION) {                                                             \\
    string _wait_;                                                             \\
    cerr << "(hit return to continue)" << endl;                                \\
    getline(cin, _wait_);                                                      \\
  }
#define dump(x)                                                                \\
  if (CONDITION)                                                               \\
    cerr << " [L" << __LINE__ << "] " << #x << " = " << (x) << endl;
#define dumpf()                                                                \\
  if (CONDITION)                                                               \\
    cerr << __PRETTY_FUNCTION__ << endl;
#define dumpv(x)                                                               \\
  if (CONDITION)                                                               \\
    cerr << " [L:" << __LINE__ << "] " << #x << " = ";                         \\
  REP(q, (x).size()) cerr << (x)[q] << " ";                                    \\
  cerr << endl;
#define where()                                                                \\
  if (CONDITION)                                                               \\
    cerr << __FILE__ << ": " << __PRETTY_FUNCTION__ << " [L: " << __LINE__     \\
         << "]" << endl;
#define show_bits(b, s)                                                        \\
  if (CONDITION) {                                                             \\
    REP(i, s) {                                                                \\
      cerr << BITOF(b, s - 1 - i);                                             \\
      if (i % 4 == 3)                                                          \\
        cerr << ' ';                                                           \\
    }                                                                          \\
    cerr << endl;                                                              \\
  }
#else
#define cerr                                                                   \\
  if (0)                                                                       \\
  cerr
#define dprt(fmt, ...)
#define darr(a)
#define darr_range(a, f, t)
#define dvec(v)
#define darr2(a, n, m)
#define dvec2(v)
#define WAIT()
#define dump(x)
#define dumpf()
#define dumpv(x)
#define where()
#define show_bits(b, s)
#endif

/* Inline functions */
inline int onbits_count(ULL b) {
  int c = 0;
  while (b != 0) {
    c += (b & 1);
    b >>= 1;
  }
  return c;
}
inline int bits_count(ULL b) {
  int c = 0;
  while (b != 0) {
    ++c;
    b >>= 1;
  }
  return c;
}
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
inline double now() {
  struct timeval tv;
  gettimeofday(&tv, NULL);
  return (static_cast<double>(tv.tv_sec) +
          static_cast<double>(tv.tv_usec) * 1e-6);
}
inline VS split(string s, char delimiter) {
  VS v;
  string t;
  REP(i, s.length()) {
    if (s[i] == delimiter)
      v.PB(t), t = "";
    else
      t += s[i];
  }
  v.PB(t);
  return v;
}

/* Tweaks */
template <typename T1, typename T2>
ostream &operator<<(ostream &s, const pair<T1, T2> &d) {
  return s << "(" << d.first << ", " << d.second << ")";
}

/* Frequent stuffs */
int n_dir = 4;
int dx[] = {0, 1, 0, -1}, dy[] = {-1, 0, 1, 0}; /* CSS order */
enum direction { UP, RIGHT, DOWN, LEFT };
// int n_dir = 8;
// int dx[] = {0, 1, 1, 1, 0, -1, -1, -1}, dy[] = {-1, -1, 0, 1, 1, 1, 0, -1};
// enum direction {
// UP, UPRIGHT, RIGHT, DOWNRIGHT, DOWN, DOWNLEFT, LEFT, UPLEFT
// }
#define FORDIR(d) REP(d, n_dir)

/*}}}*/

int main() {
  std::ios_base::sync_with_stdio(false);
  int h, w, n;
  cin >> h >> w >> n;
  VVC field = VVC(h, VC(w, '.'));
  REP(i, h) {
    REP(j, w) { cin >> field[i][j]; }
  }

  VVC save = field;
  // Try all swaps
  REP(i, h) {
    REP(j, w - 1) {
      // Initialization
      dprt("Try (%d, %d) <-> (%d, %d)\\n", i, j, i, j + 1);
      field = save;

      IFC(field[i][j] == field[i][j + 1]);
      // Swap (i, j) and (i, j+1)
      swap(field[i][j], field[i][j + 1]);
      queue<PII> q;
      q.push(MP(i, j)), q.push(MP(i, j + 1));
      FOR(jj, j, j + 2) {
        int nii = i;
        while (nii + 1 < h && field[nii + 1][jj] == '.') {
          ++nii;
        }
        if (i != nii) {
          field[nii][jj] = field[i][jj];
          field[i][jj] = '.';
        }
        q.push(MP(nii, jj));
      }
      while (true) {
        VVB vanishing = VVB(h, VB(w, false));
        // Check if state-changed cells are vanishing
        while (!q.empty()) {
          PII p = q.front();
          q.pop();
          int y = p.F, x = p.S;
          IFC(field[y][x] == '.');
          int cy, cx, sy, sx, ey, ex, cnt;
          // Horizontal
          cy = y, cx = x;
          while (0 <= cx - 1 && field[cy][cx - 1] == field[y][x]) {
            --cx;
          }
          sx = cx;
          cnt = 0;
          while (cx < w && field[cy][cx] == field[y][x]) {
            ++cnt;
            ++cx;
          }
          ex = cx;
          if (cnt >= n) {
            FOR(xx, sx, ex) { vanishing[y][xx] = true; }
          }
          // Vertical
          cy = y, cx = x;
          while (0 <= cy - 1 && field[cy - 1][cx] == field[y][x]) {
            --cy;
          }
          sy = cy;
          cnt = 0;
          while (cy < h && field[cy][cx] == field[y][x]) {
            ++cnt;
            ++cy;
          }
          ey = cy;
          if (cnt >= n) {
            FOR(yy, sy, ey) { vanishing[yy][x] = true; }
          }

          // REP (d, 4) {
          // bool line = true;
          // REP (cn, n) {
          // int cy = y + cn * dy[d], cx = x + cn * dx[d];
          // if (!( (0 <= cy && cy < h) && (0 <= cx && cx < w) )) {
          // line = false;
          // break;
          // }
          // if (field[cy][cx] != field[y][x]) {
          // line = false;
          // break;
          // }
          // }
          // if (line) {
          // REP (cn, n) {
          // int cy = y + cn * dy[d], cx = x + cn * dx[d];
          // vanishing[cy][cx] = true;
          // }
          // }
          // }
        }
        // Vanish all cells
        REP(ii, h) {
          REP(jj, w) {
            if (vanishing[ii][jj]) {
              field[ii][jj] = '.';
            }
          }
        }
        dprt("Vanish finished\\n");
        dvec2(field);
        // Fall down
        for (int ii = h - 1; ii >= 0; --ii) {
          REP(jj, w) {
            if (field[ii][jj] != '.') {
              int nii = ii;
              while (nii + 1 < h && field[nii + 1][jj] == '.') {
                ++nii;
              }
              if (ii != nii) {
                field[nii][jj] = field[ii][jj];
                field[ii][jj] = '.';
                q.push(MP(nii, jj));
              }
            }
          }
        }
        dprt("Falling finished\\n");
        dvec2(field);
        if (q.empty()) {
          break;
        }
      }
      // Clear check
      bool clear = true;
      REP(ii, h) {
        REP(jj, w) {
          if (field[ii][jj] != '.') {
            clear = false;
          }
        }
      }
      if (clear) {
        cout << "YES" << endl;
        return 0;
      }
    }
  }
  cout << "NO" << endl;
}

// vim: foldmethod=marker

Error: TLE

Fixed submission:

#define DEBUG_ON
#define CONDITION true
using namespace std; /*{{{*/

#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <sys/time.h>
#include <vector>

#define INF (1e9)
static const double PI = acos(-1.0);
static const double EPS = 1e-10;

typedef long long int LL;
typedef unsigned long long int ULL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<double> VD;
typedef vector<VD> VVD;
typedef vector<bool> VB;
typedef vector<VB> VVB;
typedef vector<char> VC;
typedef vector<VC> VVC;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef complex<double> P;

#define FOR(i, b, e) for (typeof(e) i = (b); i != (e); i < (e) ? ++i : --i)
#define REP(i, n) FOR(i, 0, n)

#define IFC(c)                                                                 \\
  if (c)                                                                       \\
    continue;
#define IFB(c)                                                                 \\
  if (c)                                                                       \\
    break;
#define IFR(c, r)                                                              \\
  if (c)                                                                       \\
    return r;

#define OPOVER(_op, _type) inline bool operator _op(const _type &t) const

#define arrsz(a) (sizeof(a) / sizeof(a[0]))

#define F first
#define S second
#define MP(a, b) make_pair(a, b)

#define SZ(a) ((LL)a.size())
#define PB(e) push_back(e)
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())

#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EACH(c, it)                                                            \\
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define EXIST(s, e) ((s).find(e) != (s).end())

#define BIT(n) (assert(n < 64), (1ULL << (n)))
#define BITOF(n, m) (assert(m < 64), ((ULL)(n) >> (m)&1))

#define RANGE(a, b, c) ((a) <= (b) && (b) <= (c))

#define PQ priority_queue
#define SC static_cast

#ifdef DEBUG_ON
#define dprt(fmt, ...)                                                         \\
  if (CONDITION)                                                               \\
  fprintf(stderr, fmt, ##__VA_ARGS__)
#define darr(a)                                                                \\
  if (CONDITION)                                                               \\
    copy((a), (a) + arrsz(a), ostream_iterator<int>(cerr, " "));               \\
  cerr << endl
#define darr_range(a, f, t)                                                    \\
  if (CONDITION)                                                               \\
    copy((a) + (f), (a) + (t), ostream_iterator<int>(cerr, " "));              \\
  cerr << endl
#define dvec(v)                                                                \\
  if (CONDITION)                                                               \\
    copy(ALL(v), ostream_iterator<char>(cerr, " "));                           \\
  cerr << endl
#define darr2(a, n, m)                                                         \\
  if (CONDITION)                                                               \\
    FOR(i, 0, (n)) { darr_range((a)[i], 0, (m)); }
#define dvec2(v)                                                               \\
  if (CONDITION)                                                               \\
    FOR(__i, 0, SZ(v)) { dvec((v)[__i]); }
#define WAIT()                                                                 \\
  if (CONDITION) {                                                             \\
    string _wait_;                                                             \\
    cerr << "(hit return to continue)" << endl;                                \\
    getline(cin, _wait_);                                                      \\
  }
#define dump(x)                                                                \\
  if (CONDITION)                                                               \\
    cerr << " [L" << __LINE__ << "] " << #x << " = " << (x) << endl;
#define dumpf()                                                                \\
  if (CONDITION)                                                               \\
    cerr << __PRETTY_FUNCTION__ << endl;
#define dumpv(x)                                                               \\
  if (CONDITION)                                                               \\
    cerr << " [L:" << __LINE__ << "] " << #x << " = ";                         \\
  REP(q, (x).size()) cerr << (x)[q] << " ";                                    \\
  cerr << endl;
#define where()                                                                \\
  if (CONDITION)                                                               \\
    cerr << __FILE__ << ": " << __PRETTY_FUNCTION__ << " [L: " << __LINE__     \\
         << "]" << endl;
#define show_bits(b, s)                                                        \\
  if (CONDITION) {                                                             \\
    REP(i, s) {                                                                \\
      cerr << BITOF(b, s - 1 - i);                                             \\
      if (i % 4 == 3)                                                          \\
        cerr << ' ';                                                           \\
    }                                                                          \\
    cerr << endl;                                                              \\
  }
#else
#define cerr                                                                   \\
  if (0)                                                                       \\
  cerr
#define dprt(fmt, ...)
#define darr(a)
#define darr_range(a, f, t)
#define dvec(v)
#define darr2(a, n, m)
#define dvec2(v)
#define WAIT()
#define dump(x)
#define dumpf()
#define dumpv(x)
#define where()
#define show_bits(b, s)
#endif

/* Inline functions */
inline int onbits_count(ULL b) {
  int c = 0;
  while (b != 0) {
    c += (b & 1);
    b >>= 1;
  }
  return c;
}
inline int bits_count(ULL b) {
  int c = 0;
  while (b != 0) {
    ++c;
    b >>= 1;
  }
  return c;
}
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
inline double now() {
  struct timeval tv;
  gettimeofday(&tv, NULL);
  return (static_cast<double>(tv.tv_sec) +
          static_cast<double>(tv.tv_usec) * 1e-6);
}
inline VS split(string s, char delimiter) {
  VS v;
  string t;
  REP(i, s.length()) {
    if (s[i] == delimiter)
      v.PB(t), t = "";
    else
      t += s[i];
  }
  v.PB(t);
  return v;
}

/* Tweaks */
template <typename T1, typename T2>
ostream &operator<<(ostream &s, const pair<T1, T2> &d) {
  return s << "(" << d.first << ", " << d.second << ")";
}

/* Frequent stuffs */
int n_dir = 4;
int dx[] = {0, 1, 0, -1}, dy[] = {-1, 0, 1, 0}; /* CSS order */
enum direction { UP, RIGHT, DOWN, LEFT };
// int n_dir = 8;
// int dx[] = {0, 1, 1, 1, 0, -1, -1, -1}, dy[] = {-1, -1, 0, 1, 1, 1, 0, -1};
// enum direction {
// UP, UPRIGHT, RIGHT, DOWNRIGHT, DOWN, DOWNLEFT, LEFT, UPLEFT
// }
#define FORDIR(d) REP(d, n_dir)

/*}}}*/

int main() {
  std::ios_base::sync_with_stdio(false);
  int h, w, n;
  cin >> h >> w >> n;
  VVC field = VVC(h, VC(w, '.'));
  REP(i, h) {
    REP(j, w) { cin >> field[i][j]; }
  }

  VVC save = field;
  // Try all swaps
  REP(i, h) {
    REP(j, w - 1) {
      // Initialization
      dprt("Try (%d, %d) <-> (%d, %d)\\n", i, j, i, j + 1);
      field = save;

      IFC(field[i][j] == field[i][j + 1]);
      // Swap (i, j) and (i, j+1)
      swap(field[i][j], field[i][j + 1]);
      queue<PII> q;
      q.push(MP(i, j)), q.push(MP(i, j + 1));
      while (true) {
        VVB vanishing = VVB(h, VB(w, false));
        // Check if state-changed cells are vanishing
        REP(ii, h) {
          REP(jj, w) {
            int y = ii, x = jj;
            IFC(field[y][x] == '.');
            // IFC(vanishing[y][x]);
            int cy, cx, cnt;
            // Horizontal
            cy = y, cx = x;
            cnt = 0;
            while (cx < w && field[cy][cx] == field[y][x]) {
              ++cnt;
              ++cx;
            }
            if (cnt >= n) {
              FOR(xx, x, cx) { vanishing[y][xx] = true; }
            }
            // Vertical
            cy = y, cx = x;
            cnt = 0;
            while (cy < h && field[cy][cx] == field[y][x]) {
              ++cnt;
              ++cy;
            }
            if (cnt >= n) {
              FOR(yy, y, cy) { vanishing[yy][x] = true; }
            }
          }
        }
        // Vanish all cells
        REP(ii, h) {
          REP(jj, w) {
            if (vanishing[ii][jj]) {
              field[ii][jj] = '.';
            }
          }
        }
        dprt("Vanish finished\\n");
        dvec2(field);
        // Fall down
        for (int ii = h - 1; ii >= 0; --ii) {
          REP(jj, w) {
            if (field[ii][jj] != '.') {
              int nii = ii;
              while (nii + 1 < h && field[nii + 1][jj] == '.') {
                ++nii;
              }
              if (ii != nii) {
                field[nii][jj] = field[ii][jj];
                field[ii][jj] = '.';
                q.push(MP(nii, jj));
              }
            }
          }
        }
        dprt("Falling finished\\n");
        dvec2(field);
        if (q.empty()) {
          break;
        }
      }
      // Clear check
      bool clear = true;
      REP(ii, h) {
        REP(jj, w) {
          if (field[ii][jj] != '.') {
            clear = false;
          }
        }
      }
      if (clear) {
        cout << "YES" << endl;
        return 0;
      }
    }
  }
  cout << "NO" << endl;
}

// vim: foldmethod=marker