Problem p03053 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
#define _USE_MATH_DEFINES
#include <math.h>

using namespace std;

using LL = long long;
using VLL = std::vector<LL>;
using VVLL = std::vector<VLL>;
using VVVLL = std::vector<VVLL>;
using LD = long double;
using VLD = std::vector<LD>;
using VVLD = std::vector<VLD>;
using VVVLD = std::vector<VVLD>;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

#define REP(i, n) for (LL(i) = 0; (i) < (n); (i)++)
#define ALL(v) v.begin(), v.end()
#define PRINTLN(v)                                                             \
  {                                                                            \
    LL i = 0;                                                                  \
    for (auto(a) : v) {                                                        \
      cerr << i << " : " << a << endl;                                         \
      i++;                                                                     \
    }                                                                          \
  }
#define PRINTSP(v)                                                             \
  for (auto(a) : v) {                                                          \
    cerr << a << " ";                                                          \
  }                                                                            \
  cerr << endl
#define cmax(a, b)                                                             \
  {                                                                            \
    if (a < b) {                                                               \
      a = b;                                                                   \
    }                                                                          \
  }
#define cmin(a, b)                                                             \
  {                                                                            \
    if (a > b) {                                                               \
      a = b;                                                                   \
    }                                                                          \
  }
#define EACH(a, A) for (auto a : A)
// #define cmax(a,b) a = (a>b ? a:b)
// #define cmin(a,b) a = (a<b ? a:b)

void func() {
  LL H, W;
  cin >> H >> W;
  vector<string> M(H);
  REP(i, H) { cin >> M[i]; }

  queue<pair<LL, LL>> Q;
  set<pair<LL, LL>> S;
  REP(i, H) {
    REP(j, W) {
      if (M[i][j] == '#') {
        Q.emplace(i, j);
        S.emplace(i, j);
      }
    }
  }

  LL cnt = 0;
  while (not S.empty()) {
    cerr << cnt << endl;
    EACH(m, M) { PRINTSP(m); }

    set<pair<LL, LL>> T;
    for (auto s : S) {
      LL y = s.first;
      LL x = s.second;
      if (y + 1 < H and M[y + 1][x] == '.') {
        M[y + 1][x] = '#';
        T.emplace(y + 1, x);
      }
      if (x + 1 < W and M[y][x + 1] == '.') {
        M[y][x + 1] = '#';
        T.emplace(y, x + 1);
      }
      if (y - 1 >= 0 and M[y - 1][x] == '.') {
        M[y - 1][x] = '#';
        T.emplace(y - 1, x);
      }
      if (x - 1 >= 0 and M[y][x - 1] == '.') {
        M[y][x - 1] = '#';
        T.emplace(y, x - 1);
      }
    }
    S = T;
    cnt++;
  }
  EACH(m, M) {
    // PRINTSP(m);
  }
  cout << cnt - 1;
  return;
}

int main() {
  func();
  return 0;
}


Fixed submission:
#include "bits/stdc++.h"
#define _USE_MATH_DEFINES
#include <math.h>

using namespace std;

using LL = long long;
using VLL = std::vector<LL>;
using VVLL = std::vector<VLL>;
using VVVLL = std::vector<VVLL>;
using LD = long double;
using VLD = std::vector<LD>;
using VVLD = std::vector<VLD>;
using VVVLD = std::vector<VVLD>;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

#define REP(i, n) for (LL(i) = 0; (i) < (n); (i)++)
#define ALL(v) v.begin(), v.end()
#define PRINTLN(v)                                                             \
  {                                                                            \
    LL i = 0;                                                                  \
    for (auto(a) : v) {                                                        \
      cerr << i << " : " << a << endl;                                         \
      i++;                                                                     \
    }                                                                          \
  }
#define PRINTSP(v)                                                             \
  for (auto(a) : v) {                                                          \
    cerr << a << " ";                                                          \
  }                                                                            \
  cerr << endl
#define cmax(a, b)                                                             \
  {                                                                            \
    if (a < b) {                                                               \
      a = b;                                                                   \
    }                                                                          \
  }
#define cmin(a, b)                                                             \
  {                                                                            \
    if (a > b) {                                                               \
      a = b;                                                                   \
    }                                                                          \
  }
#define EACH(a, A) for (auto a : A)
// #define cmax(a,b) a = (a>b ? a:b)
// #define cmin(a,b) a = (a<b ? a:b)

void func() {
  LL H, W;
  cin >> H >> W;
  vector<string> M(H);
  REP(i, H) { cin >> M[i]; }
  set<pair<LL, LL>> S;
  REP(i, H) {
    REP(j, W) {
      if (M[i][j] == '#') {
        S.emplace(i, j);
      }
    }
  }

  LL cnt = 0;
  while (not S.empty()) {
    // cerr << cnt<< endl;
    EACH(m, M) {
      // PRINTSP(m);
    }

    set<pair<LL, LL>> T;
    for (auto s : S) {
      LL y = s.first;
      LL x = s.second;
      if (y + 1 < H and M[y + 1][x] == '.') {
        M[y + 1][x] = '#';
        // T.emplace(y+1,x);
        T.insert(make_pair(y + 1, x));
      }
      if (x + 1 < W and M[y][x + 1] == '.') {
        M[y][x + 1] = '#';
        // T.emplace(y,x+1);
        T.insert(make_pair(y, x + 1));
      }
      if (y - 1 >= 0 and M[y - 1][x] == '.') {
        M[y - 1][x] = '#';
        // T.emplace(y-1,x);
        T.insert(make_pair(y - 1, x));
      }
      if (x - 1 >= 0 and M[y][x - 1] == '.') {
        M[y][x - 1] = '#';
        // T.emplace(y,x-1);
        T.insert(make_pair(y, x - 1));
      }
    }
    S = T;
    cnt++;
  }
  EACH(m, M) {
    // PRINTSP(m);
  }
  cout << cnt - 1;
  return;
}

void _func() {
  LL H, W;
  cin >> H >> W;
  vector<string> M(H);
  REP(i, H) { cin >> M[i]; }

  queue<pair<LL, LL>> Q;
  set<pair<LL, LL>> S;
  REP(i, H) {
    REP(j, W) {
      if (M[i][j] == '#') {
        Q.emplace(i, j);
        S.emplace(i, j);
      }
    }
  }

  LL cnt = 0;
  while (not S.empty()) {
    cerr << cnt << endl;
    EACH(m, M) { PRINTSP(m); }

    set<pair<LL, LL>> T;
    for (auto s : S) {
      LL y = s.first;
      LL x = s.second;
      if (y + 1 < H and M[y + 1][x] == '.') {
        M[y + 1][x] = '#';
        T.emplace(y + 1, x);
      }
      if (x + 1 < W and M[y][x + 1] == '.') {
        M[y][x + 1] = '#';
        T.emplace(y, x + 1);
      }
      if (y - 1 >= 0 and M[y - 1][x] == '.') {
        M[y - 1][x] = '#';
        T.emplace(y - 1, x);
      }
      if (x - 1 >= 0 and M[y][x - 1] == '.') {
        M[y][x - 1] = '#';
        T.emplace(y, x - 1);
      }
    }
    S = T;
    cnt++;
  }
  EACH(m, M) {
    // PRINTSP(m);
  }
  cout << cnt - 1;
  return;
}

int main() {
  func();
  return 0;
}