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

#include <algorithm>
#include <array>
#include <complex>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)

typedef long long int ll;
typedef double D;     // ??§?¨?????????????double???long double?????????
typedef complex<D> P; // Point
typedef pair<P, P> L; // Line
typedef vector<P> VP;

const int INF = (1 << 30) - 1;
const D EPS = 1e-9;

#define X real()
#define Y imag()
#define LE(n, m) ((n) < (m) + EPS)
#define GE(n, m) ((n) + EPS > (m))
#define EQ(n, m) (abs((n) - (m)) < EPS)

int H, W, N;
vector<string> original_field;
vector<string> field;

int dh[] = {1, 0};
int dw[] = {0, 1};

inline bool in_field(int h, int w) {
  return 0 <= h && h < H && 0 <= w && w < W;
}

bool vanish() {
  //?¶?????????????true?????????
  vector<vector<bool>> del_field(H, vector<bool>(W));

  bool updated = false;
  for (int h = 0; h < H; h++) {
    for (int w = 0; w < W; w++) {
      if (field[h][w] == '.')
        continue;

      char color = field[h][w];
      for (int d = 0; d < 2; d++) {
        int l = 1;
        while (in_field(h + dh[d] * l, w + dw[d] * l) &&
               field[h + dh[d] * l][w + dw[d] * l] == color) {
          l++;
        }
        if (l < N)
          continue;

        for (int i = 0; i < l; i++) {
          del_field[h + dh[d] * i][w + dw[d] * i] = true;
          updated = true;
        }
      }
    }
  }

  if (updated)
    for (int h = 0; h < H; h++)
      for (int w = 0; w < W; w++)
        if (del_field[h][w])
          field[h][w] = '.';

  return updated;
}

void down() {
  for (int w = 0; w < W; w++) {
    int top = -1;
    for (int h = H - 1; h >= 0; h--) {
      if (field[h][w] == '.')
        continue;
      if (top >= 0)
        top--;
      else {
        int hi = h + 1;
        while (hi < H && field[hi][w] == '.')
          hi++;
        top = hi - 1;
      }
      swap(field[top][w], field[h][w]);
    }
  }
}

bool check() {
  down();
  while (vanish())
    down();

  for (int h = 0; h < H; h++)
    for (int w = 0; w < W; w++)
      if (field[h][w] != '.')
        return false;

  return true;
}

string solve() {
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      int nh = i, nw = j + 1;
      if (!in_field(nh, nw))
        continue;

      field = original_field;
      swap(field[i][j], field[nh][nw]);
      if (check())
        return "YES";
    }
  }
  return "NO";
}

// g++ -std=c++0x -msse4.2 -O3
// #include <bits/stdc++.h>
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  // cout.precision(16);

  cin >> H >> W >> N;
  original_field = vector<string>(H);
  for (string &r : original_field)
    cin >> r;

  cout << solve() << endl;

  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <array>
#include <complex>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)

typedef long long int ll;
typedef double D;     // ??§?¨?????????????double???long double?????????
typedef complex<D> P; // Point
typedef pair<P, P> L; // Line
typedef vector<P> VP;

const int INF = (1 << 30) - 1;
const D EPS = 1e-9;

#define X real()
#define Y imag()
#define LE(n, m) ((n) < (m) + EPS)
#define GE(n, m) ((n) + EPS > (m))
#define EQ(n, m) (abs((n) - (m)) < EPS)

int H, W, N;
vector<string> original_field;
vector<string> field;

int dh[] = {1, 0};
int dw[] = {0, 1};

inline bool in_field(int h, int w) {
  return 0 <= h && h < H && 0 <= w && w < W;
}

bool vanish() {
  //?¶?????????????true?????????
  vector<vector<bool>> del_field(H, vector<bool>(W));

  bool updated = false;
  for (int h = 0; h < H; h++) {
    for (int w = 0; w < W; w++) {
      if (field[h][w] == '.')
        continue;

      char color = field[h][w];
      for (int d = 0; d < 2; d++) {
        int l = 1;
        while (in_field(h + dh[d] * l, w + dw[d] * l) &&
               field[h + dh[d] * l][w + dw[d] * l] == color) {
          l++;
        }
        if (l < N)
          continue;

        for (int i = 0; i < l; i++) {
          del_field[h + dh[d] * i][w + dw[d] * i] = true;
          updated = true;
        }
      }
    }
  }

  if (updated)
    for (int h = 0; h < H; h++)
      for (int w = 0; w < W; w++)
        if (del_field[h][w])
          field[h][w] = '.';

  return updated;
}

void down() {
  int top = -1;
  for (int w = 0; w < W; w++) {
    for (int h = H - 1; h >= 0; h--) {
      if (field[h][w] == '.')
        continue;
      if (top >= 0)
        top++;
      else {
        int hi = h + 1;
        while (hi < H && field[hi][w] == '.')
          hi++;
        top = hi - 1;
      }
      swap(field[top][w], field[h][w]);
    }
  }
}

bool check() {
  down();
  while (vanish())
    down();

  for (int h = 0; h < H; h++)
    for (int w = 0; w < W; w++)
      if (field[h][w] != '.')
        return false;

  return true;
}

string solve() {
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      int nh = i, nw = j + 1;
      if (!in_field(nh, nw))
        continue;

      field = original_field;
      swap(field[i][j], field[nh][nw]);
      if (check())
        return "YES";
    }
  }
  return "NO";
}

// g++ -std=c++0x -msse4.2 -O3
// #include <bits/stdc++.h>
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  // cout.precision(16);

  cin >> H >> W >> N;
  original_field = vector<string>(H);
  for (string &r : original_field)
    cin >> r;

  cout << solve() << endl;

  return 0;
}