Problem p03053 (C++)

Buggy submission (status: Runtime Error):
// include
//------------------------------------------
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define SORT(c) sort((c).begin(), (c).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
#define ANS(ans) cout << (ans) << endl;
#define UNIQUE(v) (v).erase(unique((v).begin(), (v).end()), (v).end());
typedef vector<int> VI;
typedef pair<int, int> P;

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define REP(i, n) for (int i = 0; i < (n); ++i)

int main() {
  int H, W;
  cin >> H >> W;

  const int MAX_N = 10;

  bool a[MAX_N][MAX_N];
  int memo[MAX_N][MAX_N];

  int dx[4] = {1, 0, -1, 0};
  int dy[4] = {0, 1, 0, -1};
  string str;
  REP(i, H) {
    cin >> str;
    REP(j, W) {
      if (str[j] == '.')
        a[i][j] = 0;
      else
        a[i][j] = 1;
    }
  }

  queue<P> q;

  REP(i, H) {
    REP(j, W) {
      if (a[i][j] == 1) {
        q.push(make_pair(i, j));
        memo[i][j] = 0;
      }
    }
  }
  int ans = 0;
  while (q.size() > 0) {
    int y = q.front().first;
    int x = q.front().second;

    REP(i, 4) {
      if (x + dx[i] >= 0 && x + dx[i] < W && y + dy[i] >= 0 && y + dy[i] < H) {
        if (a[y + dy[i]][x + dx[i]] == 0) {
          a[y + dy[i]][x + dx[i]] = 1;
          q.push(make_pair(y + dy[i], x + dx[i]));
          memo[y + dy[i]][x + dx[i]] = memo[y][x] + 1;
        }
      }
    }
    q.pop();
  }
  REP(i, H) {
    REP(j, W) { ans = max(ans, memo[i][j]); }
  }
  ANS(ans);

  return 0;
}


Fixed submission:
// include
//------------------------------------------
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define SORT(c) sort((c).begin(), (c).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
#define ANS(ans) cout << (ans) << endl;
#define UNIQUE(v) (v).erase(unique((v).begin(), (v).end()), (v).end());
typedef vector<int> VI;
typedef pair<int, int> P;

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define REP(i, n) for (int i = 0; i < (n); ++i)

int main() {
  int H, W;
  cin >> H >> W;

  const int MAX_N = 1010;

  bool a[MAX_N][MAX_N];
  int memo[MAX_N][MAX_N];

  int dx[4] = {1, 0, -1, 0};
  int dy[4] = {0, 1, 0, -1};
  string str;
  REP(i, H) {
    cin >> str;
    REP(j, W) {
      if (str[j] == '.')
        a[i][j] = 0;
      else
        a[i][j] = 1;
    }
  }

  queue<P> q;

  REP(i, H) {
    REP(j, W) {
      if (a[i][j] == 1) {
        q.push(make_pair(i, j));
        memo[i][j] = 0;
      }
    }
  }
  int ans = 0;
  while (q.size() > 0) {
    int y = q.front().first;
    int x = q.front().second;

    REP(i, 4) {
      if (x + dx[i] >= 0 && x + dx[i] < W && y + dy[i] >= 0 && y + dy[i] < H) {
        if (a[y + dy[i]][x + dx[i]] == 0) {
          a[y + dy[i]][x + dx[i]] = 1;
          q.push(make_pair(y + dy[i], x + dx[i]));
          memo[y + dy[i]][x + dx[i]] = memo[y][x] + 1;
        }
      }
    }
    q.pop();
  }
  REP(i, H) {
    REP(j, W) { ans = max(ans, memo[i][j]); }
  }
  ANS(ans);

  return 0;
}