Problem p03053 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define _repargs(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define rep(...) _repargs(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define all(x) (x).begin(), (x).end()
#define mod 1000000007
#define inf 2000000007
#define mp make_pair
#define pb push_back
typedef long long ll;
using namespace std;
template <typename T> inline void output(T a, int p = 0) {
  if (p)
    cout << fixed << setprecision(p) << a << "\n";
  else
    cout << a << "\n";
}
// end of template

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

int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  // source code
  int H, W;
  cin >> H >> W;
  vector<string> A(H);
  vector<vector<int>> B(H, vector<int>(W, -1));
  rep(i, H) cin >> A[i];
  vector<pair<int, int>> Q;
  rep(i, H) rep(j, W) if (A[i][j] == '#') {
    B[i][j] = 0;
    Q.pb(mp(i, j));
  }
  vector<pair<int, int>> R;
  rep(c, 2222) {
    for (auto q : Q) {
      rep(d, 4) {
        if (q.first + dy[d] >= 0 && q.first + dy[d] < H &&
            q.second + dx[d] >= 0 && q.second + dx[d] < W) {
          if (B[q.first + dy[d]][q.second + dx[d]] < 0) {
            B[q.first + dy[d]][q.second + dx[d]] = c + 1;
            R.pb(mp(q.first + dy[d], q.second + dx[d]));
          }
        }
      }
    }
    Q = R;
  }

  int ans = 0;
  rep(i, H) rep(j, W) ans = max(ans, B[i][j]);
  output(ans);

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define _repargs(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define rep(...) _repargs(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define all(x) (x).begin(), (x).end()
#define mod 1000000007
#define inf 2000000007
#define mp make_pair
#define pb push_back
typedef long long ll;
using namespace std;
template <typename T> inline void output(T a, int p = 0) {
  if (p)
    cout << fixed << setprecision(p) << a << "\n";
  else
    cout << a << "\n";
}
// end of template

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

int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  // source code
  int H, W;
  cin >> H >> W;
  vector<string> A(H);
  vector<vector<int>> B(H, vector<int>(W, -1));
  rep(i, H) cin >> A[i];
  vector<pair<int, int>> Q;
  rep(i, H) rep(j, W) if (A[i][j] == '#') {
    B[i][j] = 0;
    Q.pb(mp(i, j));
  }
  vector<pair<int, int>> R;
  rep(c, 2222) {
    for (auto q : Q) {
      rep(d, 4) {
        if (q.first + dy[d] >= 0 && q.first + dy[d] < H &&
            q.second + dx[d] >= 0 && q.second + dx[d] < W) {
          if (B[q.first + dy[d]][q.second + dx[d]] < 0) {
            B[q.first + dy[d]][q.second + dx[d]] = c + 1;
            R.pb(mp(q.first + dy[d], q.second + dx[d]));
          }
        }
      }
    }
    Q = R;
    R.clear();
  }

  int ans = 0;
  rep(i, H) rep(j, W) ans = max(ans, B[i][j]);
  output(ans);

  return 0;
}