Problem p03053 (C++)

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

using namespace std;

typedef long long ll;
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
typedef pair<ll, ll> cp2;
typedef pair<ll, cp2> cp3;
#define fi first
#define se second
#define sec se.fi
#define thr se.se
const ll mod = 1000000007;
//              123456789

///////////////////////////////////////////////
//
//
///////////////////////////////////////////////

////////////////////////////////////////////////
////////////////////////////////////////////////

int H, W;
int ans = 0;

int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
string A[1123];
int dp[1123][1123];
vector<cp2> S[1123];
int idx = 0;

int main() {

  fill(dp[0], dp[1123], -1);

  cin >> H >> W;
  REP(i, H) cin >> A[i];

  REP(i, H) {
    REP(j, W) {
      if (A[i][j] == '#') {
        dp[i][j] = idx;
        S[idx].push_back(cp2(i, j));
      }
    }
  }

  while (idx < H + W) {
    if (S[idx].size() == 0)
      break;
    REP(i, S[idx].size()) {
      int y = S[idx][i].fi;
      int x = S[idx][i].se;
      REP(j, 4) {
        int nx = x + dx[j];
        int ny = y + dy[j];
        if (nx < W && nx >= 0 && ny < H && ny >= 0) {
          if (dp[ny][nx] < 0) {
            dp[ny][nx] = idx + 1;
            S[idx + 1].push_back(cp2(ny, nx));
          }
        }
      }
    }
    idx++;
  }

  REP(i, H) REP(j, W) ans = max(ans, dp[i][j]);

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
typedef pair<ll, ll> cp2;
typedef pair<ll, cp2> cp3;
#define fi first
#define se second
#define sec se.fi
#define thr se.se
const ll mod = 1000000007;
//              123456789

///////////////////////////////////////////////
//
//
///////////////////////////////////////////////

////////////////////////////////////////////////
////////////////////////////////////////////////

int H, W;
int ans = 0;

int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
string A[1123];
int dp[1123][1123];
vector<cp2> S[2123];
int idx = 0;

int main() {

  fill(dp[0], dp[1123], -1);

  cin >> H >> W;
  REP(i, H) cin >> A[i];

  REP(i, H) {
    REP(j, W) {
      if (A[i][j] == '#') {
        dp[i][j] = idx;
        S[idx].push_back(cp2(i, j));
      }
    }
  }

  while (idx < H + W) {
    if (S[idx].size() == 0)
      break;
    REP(i, S[idx].size()) {
      int y = S[idx][i].fi;
      int x = S[idx][i].se;
      REP(j, 4) {
        int nx = x + dx[j];
        int ny = y + dy[j];
        if (nx < W && nx >= 0 && ny < H && ny >= 0) {
          if (dp[ny][nx] < 0) {
            dp[ny][nx] = idx + 1;
            S[idx + 1].push_back(cp2(ny, nx));
          }
        }
      }
    }
    idx++;
  }

  REP(i, H) REP(j, W) ans = max(ans, dp[i][j]);

  cout << ans << endl;

  return 0;
}