Problem p03725 (C++)

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

int main() {
  int64_t H, W, K;
  cin >> H >> W >> K;
  vector<string> A(H);
  for (int64_t i = 0; i < H; i++) {
    cin >> A[i];
  }

  int64_t Sx, Sy;
  for (int64_t i = 0; i < H; i++) {
    for (int64_t j = 0; j < W; j++) {
      if (A[i][j] == 'S') {
        Sx = j;
        Sy = i;
        i = H;
        j = W;
      }
    }
  }

  // Sから深さKのDFSでどこに到達できるか調べる
  int64_t dx[] = {1, 0, -1, 0};
  int64_t dy[] = {0, 1, 0, -1};

  struct Element {
    int64_t x, y, cost;
    bool operator<(const Element &rhs) const { return cost < rhs.cost; }
    bool operator>(const Element &rhs) const { return cost > rhs.cost; }
  };
  priority_queue<Element, vector<Element>, greater<Element>> pq;

  vector<vector<int64_t>> cost(H, vector<int64_t>(W, LLONG_MAX));
  queue<pair<int64_t, int64_t>> st;
  cost[Sy][Sx] = 0;
  pq.push({Sx, Sy, 0});

  while (!pq.empty()) {
    Element t = pq.top();
    pq.pop();
    if (t.cost > cost[t.y][t.x] || t.cost >= K) {
      continue;
    }

    for (int64_t i = 0; i < 4; i++) {
      int64_t nx = t.x + dx[i];
      int64_t ny = t.y + dy[i];
      if (nx < 0 || nx >= W || ny < 0 || ny >= H || A[ny][nx] == '#') {
        continue;
      }

      int64_t new_cost = cost[t.y][t.x] + 1;
      if (new_cost < cost[ny][nx]) {
        cost[ny][nx] = new_cost;
        pq.push({ny, nx, new_cost});
      }
    }
  }

  int64_t ans = LLONG_MAX;
  for (int64_t i = 0; i < H; i++) {
    for (int64_t j = 0; j < W; j++) {
      if (cost[i][j] == LLONG_MAX) {
        continue;
      }

      int64_t min_distance = min({i, j, H - 1 - i, W - 1 - j});

      ans = min(ans, 1 + (min_distance + K - 1) / K);
    }
  }

  cout << ans << endl;
}

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

int main() {
  int64_t H, W, K;
  cin >> H >> W >> K;
  vector<string> A(H);
  for (int64_t i = 0; i < H; i++) {
    cin >> A[i];
  }

  int64_t Sx, Sy;
  for (int64_t i = 0; i < H; i++) {
    for (int64_t j = 0; j < W; j++) {
      if (A[i][j] == 'S') {
        Sx = j;
        Sy = i;
        i = H;
        j = W;
      }
    }
  }

  // Sから深さKのDFSでどこに到達できるか調べる
  int64_t dx[] = {1, 0, -1, 0};
  int64_t dy[] = {0, 1, 0, -1};

  struct Element {
    int64_t x, y, cost;
    bool operator<(const Element &rhs) const { return cost < rhs.cost; }
    bool operator>(const Element &rhs) const { return cost > rhs.cost; }
  };
  priority_queue<Element, vector<Element>, greater<Element>> pq;

  vector<vector<int64_t>> cost(H, vector<int64_t>(W, LLONG_MAX));
  queue<pair<int64_t, int64_t>> st;
  cost[Sy][Sx] = 0;
  pq.push({Sx, Sy, 0});

  while (!pq.empty()) {
    Element t = pq.top();
    pq.pop();
    if (t.cost > cost[t.y][t.x] || t.cost >= K) {
      continue;
    }

    for (int64_t i = 0; i < 4; i++) {
      int64_t nx = t.x + dx[i];
      int64_t ny = t.y + dy[i];
      if (nx < 0 || nx >= W || ny < 0 || ny >= H || A[ny][nx] == '#') {
        continue;
      }

      int64_t new_cost = cost[t.y][t.x] + 1;
      if (new_cost < cost[ny][nx]) {
        cost[ny][nx] = new_cost;
        pq.push({nx, ny, new_cost});
      }
    }
  }

  int64_t ans = LLONG_MAX;
  for (int64_t i = 0; i < H; i++) {
    for (int64_t j = 0; j < W; j++) {
      if (cost[i][j] == LLONG_MAX) {
        continue;
      }

      int64_t min_distance = min({i, j, H - 1 - i, W - 1 - j});

      ans = min(ans, 1 + (min_distance + K - 1) / K);
    }
  }

  cout << ans << endl;
}