Problem p03053 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define MP make_pair
#define PB push_back
#define ALL(x) (x).begin(), (x).end()
#define REP(i, n) for (int i = 0; i < (n); i++)
#define REP1(i, n) for (int i = 1; i < (n); i++)
#define REP2(i, d, n) for (int i = (d); i < (n); i++)
#define RREP(i, n) for (int i = (n); i >= 0; i--)
#define CLR(a) memset((a), 0, sizeof(a))
#define MCLR(a) memset((a), -1, sizeof(a))
#define RANGE(x, y, maxX, maxY)                                                \
  (0 <= (x) && 0 <= (y) && (x) < (maxX) && (y) < (maxY))

typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef vector<LL> VLL;
typedef pair<int, int> PII;

const int INF = 0x3f3f3f3f;
const LL INFL = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-9;

const int DX[] = {1, 0, -1, 0}, DY[] = {0, -1, 0, 1};

int memo[1000][1000];

void solve(long long H, long long W, std::vector<std::string> A) {
  vector<PII> now, next;
  REP(i, H) REP(j, W) {
    if (A[i][j] == '#')
      now.PB(MP(i, j));
  }

  int cnt = -1;
  while (!now.empty()) {
    cnt++;

    REP(i, now.size()) {
      REP(j, 4) {
        int mx = now[i].second + DX[j];
        int my = now[i].first + DY[j];
        if (RANGE(mx, my, W, H) && A[my][mx] == '.') {
          next.PB(MP(my, mx));
          A[my][mx] = '#';
        }
      }
    }
    now = next;
    next.clear();
  }

  cout << cnt << endl;
}

int main() {
  long long H;
  scanf("%lld", &H);
  long long W;
  scanf("%lld", &W);
  std::vector<std::string> A(W);
  for (int i = 0; i < H; i++) {
    std::cin >> A[i];
  }
  solve(H, W, std::move(A));
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define MP make_pair
#define PB push_back
#define ALL(x) (x).begin(), (x).end()
#define REP(i, n) for (int i = 0; i < (n); i++)
#define REP1(i, n) for (int i = 1; i < (n); i++)
#define REP2(i, d, n) for (int i = (d); i < (n); i++)
#define RREP(i, n) for (int i = (n); i >= 0; i--)
#define CLR(a) memset((a), 0, sizeof(a))
#define MCLR(a) memset((a), -1, sizeof(a))
#define RANGE(x, y, maxX, maxY)                                                \
  (0 <= (x) && 0 <= (y) && (x) < (maxX) && (y) < (maxY))

typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef vector<LL> VLL;
typedef pair<int, int> PII;

const int INF = 0x3f3f3f3f;
const LL INFL = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-9;

const int DX[] = {1, 0, -1, 0}, DY[] = {0, -1, 0, 1};

int memo[1000][1000];

void solve(long long H, long long W, std::vector<std::string> A) {
  vector<PII> now, next;
  REP(i, H) REP(j, W) {
    if (A[i][j] == '#')
      now.PB(MP(i, j));
  }

  int cnt = -1;
  while (!now.empty()) {
    cnt++;

    REP(i, now.size()) {
      REP(j, 4) {
        int mx = now[i].second + DX[j];
        int my = now[i].first + DY[j];
        if (RANGE(mx, my, W, H) && A[my][mx] == '.') {
          next.PB(MP(my, mx));
          A[my][mx] = '#';
        }
      }
    }
    now = next;
    next.clear();
  }

  cout << cnt << endl;
}

int main() {
  long long H;
  scanf("%lld", &H);
  long long W;
  scanf("%lld", &W);
  std::vector<std::string> A(H);
  for (int i = 0; i < H; i++) {
    std::cin >> A[i];
  }
  solve(H, W, std::move(A));
  return 0;
}