Problem p03053 (C++)

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

#define rep(i, n) for (int i = 0; i < int(n); i++)
#define rrep(i, n) for (int i = int(n) - 1; i >= 0; i--)
#define reps(i, n) for (int i = 1; i <= int(n); i++)
#define rreps(i, n) for (int i = int(n); i >= 1; i--)
#define repi(i, a, b) for (int i = (a); i < int(b); i++)
#define all(a) (a).begin(), (a).end()
#define bit(b) (1ll << (b))
#define uniq(v) (v).erase(unique(all(v)), (v).end())
#define rsort(v)                                                               \
  sort(all(v));                                                                \
  reverse(all(v))

using namespace std;
using i32 = int;
using i64 = long long;
using f80 = long double;
using vi32 = vector<i32>;
using vi64 = vector<i64>;
using vf80 = vector<f80>;
using vstr = vector<string>;

inline void yes() {
  cout << "Yes" << '\n';
  exit(0);
}
inline void no() {
  cout << "No" << '\n';
  exit(0);
}
inline i64 gcd(i64 a, i64 b) {
  if (min(a, b) == 0)
    return max(a, b);
  if (a % b == 0)
    return b;
  return gcd(b, a % b);
}
inline i64 lcm(i64 a, i64 b) { return a / gcd(a, b) * b; }
inline bool isprime(i32 n) {
  if (n < 2)
    return 0;
  for (i32 i = 2; i * i <= n; i++)
    if (n % i == 0)
      return 0;
  return 1;
}
inline vi32 plist(i32 n) {
  vi32 f(n, 1);
  f[0] = f[1] = 0;
  repi(i, 2, n) for (int j = i * 2; j < n; j += i) f[j] = 0;
  return f;
}
inline void ioinit() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout << fixed << setprecision(16);
}
template <class T>
class pqasc : public priority_queue<T, vector<T>, greater<T>> {};
template <class T>
class pqdesc : public priority_queue<T, vector<T>, less<T>> {};
template <class T> inline bool amax(T &x, T y) {
  if (x >= y)
    return 0;
  x = y;
  return 1;
}
template <class T> inline bool amin(T &x, T y) {
  if (x <= y)
    return 0;
  x = y;
  return 1;
}
template <class T> inline T power(T x, i64 n) {
  T r = 1;
  while (n > 0) {
    if (n & 1)
      r *= x;
    x *= x;
    n >>= 1;
  }
  return r;
}
template <class T> vector<T> make_v(int n, T v) { return vector<T>(n, v); }
template <class... Ts> auto make_v(int n, Ts... ts) {
  return vector<decltype(make_v(ts...))>(n, make_v(ts...));
}

int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};

int main() {
  ioinit();
  int h, w;
  cin >> h >> w;
  vstr a(h);
  rep(i, h) cin >> a[i];
  using P = pair<int, int>;
  queue<P> q;
  auto board = make_v(h, w, (int)1e9);
  rep(i, h) rep(j, w) if (a[i][j] == '#') {
    q.emplace(i, j);
    board[i][j] = 0;
  }
  while (!q.empty()) {
    int i, j;
    tie(i, j) = q.front(), q.pop();
    rep(k, 4) {
      int fx = i + dx[k], fy = j + dy[k];
      if (fx < 0 || fx >= h || fy < 0 || fy >= w)
        continue;
      if (board[fx][fy] < board[i][j] + 1)
        continue;
      board[fx][fy] = board[i][j] + 1;
      q.emplace(fx, fy);
    }
  }
  int ans = 0;
  rep(i, h) rep(j, w) amax(ans, board[i][j]);
  cout << ans << '\n';
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < int(n); i++)
#define rrep(i, n) for (int i = int(n) - 1; i >= 0; i--)
#define reps(i, n) for (int i = 1; i <= int(n); i++)
#define rreps(i, n) for (int i = int(n); i >= 1; i--)
#define repi(i, a, b) for (int i = (a); i < int(b); i++)
#define all(a) (a).begin(), (a).end()
#define bit(b) (1ll << (b))
#define uniq(v) (v).erase(unique(all(v)), (v).end())
#define rsort(v)                                                               \
  sort(all(v));                                                                \
  reverse(all(v))

using namespace std;
using i32 = int;
using i64 = long long;
using f80 = long double;
using vi32 = vector<i32>;
using vi64 = vector<i64>;
using vf80 = vector<f80>;
using vstr = vector<string>;

inline void yes() {
  cout << "Yes" << '\n';
  exit(0);
}
inline void no() {
  cout << "No" << '\n';
  exit(0);
}
inline i64 gcd(i64 a, i64 b) {
  if (min(a, b) == 0)
    return max(a, b);
  if (a % b == 0)
    return b;
  return gcd(b, a % b);
}
inline i64 lcm(i64 a, i64 b) { return a / gcd(a, b) * b; }
inline bool isprime(i32 n) {
  if (n < 2)
    return 0;
  for (i32 i = 2; i * i <= n; i++)
    if (n % i == 0)
      return 0;
  return 1;
}
inline vi32 plist(i32 n) {
  vi32 f(n, 1);
  f[0] = f[1] = 0;
  repi(i, 2, n) for (int j = i * 2; j < n; j += i) f[j] = 0;
  return f;
}
inline void ioinit() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout << fixed << setprecision(16);
}
template <class T>
class pqasc : public priority_queue<T, vector<T>, greater<T>> {};
template <class T>
class pqdesc : public priority_queue<T, vector<T>, less<T>> {};
template <class T> inline bool amax(T &x, T y) {
  if (x >= y)
    return 0;
  x = y;
  return 1;
}
template <class T> inline bool amin(T &x, T y) {
  if (x <= y)
    return 0;
  x = y;
  return 1;
}
template <class T> inline T power(T x, i64 n) {
  T r = 1;
  while (n > 0) {
    if (n & 1)
      r *= x;
    x *= x;
    n >>= 1;
  }
  return r;
}
template <class T> vector<T> make_v(int n, T v) { return vector<T>(n, v); }
template <class... Ts> auto make_v(int n, Ts... ts) {
  return vector<decltype(make_v(ts...))>(n, make_v(ts...));
}

int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};

int main() {
  ioinit();
  int h, w;
  cin >> h >> w;
  vstr a(h);
  rep(i, h) cin >> a[i];
  using P = pair<int, int>;
  queue<P> q;
  auto board = make_v(h, w, (int)1e9);
  rep(i, h) rep(j, w) if (a[i][j] == '#') {
    q.emplace(i, j);
    board[i][j] = 0;
  }
  while (!q.empty()) {
    int i, j;
    tie(i, j) = q.front(), q.pop();
    rep(k, 4) {
      int fx = i + dx[k], fy = j + dy[k];
      if (fx < 0 || fx >= h || fy < 0 || fy >= w)
        continue;
      if (board[fx][fy] <= board[i][j] + 1)
        continue;
      board[fx][fy] = board[i][j] + 1;
      q.emplace(fx, fy);
    }
  }
  int ans = 0;
  rep(i, h) rep(j, w) amax(ans, board[i][j]);
  cout << ans << '\n';
  return 0;
}