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

#define _CRT_SECURE_NO_WARNINGS 1
#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <forward_list>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define INF ((int)(1 << 25))
#define REP(i, n) for (int i = 0; i < (int)n; i++)
#define FOR(i, n) REP(i, n)
#define FORI(i, k, n) for (int i = k; i < (int)n; i++)
#define TORAD 2.0 * M_PI / 360.0
#define INT(x)                                                                 \\
  int x;                                                                       \\
  cin >> x;
#define ALL(x) begin(x), end(x)
#define RALL(x) (x).rbegin(), (x).rend()
#define DEBUG(x) cout << #x << ":" << x << endl;
#define EPS 1e-12
#ifdef _MSC_VER
#include <intrin.h>
#define __builtin_popcount __popcnt
#endif
typedef long long lint;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
template <class T> void debug(T a) {
  for (auto i : a)
    cout << i << endl;
}

typedef vector<vc> P;
int h, w, n;
bool check(P &a) {
  int ans = 0;
  FORI(i, 1, h + 1) {
    FORI(j, 1, w + 1) {
      if (a[i][j] != '.')
        ans++;
    }
  }
  return !ans;
}

bool remove(P &a) {
  bool r[40][40] = {};
  FORI(i, 1, h + 1) {
    FORI(j, 1, w + 1) {
      if (a[i][j] != '.') {
        int c = 0;
        char ch = a[i][j];
        for (int x = j;; x++) {
          if (a[i][x] == ch)
            c++;
          else
            break;
        }
        if (c >= n) {
          for (int x = j;; x++) {
            if (a[i][x] == ch)
              r[i][x] = true;
            else
              break;
          }
        }
        c = 0;
        for (int y = i;; y++) {
          if (a[y][j] == ch)
            c++;
          else
            break;
        }
        if (c >= n) {
          for (int y = i;; y++) {
            if (a[y][j] == ch)
              r[y][j] = true;
            else
              break;
          }
        }
      }
    }
  }
  bool ok = false;
  FOR(i, 40) {
    FOR(j, 40) {
      if (r[i][j]) {
        a[i][j] = '.';
        ok = true;
      }
    }
  }
  return ok;
}

void fall(P &a) {
  bool ok = true;
  while (ok) {
    ok = false;
    FORI(i, 2, h + 1) {
      FORI(j, 1, w + 1) {
        if (a[i][j] == '.' && a[i - 1][j] != '.') {
          swap(a[i][j], a[i - 1][j]);
          ok = true;
        }
      }
    }
  }
}

int main() {

  cin >> h >> w >> n;

  vc wall(40, '!');
  P a(40, wall);

  FORI(i, 1, h + 1) {
    FORI(j, 1, w + 1) { cin >> a[i][j]; }
  }

  FORI(i, 1, h + 1) {
    FORI(j, 1, w) {
      if (a[i][j] != a[i][j + 1]) {
        P x = a;
        swap(x[i][j], x[i][j + 1]);
        fall(x);
        while (remove(x)) {
          fall(x);
        }

        if (check(x)) {
          cout << "YES" << endl;
          return 0;
        }
      }
    }
  }

  cout << "NO" << endl;
}

Error: 0

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS 1
#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <forward_list>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define INF ((int)(1 << 25))
#define REP(i, n) for (int i = 0; i < (int)n; i++)
#define FOR(i, n) REP(i, n)
#define FORI(i, k, n) for (int i = k; i < (int)n; i++)
#define TORAD 2.0 * M_PI / 360.0
#define INT(x)                                                                 \\
  int x;                                                                       \\
  cin >> x;
#define ALL(x) begin(x), end(x)
#define RALL(x) (x).rbegin(), (x).rend()
#define DEBUG(x) cout << #x << ":" << x << endl;
#define EPS 1e-12
#ifdef _MSC_VER
#include <intrin.h>
#define __builtin_popcount __popcnt
#endif
typedef long long lint;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
template <class T> void debug(T a) {
  for (auto i : a)
    cout << i << endl;
}

typedef vector<vc> P;
int h, w, n;
bool check(P &a) {
  int ans = 0;
  FORI(i, 1, h + 1) {
    FORI(j, 1, w + 1) {
      if (a[i][j] != '.')
        ans++;
    }
  }
  return !ans;
}

bool remove(P &a) {
  bool r[30][30] = {};
  FORI(i, 1, h + 1) {
    FORI(j, 1, w + 1) {
      if (a[i][j] != '.') {
        int c = 0;
        char ch = a[i][j];
        for (int x = j;; x++) {
          if (a[i][x] == ch)
            c++;
          else
            break;
        }
        if (c >= n) {
          for (int x = j;; x++) {
            if (a[i][x] == ch)
              r[i][x] = true;
            else
              break;
          }
        }
        c = 0;
        for (int y = i;; y++) {
          if (a[y][j] == ch)
            c++;
          else
            break;
        }
        if (c >= n) {
          for (int y = i;; y++) {
            if (a[y][j] == ch)
              r[y][j] = true;
            else
              break;
          }
        }
      }
    }
  }
  bool ok = false;
  FOR(i, 30) {
    FOR(j, 30) {
      if (r[i][j]) {
        a[i][j] = '.';
        ok = true;
      }
    }
  }
  return ok;
}

void fall(P &a) {
  bool ok = true;
  while (ok) {
    ok = false;
    FORI(i, 2, h + 1) {
      FORI(j, 1, w + 1) {
        if (a[i][j] == '.' && a[i - 1][j] != '.') {
          swap(a[i][j], a[i - 1][j]);
          ok = true;
        }
      }
    }
  }
}

int main() {

  cin >> h >> w >> n;

  vc wall(30, '!');
  P a(30, wall);

  FORI(i, 1, h + 1) {
    FORI(j, 1, w + 1) { cin >> a[i][j]; }
  }

  FORI(i, 1, h + 1) {
    FORI(j, 1, w) {
      if (a[i][j] != a[i][j + 1]) {
        P x = a;
        swap(x[i][j], x[i][j + 1]);
        fall(x);
        while (remove(x)) {
          fall(x);
        }

        if (check(x)) {
          cout << "YES" << endl;
          return 0;
        }
      }
    }
  }

  cout << "NO" << endl;
}