Problem p03725 (C++)

Buggy submission (status: Time Limit Exceeded):
// webcode about science as the new webcode about pirates/monkeys/ninjas? here's
// hoping!
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, a, n) for (int i = a; i < (int)(n); i++)
#define per(i, a, n) for (int i = (n)-1; i >= (int)(a); i--)
template <typename T> ostream &operator<<(ostream &s, vector<T> t) {
  rep(i, 0, t.size()) s << (i ? " " : "") << t[i];
  return s;
}
template <typename T> istream &operator>>(istream &s, vector<T> &t) {
  rep(i, 0, t.size()) s >> t[i];
  return s;
}
template <typename T, typename U>
ostream &operator<<(ostream &s, pair<T, U> t) {
  s << "(" << t.first << "," << t.second << ")";
  return s;
}
template <typename T, typename U>
istream &operator>>(istream &s, pair<T, U> &t) {
  s >> t.first >> t.second;
  return s;
}
typedef long long ll;

const int N = 888;
bool seen[N][N];
int rem[N][N];
int di[4] = {-1, 0, 1, 0};
int dj[4] = {0, 1, 0, -1};

vector<string> s;
int h, w;

bool canGo(int i, int j) {
  if (i < 0 || j < 0 || i >= h || j >= w)
    return false;
  return s[i][j] != '#';
}

int main() {
  ios_base::sync_with_stdio(false);
  int k;
  cin >> h >> w >> k;
  s.resize(h);
  cin >> s;
  int i0 = 0, j0 = 0;
  rep(i, 0, h) rep(j, 0, w) {
    if (s[i][j] == 'S') {
      i0 = i;
      j0 = j;
    }
  }
  queue<pair<int, int>> q;
  q.push({i0, j0});
  rem[i0][j0] = k;
  while (!q.empty()) {
    int i, j;
    tie(i, j) = q.front();
    q.pop();
    seen[i][j] = true;
    if (rem[i][j] == 0)
      continue;

    rep(dir, 0, 4) {
      int ni = i + di[dir];
      int nj = j + dj[dir];
      if (!canGo(ni, nj))
        continue;
      seen[ni][nj] = true;
      rem[ni][nj] = rem[i][j] - 1;
      q.push({ni, nj});
    }
  }

  // rep(i, 0, h) {rep(j, 0, w) cout << seen[i][j] << " "; cout << endl;}
  int best = h * w;
  rep(i, 0, h) rep(j, 0, w) {
    if (seen[i][j]) {
      int cur = min(min(i, j), min(h - i - 1, w - j - 1));
      if (cur == 0) {
        cout << 1 << endl;
        return 0;
      }
      cur = (cur - 1) / k + 1;
      best = min(best, cur);
    }
  }
  cout << best + 1 << endl;
}


Fixed submission:
// webcode about science as the new webcode about pirates/monkeys/ninjas? here's
// hoping!
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, a, n) for (int i = a; i < (int)(n); i++)
#define per(i, a, n) for (int i = (n)-1; i >= (int)(a); i--)
template <typename T> ostream &operator<<(ostream &s, vector<T> t) {
  rep(i, 0, t.size()) s << (i ? " " : "") << t[i];
  return s;
}
template <typename T> istream &operator>>(istream &s, vector<T> &t) {
  rep(i, 0, t.size()) s >> t[i];
  return s;
}
template <typename T, typename U>
ostream &operator<<(ostream &s, pair<T, U> t) {
  s << "(" << t.first << "," << t.second << ")";
  return s;
}
template <typename T, typename U>
istream &operator>>(istream &s, pair<T, U> &t) {
  s >> t.first >> t.second;
  return s;
}
typedef long long ll;

const int N = 888;
bool seen[N][N];
int rem[N][N];
int di[4] = {-1, 0, 1, 0};
int dj[4] = {0, 1, 0, -1};

vector<string> s;
int h, w;

bool canGo(int i, int j) {
  if (i < 0 || j < 0 || i >= h || j >= w)
    return false;
  return s[i][j] != '#';
}

int main() {
  ios_base::sync_with_stdio(false);
  int k;
  cin >> h >> w >> k;
  s.resize(h);
  cin >> s;
  int i0 = 0, j0 = 0;
  rep(i, 0, h) rep(j, 0, w) {
    if (s[i][j] == 'S') {
      i0 = i;
      j0 = j;
    }
  }
  queue<pair<int, int>> q;
  q.push({i0, j0});
  rem[i0][j0] = k;
  while (!q.empty()) {
    int i, j;
    tie(i, j) = q.front();
    q.pop();
    seen[i][j] = true;
    if (rem[i][j] == 0)
      continue;

    rep(dir, 0, 4) {
      int ni = i + di[dir];
      int nj = j + dj[dir];
      if (!canGo(ni, nj) || seen[ni][nj])
        continue;
      seen[ni][nj] = true;
      rem[ni][nj] = rem[i][j] - 1;
      q.push({ni, nj});
    }
  }

  // rep(i, 0, h) {rep(j, 0, w) cout << seen[i][j] << " "; cout << endl;}
  int best = h * w;
  rep(i, 0, h) rep(j, 0, w) {
    if (seen[i][j]) {
      int cur = min(min(i, j), min(h - i - 1, w - j - 1));
      if (cur == 0) {
        cout << 1 << endl;
        return 0;
      }
      cur = (cur - 1) / k + 1;
      best = min(best, cur);
    }
  }
  cout << best + 1 << endl;
}