Problem p03053 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll mod = 1e9 + 7;
#define rep(i, n) for (int i = 0; i < (n); ++i)

int main() {
  int h, w;
  cin >> h >> w;
  vector<vector<char>> hw(h, vector<char>(w));
  int reach_cnt[h][w];
  queue<pair<int, int>> que;
  rep(i, h) rep(j, w) {
    cin >> hw[i][j];
    reach_cnt[i][j] = -1;
    if (hw[i][j] == '#') {
      que.push(make_pair(i, j));
      reach_cnt[i][j] = 0;
    }
  }
  int dx[4] = {1, -1, 0, 0};
  int dy[4] = {0, 0, 1, -1};
  int ans = 0;
  while (!que.empty()) {
    int size = que.size();
    //        cout << "size " << size << endl;
    rep(rj, size) {
      pair<int, int> now_p = que.front();
      que.pop();
      rep(ri, 4) {
        int next_y = now_p.first + dy[ri];
        int next_x = now_p.second + dx[ri];
        if (next_y >= h || next_y < 0 || next_x >= w || next_x < 0) {
          continue;
        }
        if (reach_cnt[next_y][next_x] != -1) {
          continue;
        }
        reach_cnt[next_y][next_x] = reach_cnt[now_p.first][now_p.second] + 1;
        //                cout << "test" << endl;
        //                hw[next_y][next_x] = '#';
        que.push(make_pair(next_y, next_x));
        ans = max(ans, reach_cnt[next_y][next_x]);
      }
    }
    bool end_flg = 1;
    rep(i, h) {
      rep(j, w) {
        //                cout << hw[i][j];
        //                cout << reach_flg[i][j] << " ";
        if (reach_cnt[i][j] == -1) {
          end_flg = 0;
        }
      }
      //            cout << endl;
    }
    if (end_flg)
      break;
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll mod = 1e9 + 7;
#define rep(i, n) for (int i = 0; i < (n); ++i)

int main() {
  int h, w;
  cin >> h >> w;
  vector<vector<char>> hw(h, vector<char>(w));
  int reach_cnt[h][w];
  queue<pair<int, int>> que;
  rep(i, h) rep(j, w) {
    cin >> hw[i][j];
    reach_cnt[i][j] = -1;
    if (hw[i][j] == '#') {
      que.push(make_pair(i, j));
      reach_cnt[i][j] = 0;
    }
  }
  int dx[4] = {1, -1, 0, 0};
  int dy[4] = {0, 0, 1, -1};
  int ans = 0;
  while (!que.empty()) {
    int size = que.size();
    //        cout << "size " << size << endl;
    rep(rj, size) {
      pair<int, int> now_p = que.front();
      que.pop();
      rep(ri, 4) {
        int next_y = now_p.first + dy[ri];
        int next_x = now_p.second + dx[ri];
        if (next_y >= h || next_y < 0 || next_x >= w || next_x < 0) {
          continue;
        }
        if (reach_cnt[next_y][next_x] != -1) {
          continue;
        }
        reach_cnt[next_y][next_x] = reach_cnt[now_p.first][now_p.second] + 1;
        //                cout << "test" << endl;
        //                hw[next_y][next_x] = '#';
        que.push(make_pair(next_y, next_x));
        ans = max(ans, reach_cnt[next_y][next_x]);
      }
    }
    //        bool end_flg = 1;
    //        rep(i, h) {
    //            rep(j, w) {
    ////                cout << hw[i][j];
    ////                cout << reach_flg[i][j] << " ";
    //                if ( reach_cnt[i][j] == -1 ) {
    //                    end_flg = 0;
    //                }
    //            }
    //            cout << endl;
    //        }
    //        if ( end_flg ) break;
  }
  cout << ans << endl;
  return 0;
}