Problem p03725 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
typedef pair<int, P> P1;

#define fr first
#define sc second
#define mp make_pair
#define pb push_back
#define rep(i, x) for (int i = 0; i < x; i++)
#define rep1(i, x) for (int i = 1; i <= x; i++)
#define rrep(i, x) for (int i = x - 1; i >= 0; i--)
#define rrep1(i, x) for (int i = x; i > 0; i--)
#define sor(v) sort(v.begin(), v.end())
#define rev(s) reverse(s.begin(), s.end())
#define lb(vec, a) lower_bound(vec.begin(), vec.end(), a)
#define ub(vec, a) upper_bound(vec.begin(), vec.end(), a)
#define uniq(vec) vec.erase(unique(vec.begin(), vec.end()), vec.end())
#define mp1(a, b, c) P1(a, P(b, c))

const int INF = 1000000000;
const int dir_4[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
const int dir_8[8][2] = {{1, 0},  {1, 1},   {0, 1},  {-1, 1},
                         {-1, 0}, {-1, -1}, {0, -1}, {1, -1}};

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

int main() {
  static int h, w, k;
  static char a[802][802];
  scanf("%d%d%d", &h, &w, &k);
  rep1(i, h) {
    scanf("\n");
    rep1(j, w) scanf("%c", &a[i][j]);
  }

  static int d[802][802];
  static bool used[802][802];
  static queue<P> que;
  rep(i, 802) rep(j, 802) {
    d[i][j] = INF;
    used[i][j] = false;
  }
  rep1(i, h) rep1(j, w) {
    if (a[i][j] == 'S') {
      d[i][j] = 0;
      que.push(P(i, j));
    }
  }
  while (!que.empty()) {
    P p = que.front();
    que.pop();
    if (used[p.fr][p.sc])
      continue;
    used[p.fr][p.sc] = true;
    rep(i, 4) {
      if (a[p.fr + dx[i]][p.sc + dy[i]] == '#')
        continue;
      d[p.fr + dx[i]][p.sc + dy[i]] = d[p.fr][p.sc] + 1;
      if (d[p.fr + dx[i]][p.sc + dy[i]] < k)
        que.push(P(p.fr + dx[i], p.sc + dy[i]));
    }
  }

  int ret = INF;
  rep1(i, h) rep1(j, w) {
    if (d[i][j] == INF)
      continue;
    int D = min(i - 1, h - i);
    D = min(D, min(j - 1, w - j));
    ret = min(ret, (D + k - 1) / k + 1);
  }
  cout << ret << endl;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
typedef pair<int, P> P1;

#define fr first
#define sc second
#define mp make_pair
#define pb push_back
#define rep(i, x) for (int i = 0; i < x; i++)
#define rep1(i, x) for (int i = 1; i <= x; i++)
#define rrep(i, x) for (int i = x - 1; i >= 0; i--)
#define rrep1(i, x) for (int i = x; i > 0; i--)
#define sor(v) sort(v.begin(), v.end())
#define rev(s) reverse(s.begin(), s.end())
#define lb(vec, a) lower_bound(vec.begin(), vec.end(), a)
#define ub(vec, a) upper_bound(vec.begin(), vec.end(), a)
#define uniq(vec) vec.erase(unique(vec.begin(), vec.end()), vec.end())
#define mp1(a, b, c) P1(a, P(b, c))

const int INF = 1000000000;
const int dir_4[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
const int dir_8[8][2] = {{1, 0},  {1, 1},   {0, 1},  {-1, 1},
                         {-1, 0}, {-1, -1}, {0, -1}, {1, -1}};

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

int main() {
  static int h, w, k;
  static char a[802][802];
  rep(i, 802) rep(j, 802) a[i][j] = '#';
  scanf("%d%d%d", &h, &w, &k);
  rep1(i, h) {
    scanf("\n");
    rep1(j, w) scanf("%c", &a[i][j]);
  }

  static int d[802][802];
  static bool used[802][802];
  static queue<P> que;
  rep(i, 802) rep(j, 802) {
    d[i][j] = INF;
    used[i][j] = false;
  }
  rep1(i, h) rep1(j, w) {
    if (a[i][j] == 'S') {
      d[i][j] = 0;
      que.push(P(i, j));
    }
  }
  while (!que.empty()) {
    P p = que.front();
    que.pop();
    if (used[p.fr][p.sc])
      continue;
    used[p.fr][p.sc] = true;
    rep(i, 4) {
      if (a[p.fr + dx[i]][p.sc + dy[i]] == '#')
        continue;
      d[p.fr + dx[i]][p.sc + dy[i]] = d[p.fr][p.sc] + 1;
      if (d[p.fr + dx[i]][p.sc + dy[i]] < k)
        que.push(P(p.fr + dx[i], p.sc + dy[i]));
    }
  }

  int ret = INF;
  rep1(i, h) rep1(j, w) {
    if (d[i][j] == INF)
      continue;
    int D = min(i - 1, h - i);
    D = min(D, min(j - 1, w - j));
    ret = min(ret, (D + k - 1) / k + 1);
  }
  cout << ret << endl;
}