Problem p03725 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <array>
#include <climits>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <valarray>
#include <vector>

using namespace std;

using ll = long long int;

#define loop(i, N) for (int i = 0; i < N; ++i)
#define scanInt(x)                                                             \
  int x;                                                                       \
  scanf("%d", &x);                                                             \
  getchar();
#define scanLLInt(x)                                                           \
  ll x;                                                                        \
  scanf("%lld", &x);                                                           \
  getchar();
#define scanVecInt(A, N)                                                       \
  vector<int> A(N);                                                            \
  loop(i, N) {                                                                 \
    scanf("%d", &(A[i]));                                                      \
    getchar();                                                                 \
  }
#define scanVecLLInt(A, N)                                                     \
  vector<ll> A(N);                                                             \
  loop(i, N) {                                                                 \
    scanf("%lld", &(A[i]));                                                    \
    getchar();                                                                 \
  }

int main() {
  scanInt(H);
  scanInt(W);
  scanInt(K);
  vector<vector<char>> board(H, vector<char>(W));
  using coordinate = pair<int, int>;
  coordinate start;
  loop(y, H) {
    loop(x, W) {
      board[y][x] = getchar();
      if (board[y][x] == 'S') {
        start = make_pair(x, y);
      }
    }
    getchar();
  }
  queue<tuple<coordinate, int>> q;
  q.emplace(start, 0);
  auto add_to_queue = [&](int x, int y, int times) {
    if (x < 0 || x >= W || y < 0 || y >= H || board[y][x] == '#')
      return;
    // printf("%d %d\n", x, y);
    q.emplace(make_pair(x, y), times);
  };
  int distance = INT_MAX;
  while (!q.empty()) {
    int x = get<0>(q.front()).first;
    int y = get<0>(q.front()).second;
    int times = get<1>(q.front());
    q.pop();
    board[y][x] = '#';
    distance = min(distance, min(min(x, W - 1 - x), min(y, H - 1 - y)));
    // printf("%d %d %d %d\n", x, y, times, distance);
    if (times < K) {
      add_to_queue(x - 1, y, times + 1);
      add_to_queue(x + 1, y, times + 1);
      add_to_queue(x, y - 1, times + 1);
      add_to_queue(x, y + 1, times + 1);
    }
  }
  // printf("%d\n", distance);
  if (distance == 0) {
    printf("%d\n", 1);
  } else {
    printf("%d\n", 2 + (distance - 1) / K);
  }
}


Fixed submission:
#include <algorithm>
#include <array>
#include <climits>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <valarray>
#include <vector>

using namespace std;

using ll = long long int;

#define loop(i, N) for (int i = 0; i < N; ++i)
#define scanInt(x)                                                             \
  int x;                                                                       \
  scanf("%d", &x);                                                             \
  getchar();
#define scanLLInt(x)                                                           \
  ll x;                                                                        \
  scanf("%lld", &x);                                                           \
  getchar();
#define scanVecInt(A, N)                                                       \
  vector<int> A(N);                                                            \
  loop(i, N) {                                                                 \
    scanf("%d", &(A[i]));                                                      \
    getchar();                                                                 \
  }
#define scanVecLLInt(A, N)                                                     \
  vector<ll> A(N);                                                             \
  loop(i, N) {                                                                 \
    scanf("%lld", &(A[i]));                                                    \
    getchar();                                                                 \
  }

int main() {
  scanInt(H);
  scanInt(W);
  scanInt(K);
  vector<vector<char>> board(H, vector<char>(W));
  using coordinate = pair<int, int>;
  coordinate start;
  loop(y, H) {
    loop(x, W) {
      board[y][x] = getchar();
      if (board[y][x] == 'S') {
        start = make_pair(x, y);
      }
    }
    getchar();
  }
  queue<tuple<coordinate, int>> q;
  q.emplace(start, 0);
  auto add_to_queue = [&](int x, int y, int times) {
    if (x < 0 || x >= W || y < 0 || y >= H || board[y][x] == '#')
      return;
    // printf("%d %d\n", x, y);
    q.emplace(make_pair(x, y), times);
    board[y][x] = '#';
  };
  int distance = INT_MAX;
  while (!q.empty()) {
    int x = get<0>(q.front()).first;
    int y = get<0>(q.front()).second;
    int times = get<1>(q.front());
    q.pop();
    board[y][x] = '#';
    distance = min(distance, min(min(x, W - 1 - x), min(y, H - 1 - y)));
    // printf("%d %d %d %d\n", x, y, times, distance);
    if (times < K) {
      add_to_queue(x - 1, y, times + 1);
      add_to_queue(x + 1, y, times + 1);
      add_to_queue(x, y - 1, times + 1);
      add_to_queue(x, y + 1, times + 1);
    }
  }
  // printf("%d\n", distance);
  if (distance == 0) {
    printf("%d\n", 1);
  } else {
    printf("%d\n", 2 + (distance - 1) / K);
  }
}