Problem p03725 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>  // minmax, sort, swap
#include <cstdio>     // printf, scanf
#include <deque>      // deque
#include <functional> // function<void(int)>
#include <iomanip>    // cout<<setprecision(n)
#include <iostream>   // cin, cout, cerr
#include <map>        // key-value pairs sorted by keys
#include <numeric>    // iota
#include <queue>      // queue, priority_queue
#include <set>        // set
#include <string>     // string, stoi, to_string
#include <vector>     // vector

#define rep(i, n) for (int i = 0; i < (n); i++)
#define ENDL '\n'
#define print(i) std::cout << (i) << '\n'

#define int long long // at least int64 > 9*10^18
#define all(v) (v).begin(), (v).end()
/* libraries */

// ceil of a/b
inline int ceil(int a, int b) { return (a % b ? a / b + 1 : a / b); }

signed main() {
  int h, w, k;
  std::cin >> h >> w >> k;
  std::vector<std::string> g(h);
  rep(i, h) std::cin >> g[i];
  std::pair<int, int> spos;
  rep(i, h) rep(j, w) if (g[i][j] == 'S') spos = {i, j};

  int min = h;

  // bfs
  std::vector<std::vector<int>> went(h, std::vector<int>(w, false));
  std::queue<std::pair<std::pair<int, int>, int>> que;
  que.emplace(spos, 0);
  while (!que.empty()) {
    auto c = que.front();
    que.pop();
    int x = c.first.first;
    int y = c.first.second;
    int d = c.second;
    went[x][y] = true;
    if (d <= k)
      min = std::min({min, x, y, h - x - 1, w - y - 1});
    for (int dx = -1; dx <= 1; dx++) {
      for (int dy = -1; dy <= 1; dy++) {
        if (dx * dy == 0 && x + dx >= 0 && x + dx < h && y + dy >= 0 &&
            y + dy < w && went[x + dx][y + dy] == 0 &&
            g[x + dx][y + dy] == '.') {
          que.emplace(std::make_pair(x + dx, y + dy), d + 1);
        }
      }
    }
  }
  print(ceil(min, k) + 1);

  return 0;
}


Fixed submission:
#include <algorithm>  // minmax, sort, swap
#include <cstdio>     // printf, scanf
#include <deque>      // deque
#include <functional> // function<void(int)>
#include <iomanip>    // cout<<setprecision(n)
#include <iostream>   // cin, cout, cerr
#include <map>        // key-value pairs sorted by keys
#include <numeric>    // iota
#include <queue>      // queue, priority_queue
#include <set>        // set
#include <string>     // string, stoi, to_string
#include <vector>     // vector

#define rep(i, n) for (int i = 0; i < (n); i++)
#define ENDL '\n'
#define print(i) std::cout << (i) << '\n'

#define int long long // at least int64 > 9*10^18
#define all(v) (v).begin(), (v).end()
/* libraries */

// ceil of a/b
inline int ceil(int a, int b) { return (a % b ? a / b + 1 : a / b); }

signed main() {
  int h, w, k;
  std::cin >> h >> w >> k;
  std::vector<std::string> g(h);
  rep(i, h) std::cin >> g[i];
  std::pair<int, int> spos;
  rep(i, h) rep(j, w) if (g[i][j] == 'S') spos = {i, j};

  int min = h;

  // bfs
  std::vector<std::vector<int>> went(h, std::vector<int>(w, false));
  std::queue<std::pair<std::pair<int, int>, int>> que;
  que.emplace(spos, 0);
  while (!que.empty()) {
    auto c = que.front();
    que.pop();
    int x = c.first.first;
    int y = c.first.second;
    int d = c.second;
    went[x][y] = true;
    if (d <= k)
      min = std::min({min, x, y, h - x - 1, w - y - 1});
    for (int dx = -1; dx <= 1; dx++) {
      for (int dy = -1; dy <= 1; dy++) {
        if (dx * dy == 0 && x + dx >= 0 && x + dx < h && y + dy >= 0 &&
            y + dy < w && went[x + dx][y + dy] == 0 &&
            g[x + dx][y + dy] == '.') {
          went[x + dx][y + dy] = true;
          que.emplace(std::make_pair(x + dx, y + dy), d + 1);
        }
      }
    }
  }
  print(ceil(min, k) + 1);

  return 0;
}