Problem p03725 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _CRT_SECURE_NO_WARNINGS
#define _SCL_SECURE_NO_WARNINGS
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <valarray>
#include <vector>

using namespace std;
typedef unsigned int uint;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<LL, LL> PP;
#define REP(i, a, n) for (LL i = (a); i < (LL)(n); ++i)
#define REM(i, a, n) for (LL i = ((n)-1); i >= (a); --i)
#define FLOAT fixed << setprecision(16)
#define SPEEDUP                                                                \
  {                                                                            \
    cin.tie(NULL);                                                             \
    ios::sync_with_stdio(false);                                               \
  }
const int INF = 0x3FFFFFFF;
const LL INFLL = 0x3FFFFFFF3FFFFFFF;
const double INFD = 1.0e+308;
const string INFSTR = "\x7f";
const double EPS = 1.0e-9;

void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; }
void YESNO(bool b) { cout << (b ? "YES" : "NO") << endl; }
template <class T, class U>
istream &operator>>(istream &ist, pair<T, U> &right) {
  return ist >> right.first >> right.second;
}
template <class T, class U>
ostream &operator<<(ostream &ost, const pair<T, U> &right) {
  return ost << right.first << ' ' << right.second;
}
template <class T, class TCompatible, size_t N>
void Fill(T (&dest)[N], const TCompatible &val) {
  fill(dest, dest + N, val);
}
template <class T, class TCompatible, size_t M, size_t N>
void Fill(T (&dest)[M][N], const TCompatible &val) {
  for (int i = 0; i < M; ++i)
    Fill(dest[i], val);
}
template <class T> T Compare(T left, T right) {
  return left > right ? 1 : (left < right ? -1 : 0);
}
istream &Ignore(istream &ist) {
  string s;
  ist >> s;
  return ist;
}
bool Inside(int i, int j, int h, int w) {
  return i >= 0 && i < h && j >= 0 && j < w;
}
// all_of
// partial_sum, adjacent_difference

#ifdef ONLY_MY_ENVIR
#include "Flow_Solver.h"
#include "Geometry.h"
#include "Global.h"
#include "Graph.h"
#include "IntMod.h"
#include "Matrix.h"
#include "Range.h"
#include "Suffix_Array.h"
#include "Tree.h"
#include "Union_Find.h"
#endif

#ifdef __GNUC__
typedef __int128 LLL;
istream &operator>>(istream &ist, __int128 &val) {
  LL tmp;
  ist >> tmp;
  val = tmp;
  return ist;
}
ostream &operator<<(ostream &ost, __int128 val) {
  LL tmp = val;
  ost << tmp;
  return ost;
}
#endif

#if 1234567891
#include <array>
#include <random>
#include <unordered_map>
#include <unordered_set>
template <typename T>
using PriorityQ = priority_queue<T, vector<T>, greater<T>>; // コスト小を優先
#endif

int H, W, K;
string S[800];
int si, sj;

int Dist(int i, int j) { return min({i, j, H - i - 1, W - j - 1}); }

int di[] = {0, 1, 0, -1};
int dj[] = {1, 0, -1, 0};

int dist[800][800];
int BFS(int si, int sj) {
  int ret = Dist(si, sj);

  queue<PP> Q;
  Q.push(PP(si, sj));
  Fill(dist, INF);
  dist[si][sj] = 0;
  while (!Q.empty()) {
    PP p = Q.front();
    Q.pop();
    int i = p.first;
    int j = p.second;
    if (S[i][j] == '#')
      continue;
    if (dist[i][j] > K)
      continue;
    ret = min(ret, Dist(i, j));

    REP(k, 0, 4) {
      int p = i + di[k];
      int q = j + dj[k];
      if (!Inside(p, q, H, W))
        continue;
      if (dist[i][j] + 1 > dist[p][q])
        continue;
      dist[p][q] = dist[i][j] + 1;
      Q.push(PP(p, q));
    }
  }
  return ret;
}

int main() {
  cin >> H >> W >> K;
  REP(i, 0, H) {
    cin >> S[i];
    REP(j, 0, W) {
      if (S[i][j] == 'S') {
        si = i;
        sj = j;
      }
    }
  }

  int dist = BFS(si, sj);
  cout << 1 + (dist + K - 1) / K << endl;
  return 0;
}


Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#define _SCL_SECURE_NO_WARNINGS
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <valarray>
#include <vector>

using namespace std;
typedef unsigned int uint;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<LL, LL> PP;
#define REP(i, a, n) for (LL i = (a); i < (LL)(n); ++i)
#define REM(i, a, n) for (LL i = ((n)-1); i >= (a); --i)
#define FLOAT fixed << setprecision(16)
#define SPEEDUP                                                                \
  {                                                                            \
    cin.tie(NULL);                                                             \
    ios::sync_with_stdio(false);                                               \
  }
const int INF = 0x3FFFFFFF;
const LL INFLL = 0x3FFFFFFF3FFFFFFF;
const double INFD = 1.0e+308;
const string INFSTR = "\x7f";
const double EPS = 1.0e-9;

void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; }
void YESNO(bool b) { cout << (b ? "YES" : "NO") << endl; }
template <class T, class U>
istream &operator>>(istream &ist, pair<T, U> &right) {
  return ist >> right.first >> right.second;
}
template <class T, class U>
ostream &operator<<(ostream &ost, const pair<T, U> &right) {
  return ost << right.first << ' ' << right.second;
}
template <class T, class TCompatible, size_t N>
void Fill(T (&dest)[N], const TCompatible &val) {
  fill(dest, dest + N, val);
}
template <class T, class TCompatible, size_t M, size_t N>
void Fill(T (&dest)[M][N], const TCompatible &val) {
  for (int i = 0; i < M; ++i)
    Fill(dest[i], val);
}
template <class T> T Compare(T left, T right) {
  return left > right ? 1 : (left < right ? -1 : 0);
}
istream &Ignore(istream &ist) {
  string s;
  ist >> s;
  return ist;
}
bool Inside(int i, int j, int h, int w) {
  return i >= 0 && i < h && j >= 0 && j < w;
}
// all_of
// partial_sum, adjacent_difference

#ifdef ONLY_MY_ENVIR
#include "Flow_Solver.h"
#include "Geometry.h"
#include "Global.h"
#include "Graph.h"
#include "IntMod.h"
#include "Matrix.h"
#include "Range.h"
#include "Suffix_Array.h"
#include "Tree.h"
#include "Union_Find.h"
#endif

#ifdef __GNUC__
typedef __int128 LLL;
istream &operator>>(istream &ist, __int128 &val) {
  LL tmp;
  ist >> tmp;
  val = tmp;
  return ist;
}
ostream &operator<<(ostream &ost, __int128 val) {
  LL tmp = val;
  ost << tmp;
  return ost;
}
#endif

#if 1234567891
#include <array>
#include <random>
#include <unordered_map>
#include <unordered_set>
template <typename T>
using PriorityQ = priority_queue<T, vector<T>, greater<T>>; // コスト小を優先
#endif

int H, W, K;
string S[800];
int si, sj;

int Dist(int i, int j) { return min({i, j, H - i - 1, W - j - 1}); }

int di[] = {0, 1, 0, -1};
int dj[] = {1, 0, -1, 0};

int dist[800][800];
int BFS(int si, int sj) {
  int ret = Dist(si, sj);

  queue<PP> Q;
  Q.push(PP(si, sj));
  Fill(dist, INF);
  dist[si][sj] = 0;
  while (!Q.empty()) {
    PP p = Q.front();
    Q.pop();
    int i = p.first;
    int j = p.second;
    if (S[i][j] == '#')
      continue;
    if (dist[i][j] > K)
      continue;
    ret = min(ret, Dist(i, j));

    REP(k, 0, 4) {
      int p = i + di[k];
      int q = j + dj[k];
      if (!Inside(p, q, H, W))
        continue;
      if (dist[p][q] != INF)
        continue;
      dist[p][q] = dist[i][j] + 1;
      Q.push(PP(p, q));
    }
  }
  return ret;
}

int main() {
  cin >> H >> W >> K;
  REP(i, 0, H) {
    cin >> S[i];
    REP(j, 0, W) {
      if (S[i][j] == 'S') {
        si = i;
        sj = j;
      }
    }
  }

  int dist = BFS(si, sj);
  cout << 1 + (dist + K - 1) / K << endl;
  return 0;
}