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

#include <bits/stdc++.h>

using namespace std;

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();
}
template <class T> inline T sqr(T x) { return x * x; }

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;

#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define mp make_pair
#define each(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define exist(s, e) ((s).find(e) != (s).end())
#define range(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) range(i, 0, n)
#define clr(a, b) memset((a), (b), sizeof(a))
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

const double eps = 1e-10;
const double pi = acos(-1.0);
const ll INF = 1LL << 62;
const int inf = 1 << 29;

int h, w, n;
string o[35];

string t[35];
bool d[35][35];

bool proceed() {
  bool update = true;
  while (update) {
    update = false;
    for (int i = h - 2; i >= 0; --i)
      rep(j, w) {
        if (t[i][j] == '.')
          continue;
        int ni;
        for (ni = h - 1; ni > i; --ni)
          if (t[ni][j] == '.')
            break;
        if (ni != i)
          swap(t[i][j], t[ni][j]);
      }
    rep(i, 35) rep(j, 35) d[i][j] = false;
    rep(i, h) rep(j, w) {
      if (t[i][j] == '.')
        continue;
      char cur = t[i][j];
      if (i + n - 1 < h) {
        bool ok = true;
        rep(e, n) if (cur != t[i + e][j]) ok = false;
        if (ok) {
          update = true;
          rep(e, n) d[i + e][j] = true;
        }
      }
      if (j + n - 1 < w) {
        bool ok = true;
        rep(e, n) if (cur != t[i][j + e]) ok = false;
        if (ok) {
          update = true;
          rep(e, n) d[i][j + e] = true;
        }
      }
    }
    rep(i, h) rep(j, w) if (d[i][j]) t[i][j] = '.';
  }
  bool ok = true;
  rep(i, h) rep(j, w) if (t[i][j] != '.') ok = false;
  return ok;
}

int main(void) {
  cin >> h >> w >> n;
  rep(i, h) cin >> o[i];
  bool ans = false;
  rep(i, h) rep(j, w) {
    if (o[i][j] == '.')
      continue;
    rep(a, h) t[a] = o[a];
    if (j + 1 < w) {
      swap(t[i][j], t[i][j + 1]);
      ans |= proceed();
    }
  }
  if (ans)
    puts("YES");
  else
    puts("NO");
  return 0;
}

Error: 0
i = 1
j = 3
i+n-1 = 3
i = 2
j = 3
i+n-1 = 4
i = 3
j = 0
i+n-1 = 5
i = 3
j = 1
i+n-1 = 5
i = 3
j = 2
i+n-1 = 5
i = 3
j = 3
i+n-1 = 5
i = 3
j = 4
i+n-1 = 5
i = 3
j = 5
i+n-1 = 5
i = 1
j = 3
i+n-1 = 3
i = 2
j = 3
i+n-1 = 4
i = 3
j = 0
i+n-1 = 5
i = 3
j = 1
i+n-1 = 5
i = 3
j = 2
i+n-1 = 5
i = 3
j = 3
i+n-1 = 5
i = 3
j = 4
i+n-1 = 5
i = 3
j = 5
i+n-1 = 5
i = 2
j = 3
i+n-1 = 4
i = 3
j = 0
i+n-1 = 5
i = 3
j = 1
i+n-1 = 5
i = 3
j = 3
i+n-1 = 5
i = 1
j = 3
i+n-1 = 3
i = 2
j = 3
i+n-1 = 4
i = 3
j = 0
i+n-1 = 5
i = 3
j = 1
i+n-1 = 5
i = 3
j = 2
i+n-1 = 5
i = 3
j = 3
i+n-1 = 5
i = 3
j = 4
i+n-1 = 5
i = 3
j = 5
i+n-1 = 5
i = 1
j = 3
i+n-1 = 3
i = 2
j = 3
i+n-1 = 4
i = 3
j = 0
i+n-1 = 5
i = 3
j = 1
i+n-1 = 5
i = 3
j = 2
i+n-1 = 5
i = 3
j = 3
i+n-1 = 5
i = 3
j = 4
i+n-1 = 5
i = 3
j = 5
i+n-1 = 5
i = 1
j = 3
i+n-1 = 3
i = 2
j = 3
i+n-1 = 4
i = 3
j = 0
i+n-1 = 5
i = 3
j = 1
i+n-1 = 5
i = 3
j = 2
i+n-1 = 5
i = 3
j = 3
i+n-1 = 5
i = 3
j = 4
i+n-1 = 5
i = 3
j = 5
i+n-1 = 5
i = 1
j = 3
i+n-1 = 3
i = 2
j = 3
i+n-1 = 4
i = 3
j = 0
i+n-1 = 5
i = 3
j = 1
i+n-1 = 5
i = 3
j = 2
i+n-1 = 5
i = 3
j = 3
i+n-1 = 5
i = 3
j = 4
i+n-1 = 5
i = 3
j = 5
i+n-1 = 5
i = 3
j = 0
i+n-1 = 5
i = 3
j = 1
i+n-1 = 5
i = 3
j = 2
i+n-1 = 5
i = 3
j = 4
i+n-1 = 5
i = 3
j = 5
i+n-1 = 5
i = 1
j = 3
i+n-1 = 3
i = 2
j = 3
i+n-1 = 4
i = 3
j = 0
i+n-1 = 5
i = 3
j = 1
i+n-1 = 5
i = 3
j = 2
i+n-1 = 5
i = 3
j = 3
i+n-1 = 5
i = 3
j = 4
i+n-1 = 5
i = 3
j = 5
i+n-1 = 5


Fixed submission:

#include <bits/stdc++.h>

using namespace std;

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();
}
template <class T> inline T sqr(T x) { return x * x; }

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;

#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define mp make_pair
#define each(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define exist(s, e) ((s).find(e) != (s).end())
#define range(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) range(i, 0, n)
#define clr(a, b) memset((a), (b), sizeof(a))
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

const double eps = 1e-10;
const double pi = acos(-1.0);
const ll INF = 1LL << 62;
const int inf = 1 << 29;

int h, w, n;
string o[35];

string t[35];
bool d[35][35];

bool proceed() {
  bool update = true;
  while (update) {
    update = false;
    for (int i = h - 2; i >= 0; --i)
      rep(j, w) {
        if (t[i][j] == '.')
          continue;
        int ni;
        for (ni = h - 1; ni > i; --ni)
          if (t[ni][j] == '.')
            break;
        if (ni != i)
          swap(t[i][j], t[ni][j]);
      }
    rep(i, 35) rep(j, 35) d[i][j] = false;
    rep(i, h) rep(j, w) {
      if (t[i][j] == '.')
        continue;
      char cur = t[i][j];
      dump(i) dump(j) dump(i + n - 1) if (i + n - 1 < h) {
        bool ok = true;
        rep(e, n) if (cur != t[i + e][j]) ok = false;
        if (ok) {
          update = true;
          rep(e, n) d[i + e][j] = true;
        }
      }
      if (j + n - 1 < w) {
        bool ok = true;
        rep(e, n) if (cur != t[i][j + e]) ok = false;
        if (ok) {
          update = true;
          rep(e, n) d[i][j + e] = true;
        }
      }
    }
    rep(i, h) rep(j, w) if (d[i][j]) t[i][j] = '.';
  }
  bool ok = true;
  rep(i, h) rep(j, w) if (t[i][j] != '.') ok = false;
  return ok;
}

int main(void) {
  cin >> h >> w >> n;
  rep(i, h) cin >> o[i];
  bool ans = false;
  rep(i, h) rep(j, w) {
    if (o[i][j] == '.')
      continue;
    rep(a, h) t[a] = o[a];
    if (i + 1 < h && o[i + 1][j] != '.') {
      swap(t[i][j], t[i + 1][j]);
      ans |= proceed();
    }
    rep(a, h) t[a] = o[a];
    if (j + 1 < w && o[i][j + 1] != '.') {
      swap(t[i][j], t[i][j + 1]);
      ans |= proceed();
    }
  }
  if (ans)
    puts("YES");
  else
    puts("NO");
  return 0;
}