Problem p03053 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (b); i >= (a); i--)
#define pb push_back
#define mp make_pair
#define bg begin()
#define en end()
#define all(v) (v).begin(), (v).end()
#define sz(v) (int)(v).size()

using namespace std;

typedef long long ll;

static const long long MOD = 1000000007;

int ans = -1, h, w, d[1005][1005];
char a[1005][1005];
int dx[] = {0, 0, -1, 1}, dy[] = {-1, 1, 0, 0};

int main(void) {
  cin >> h >> w;
  rep(i, 0, h - 1) scanf("%s", a[i]);
  memset(d, 0x3f, sizeof(d));

  queue<pair<int, int>> q;
  rep(i, 1, h) rep(j, 1, w) {
    if (a[i - 1][j - 1] == '#') {
      d[i][j] = 0;
      q.push(mp(i, j));
    }
  }

  while (!q.empty()) {
    auto p = q.front();
    q.pop();
    int sy = p.first, sx = p.second;
    rep(r, 0, 4) {
      int ty = sy + dy[r], tx = sx + dx[r];
      if (tx < 1 || tx > w || ty < 1 || ty > h)
        continue;
      if (d[sy][sx] + 1 < d[ty][tx]) {
        d[ty][tx] = d[sy][sx] + 1;
        q.push(mp(ty, tx));
      }
    }
  }

  /*
  rep(i, 1, h) rep(j, 1, w) {
      if (a[i-1][j-1]=='#') d[i][j] = 0;
  }

  自分の解答
  rep(hi, 1, h) {
      rep(wi, 1, w) d[hi][wi] = min(d[hi][wi], d[hi][wi-1]+1);
      per(wi, 1, w) d[hi][wi] = min(d[hi][wi], d[hi][wi+1]+1);
  }

  rep(wi, 1, w) {
      rep(hi, 1, h) d[hi][wi] = min(d[hi][wi], d[hi-1][wi]+1);
      per(hi, 1, h) d[hi][wi] = min(d[hi][wi], d[hi+1][wi]+1);
  }

  */

  rep(i, 1, h) rep(j, 1, w) ans = max(ans, d[i][j]);

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (b); i >= (a); i--)
#define pb push_back
#define mp make_pair
#define bg begin()
#define en end()
#define all(v) (v).begin(), (v).end()
#define sz(v) (int)(v).size()

using namespace std;

typedef long long ll;

static const long long MOD = 1000000007;

int ans = -1, h, w, d[1005][1005];
char a[1005][1005];
int dx[] = {0, 0, -1, 1}, dy[] = {-1, 1, 0, 0};

int main(void) {
  cin >> h >> w;
  rep(i, 0, h - 1) scanf("%s", a[i]);
  memset(d, 0x3f, sizeof(d));

  queue<pair<int, int>> q;
  rep(i, 1, h) rep(j, 1, w) {
    if (a[i - 1][j - 1] == '#') {
      d[i][j] = 0;
      q.push(mp(i, j));
    }
  }

  while (!q.empty()) {
    auto p = q.front();
    q.pop();
    int sy = p.first, sx = p.second;
    rep(r, 0, 3) {
      int ty = sy + dy[r], tx = sx + dx[r];
      if (tx < 1 || tx > w || ty < 1 || ty > h)
        continue;
      if (d[sy][sx] + 1 < d[ty][tx]) {
        d[ty][tx] = d[sy][sx] + 1;
        q.push(mp(ty, tx));
      }
    }
  }

  /*
  rep(i, 1, h) rep(j, 1, w) {
      if (a[i-1][j-1]=='#') d[i][j] = 0;
  }

  自分の解答
  rep(hi, 1, h) {
      rep(wi, 1, w) d[hi][wi] = min(d[hi][wi], d[hi][wi-1]+1);
      per(wi, 1, w) d[hi][wi] = min(d[hi][wi], d[hi][wi+1]+1);
  }

  rep(wi, 1, w) {
      rep(hi, 1, h) d[hi][wi] = min(d[hi][wi], d[hi-1][wi]+1);
      per(hi, 1, h) d[hi][wi] = min(d[hi][wi], d[hi+1][wi]+1);
  }

  */

  rep(i, 1, h) rep(j, 1, w) ans = max(ans, d[i][j]);

  cout << ans << endl;

  return 0;
}