Problem p01349 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#define N 32
using namespace std;
int Erase();
int Cnt(int, int, string);
int h, w, n;
string cp[N];
int main() {
  int r, flag, i, j, k;
  string in[N];
  cin >> h >> w >> n;
  for (i = 0; i <= w + 1; i++)
    in[0] += 'x', in[h + 1] += 'x';
  for (i = 1; i <= h; i++)
    cin >> in[i], in[i] = 'x' + in[i] + 'x';
  flag = 0;
  for (k = 0; k <= h + 1; k++)
    cp[k] = in[k];
  for (i = 1; i <= h; i++) {
    for (j = 1; j <= w; j++) {
      if (cp[i][j] != cp[i][j + 1]) {
        swap(cp[i][j], cp[i][j + 1]);
        r = Erase();
        if (r == 0) {
          flag = 1;
          break;
        }
        for (k = 0; k <= h + 1; k++)
          cp[k] = in[k];
      }
    }
    if (flag == 1)
      break;
  }
  if (flag == 1)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
  return 0;
}
int Erase() {
  int i, j, x, y, cnt, flag, flag2, flag3, mark[h + 2][w + 2];
  while (1) {
    while (1) {
      flag2 = 0;
      for (i = h - 1; i >= 1; i--) {
        for (j = 1; j <= w; j++) {
          if ('A' <= cp[i][j] && cp[i][j] <= 'Z' && cp[i + 1][j] == '.') {
            swap(cp[i][j], cp[i + 1][j]);
            flag2 = 1;
          }
        }
      }
      if (flag2 == 0)
        break;
    }
    flag = 0;
    for (i = 1; i <= h; i++) {
      for (j = 1; j <= w; j++) {
        mark[i][j] = 0;
      }
    }
    for (i = 1; i <= h; i++) {
      for (j = 1; j <= w; j++) {
        if (cp[i][j] != '.') {
          if (cp[i][j] == cp[i][j + 1]) {
            cnt = Cnt(i, j, "Right");
            if (cnt >= n) {
              flag = 1;
              y = i;
              x = j;
              while (cnt) {
                mark[y][x] = 1;
                x++;
                cnt--;
              }
            }
          }
          if (cp[i][j] == cp[i + 1][j]) {
            cnt = Cnt(i, j, "Down");
            if (cnt >= n) {
              flag = 1;
              y = i;
              x = j;
              while (cnt) {
                mark[y][x] = 1;
                y++;
                cnt--;
              }
            }
          }
        }
      }
    }
    if (flag == 0)
      break;
    for (i = 1; i <= h; i++) {
      for (j = 1; j <= w; j++) {
        if (mark[i][j] == 1)
          cp[i][j] = '.';
      }
    }
  }
  flag3 = 0;
  for (i = 1; i <= h; i++) {
    for (j = 1; j <= w; j++) {
      if (cp[i][j] != '.') {
        flag3 = 1;
        break;
      }
    }
  }
  if (flag3 == 0)
    return 0;
  return 1;
}
int Cnt(int y, int x, string d) {
  int cnt = 0, py, px;
  char t = cp[y][x];
  if (d == "Right")
    py = 0, px = 1;
  else
    py = 1, px = 0;
  while (1) {
    if (cp[y][x] == t) {
      cnt++;
    } else
      break;
    y += py, x += px;
  }
  return cnt;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#define N 35
using namespace std;
int Erase();
int Cnt(int, int, string);
int h, w, n;
string cp[N];
int main() {
  int r, flag, i, j, k;
  string in[N];
  cin >> h >> w >> n;
  for (i = 0; i <= w + 1; i++)
    in[0] += 'x', in[h + 1] += 'x';
  for (i = 1; i <= h; i++)
    cin >> in[i], in[i] = 'x' + in[i] + 'x';
  flag = 0;
  for (k = 0; k <= h + 1; k++)
    cp[k] = in[k];
  for (i = 1; i <= h; i++) {
    for (j = 1; j <= w; j++) {
      if (cp[i][j] != cp[i][j + 1]) {
        swap(cp[i][j], cp[i][j + 1]);
        r = Erase();
        if (r == 0) {
          flag = 1;
          break;
        }
        for (k = 0; k <= h + 1; k++)
          cp[k] = in[k];
      }
    }
    if (flag == 1)
      break;
  }
  if (flag == 1)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
  return 0;
}
int Erase() {
  int i, j, x, y, cnt, flag, flag2, flag3, mark[h + 2][w + 2];
  while (1) {
    while (1) {
      flag2 = 0;
      for (i = h - 1; i >= 1; i--) {
        for (j = 1; j <= w; j++) {
          if ('A' <= cp[i][j] && cp[i][j] <= 'Z' && cp[i + 1][j] == '.') {
            swap(cp[i][j], cp[i + 1][j]);
            flag2 = 1;
          }
        }
      }
      if (flag2 == 0)
        break;
    }
    flag = 0;
    for (i = 1; i <= h; i++) {
      for (j = 1; j <= w; j++) {
        mark[i][j] = 0;
      }
    }
    for (i = 1; i <= h; i++) {
      for (j = 1; j <= w; j++) {
        if (cp[i][j] != '.') {
          if (cp[i][j] == cp[i][j + 1]) {
            cnt = Cnt(i, j, "Right");
            if (cnt >= n) {
              flag = 1;
              y = i;
              x = j;
              while (cnt) {
                mark[y][x] = 1;
                x++;
                cnt--;
              }
            }
          }
          if (cp[i][j] == cp[i + 1][j]) {
            cnt = Cnt(i, j, "Down");
            if (cnt >= n) {
              flag = 1;
              y = i;
              x = j;
              while (cnt) {
                mark[y][x] = 1;
                y++;
                cnt--;
              }
            }
          }
        }
      }
    }
    if (flag == 0)
      break;
    for (i = 1; i <= h; i++) {
      for (j = 1; j <= w; j++) {
        if (mark[i][j] == 1)
          cp[i][j] = '.';
      }
    }
  }
  flag3 = 0;
  for (i = 1; i <= h; i++) {
    for (j = 1; j <= w; j++) {
      if (cp[i][j] != '.') {
        flag3 = 1;
        break;
      }
    }
  }
  if (flag3 == 0)
    return 0;
  return 1;
}
int Cnt(int y, int x, string d) {
  int cnt = 0, py, px;
  char t = cp[y][x];
  if (d == "Right")
    py = 0, px = 1;
  else
    py = 1, px = 0;
  while (1) {
    if (cp[y][x] == t) {
      cnt++;
    } else
      break;
    y += py, x += px;
  }
  return cnt;
}