Problem p03725 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define pb push_back
#define mp make_pair
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repr(i, b, e) for (int i = (b); i <= (e); i++)
#define INF (1001001001)
#define EPS (1e-15)

#define pr(x)                                                                  \
  do {                                                                         \
    cout << (#x) << " = " << (x) << endl;                                      \
  } while (0)
#define pri(x, i)                                                              \
  do {                                                                         \
    cout << (#x) << "[" << i << "] = " << (x[i]) << endl;                      \
  } while (0)
#define pra(x, n) rep(__i, n) pri(x, __i);
#define prar(x, b, e) repr(__i, b, e) pri(x, __i);

typedef long long llint;
typedef pair<int, int> pint;
typedef vector<int> vint;

int in() {
  int a;
  scanf("%d ", &a);
  return a;
}

vint v;
int H, W;
int K;

int magic(int i, int k) {
  int ydist = min(i - 1, H - i);
  int xdist = min(k - 1, W - k);
  int dist = min(ydist, xdist);
  return (dist + K - 1) / K;
}

int main() {
  H = in();
  W = in();
  K = in();
  vector<vint> v(810, vint(810, 0));
  for (int i = 0; i < H + 2; i++)
    v[i][0] = v[i][W + 1] = -1;
  for (int i = 0; i < W + 2; i++)
    v[0][i] = v[H + 1][i] = -1;
  int y = -1, x = -1;
  for (int i = 1; i <= H; i++) {
    // char temp[810];
    // gets(temp);
    string temp;
    cin >> temp;
    for (int k = 1; k <= W; k++) {
      char c = temp[k - 1];
      if (c == 'S') {
        y = i;
        x = k;
      } else if (c == '#') {
        // solid
        v[i][k] = 99;
      }
    }
  }
  assert(y >= 0 && x >= 0);
  int ans = magic(y, x);
  stack<pint> st;
  st.push(mp(y, x));
  for (int i = 0; i < K + 1; i++) {
    stack<pint> st2;
    while (!st.empty()) {
      pint p = st.top();
      st.pop();
      v[p.first][p.second] = 1;
      ans = min(ans, magic(p.first, p.second));
      for (int dy = -1; dy <= 1; dy++) {
        for (int dx = -1; dx <= 1; dx++) {
          if (dy * dx == 0 && v[p.first + dy][p.second + dx] == 0) {
            st2.push(mp(p.first + dy, p.second + dx));
          }
        }
      }
    }
    st = st2;
  }
  cout << ans + 1 << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define pb push_back
#define mp make_pair
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repr(i, b, e) for (int i = (b); i <= (e); i++)
#define INF (1001001001)
#define EPS (1e-15)

#define pr(x)                                                                  \
  do {                                                                         \
    cout << (#x) << " = " << (x) << endl;                                      \
  } while (0)
#define pri(x, i)                                                              \
  do {                                                                         \
    cout << (#x) << "[" << i << "] = " << (x[i]) << endl;                      \
  } while (0)
#define pra(x, n) rep(__i, n) pri(x, __i);
#define prar(x, b, e) repr(__i, b, e) pri(x, __i);

typedef long long llint;
typedef pair<int, int> pint;
typedef vector<int> vint;

int in() {
  int a;
  scanf("%d ", &a);
  return a;
}

vint v;
int H, W;
int K;

int magic(int i, int k) {
  int ydist = min(i - 1, H - i);
  int xdist = min(k - 1, W - k);
  int dist = min(ydist, xdist);
  return (dist + K - 1) / K;
}

int main() {
  H = in();
  W = in();
  K = in();
  vector<vint> v(810, vint(810, 0));
  for (int i = 0; i < H + 2; i++)
    v[i][0] = v[i][W + 1] = -1;
  for (int i = 0; i < W + 2; i++)
    v[0][i] = v[H + 1][i] = -1;
  int y = -1, x = -1;
  for (int i = 1; i <= H; i++) {
    // char temp[810];
    // gets(temp);
    string temp;
    cin >> temp;
    for (int k = 1; k <= W; k++) {
      char c = temp[k - 1];
      if (c == 'S') {
        y = i;
        x = k;
      } else if (c == '#') {
        // solid
        v[i][k] = 99;
      }
    }
  }
  assert(y >= 0 && x >= 0);
  int ans = magic(y, x);
  stack<pint> st;
  st.push(mp(y, x));
  for (int i = 0; i < K + 1; i++) {
    stack<pint> st2;
    while (!st.empty()) {
      pint p = st.top();
      st.pop();
      v[p.first][p.second] = 1;
      ans = min(ans, magic(p.first, p.second));
      for (int dy = -1; dy <= 1; dy++) {
        for (int dx = -1; dx <= 1; dx++) {
          if (dy * dx == 0 && v[p.first + dy][p.second + dx] == 0) {
            v[p.first + dy][p.second + dx] = 1;
            st2.push(mp(p.first + dy, p.second + dx));
          }
        }
      }
    }
    st = st2;
  }
  cout << ans + 1 << endl;
  return 0;
}