Problem p03725 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

typedef int_fast32_t int32;
typedef int_fast64_t int64;

const int32 inf = 1e9 + 7;
const int32 MOD = 1000000007;
const int64 llinf = 1e18;

#define YES(n) cout << ((n) ? "YES\n" : "NO\n")
#define Yes(n) cout << ((n) ? "Yes\n" : "No\n")
#define POSSIBLE(n) cout << ((n) ? "POSSIBLE\n" : "IMPOSSIBLE\n")
#define ANS(n) cout << (n) << "\n"
#define REP(i, n) for (int64 i = 0; i < (n); ++i)
#define FOR(i, a, b) for (int64 i = (a); i < (b); i++)
#define FORR(i, a, b) for (int64 i = (a); i >= (b); i--)
#define all(obj) (obj).begin(), (obj).end()
#define rall(obj) (obj).rbegin(), (obj).rend()
#define fi first
#define se second
#define pb(a) push_back(a)
typedef pair<int32, int32> pii;
typedef pair<int64, int64> pll;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

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

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int32 h, w, k;
  cin >> h >> w >> k;
  vector<string> a(h);
  REP(i, h) cin >> a[i];
  int32 sy = -1, sx = -1;
  REP(i, h) REP(j, w) if (a[i][j] == 'S') tie(sy, sx) = tie(i, j);

  vector<vector<int32>> dist(h, vector<int32>(w, inf));
  dist[sy][sx] = 0;
  priority_queue<pair<int32, pii>> que;
  que.emplace(0, pii(sy, sx));
  while (!que.empty()) {
    int32 d, y, x;
    d = que.top().fi;
    tie(y, x) = que.top().se;
    que.pop();
    if (dist[y][x] < d)
      continue;
    REP(i, 4) {
      int32 ny = y + dy[i];
      int32 nx = x + dx[i];
      if (ny < 0 || h <= ny)
        continue;
      if (nx < 0 || w <= nx)
        continue;
      if (d >= k)
        continue;
      if (a[ny][nx] == '#')
        continue;
      if (chmin(dist[ny][nx], dist[y][x] + 1))
        que.emplace(dist[ny][nx], pii(ny, nx));
    }
  }
  REP(i, h) REP(j, w) {
    if (dist[i][j] <= k) {
      dist[i][j] = 0;
      que.emplace(0, pii(i, j));
    }
  }
  // REP(i,h){
  //   REP(j,w){
  //     cout << dist[i][j] << " ";
  //   }
  //   cout << endl;
  // }
  while (!que.empty()) {
    int32 d, y, x;
    d = que.top().fi;
    tie(y, x) = que.top().se;
    que.pop();
    if (dist[y][x] < d)
      continue;
    REP(i, 4) {
      int32 ny = y + dy[i];
      int32 nx = x + dx[i];
      if (ny < 0 || h <= ny)
        continue;
      if (nx < 0 || w <= nx)
        continue;
      if (chmin(dist[ny][nx], dist[y][x] + 1))
        que.emplace(dist[ny][nx], pii(ny, nx));
    }
  }
  // REP(i,h){
  //   REP(j,w){
  //     cout << dist[i][j] << " ";
  //   }
  //   cout << endl;
  // }

  int32 ans = inf;
  REP(i, h) {
    chmin(ans, (dist[i][0] + k - 1) / k + 1);
    chmin(ans, (dist[i][w - 1] + k - 1) / k + 1);
  }
  REP(j, w) {
    chmin(ans, (dist[0][j] + k - 1) / k + 1);
    chmin(ans, (dist[h - 1][j] + k - 1) / k + 1);
  }
  ANS(ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

typedef int_fast32_t int32;
typedef int_fast64_t int64;

const int32 inf = 1e9 + 7;
const int32 MOD = 1000000007;
const int64 llinf = 1e18;

#define YES(n) cout << ((n) ? "YES\n" : "NO\n")
#define Yes(n) cout << ((n) ? "Yes\n" : "No\n")
#define POSSIBLE(n) cout << ((n) ? "POSSIBLE\n" : "IMPOSSIBLE\n")
#define ANS(n) cout << (n) << "\n"
#define REP(i, n) for (int64 i = 0; i < (n); ++i)
#define FOR(i, a, b) for (int64 i = (a); i < (b); i++)
#define FORR(i, a, b) for (int64 i = (a); i >= (b); i--)
#define all(obj) (obj).begin(), (obj).end()
#define rall(obj) (obj).rbegin(), (obj).rend()
#define fi first
#define se second
#define pb(a) push_back(a)
typedef pair<int32, int32> pii;
typedef pair<int64, int64> pll;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

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

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int32 h, w, k;
  cin >> h >> w >> k;
  vector<string> a(h);
  REP(i, h) cin >> a[i];
  int32 sy = -1, sx = -1;
  REP(i, h) REP(j, w) if (a[i][j] == 'S') tie(sy, sx) = tie(i, j);

  vector<vector<int32>> dist(h, vector<int32>(w, inf));
  dist[sy][sx] = 0;
  priority_queue<pair<int32, pii>, vector<pair<int32, pii>>,
                 greater<pair<int32, pii>>>
      que;
  que.emplace(0, pii(sy, sx));
  while (!que.empty()) {
    int32 d, y, x;
    d = que.top().fi;
    tie(y, x) = que.top().se;
    que.pop();
    if (dist[y][x] < d)
      continue;
    REP(i, 4) {
      int32 ny = y + dy[i];
      int32 nx = x + dx[i];
      if (ny < 0 || h <= ny)
        continue;
      if (nx < 0 || w <= nx)
        continue;
      if (d >= k)
        continue;
      if (a[ny][nx] == '#')
        continue;
      if (chmin(dist[ny][nx], dist[y][x] + 1))
        que.emplace(dist[ny][nx], pii(ny, nx));
    }
  }
  REP(i, h) REP(j, w) {
    if (dist[i][j] <= k) {
      dist[i][j] = 0;
      que.emplace(0, pii(i, j));
    }
  }
  // REP(i,h){
  //   REP(j,w){
  //     cout << dist[i][j] << " ";
  //   }
  //   cout << endl;
  // }
  while (!que.empty()) {
    int32 d, y, x;
    d = que.top().fi;
    tie(y, x) = que.top().se;
    que.pop();
    if (dist[y][x] < d)
      continue;
    REP(i, 4) {
      int32 ny = y + dy[i];
      int32 nx = x + dx[i];
      if (ny < 0 || h <= ny)
        continue;
      if (nx < 0 || w <= nx)
        continue;
      if (chmin(dist[ny][nx], dist[y][x] + 1))
        que.emplace(dist[ny][nx], pii(ny, nx));
    }
  }
  // REP(i,h){
  //   REP(j,w){
  //     cout << dist[i][j] << " ";
  //   }
  //   cout << endl;
  // }

  int32 ans = inf;
  REP(i, h) {
    chmin(ans, (dist[i][0] + k - 1) / k + 1);
    chmin(ans, (dist[i][w - 1] + k - 1) / k + 1);
  }
  REP(j, w) {
    chmin(ans, (dist[0][j] + k - 1) / k + 1);
    chmin(ans, (dist[h - 1][j] + k - 1) / k + 1);
  }
  ANS(ans);
  return 0;
}