Problem p03725 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

using namespace std;

int main() {
  int h, w, k;
  string s;
  cin >> h >> w >> k;
  vector<string> v;
  for (int i = 0; i < h; i++) {
    cin >> s;
    v.push_back(s);
  }
  queue<pair<int, int>> q, q2;
  int flag[802][802] = {};
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (v[i][j] == 'S') {
        flag[i][j] = 1;
        q.push(make_pair(i, j));
      } else if (v[i][j] == '#') {
        flag[i][j] = -1;
      }
    }
  }
  int count = 0;
  pair<int, int> p;
  int x, y;
  while (count < k) {
    while (!q.empty()) {
      p = q.front();
      q.pop();
      x = p.first;
      y = p.second;
      if (x + 1 < h) {
        if (flag[x + 1][y] == 0) {
          flag[x + 1][y] = 1;
          q2.push(make_pair(x + 1, y));
        }
      }
      if (y + 1 < w) {
        if (flag[x][y + 1] == 0) {
          flag[x][y + 1] = 1;
          q2.push(make_pair(x, y + 1));
        }
      }
      if (x > 0) {
        if (flag[x - 1][y] == 0) {
          flag[x - 1][y] = 1;
          q2.push(make_pair(x - 1, y));
        }
      }
      if (y > 0) {
        if (flag[x][y - 1] == 0) {
          flag[x][y - 1] = 1;
          q2.push(make_pair(x, y - 1));
        }
      }
    }
    q = q2;
    count += 1;
  }
  int min = 100000000;
  int tmp;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (flag[i][j] == 1) {
        tmp = std::min(i, j);
        tmp = std::min(tmp, h - i - 1);
        tmp = std::min(tmp, w - j - 1);
        if (min > tmp) {
          min = tmp;
        }
      }
    }
  }
  if (min == 0) {
    cout << 1 << endl;
  } else {
    if (min % k == 0) {
      cout << 1 + min / k << endl;
    } else {
      cout << 1 + min / k + 1 << endl;
    }
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

using namespace std;

int main() {
  int h, w, k;
  string s;
  cin >> h >> w >> k;
  vector<string> v;
  for (int i = 0; i < h; i++) {
    cin >> s;
    v.push_back(s);
  }
  queue<pair<int, int>> q, q2;
  int flag[802][802] = {};
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (v[i][j] == 'S') {
        flag[i][j] = 1;
        q.push(make_pair(i, j));
      } else if (v[i][j] == '#') {
        flag[i][j] = -1;
      }
    }
  }
  int count = 0;
  pair<int, int> p;
  int x, y;
  while (count < k) {
    while (!q.empty()) {
      p = q.front();
      q.pop();
      x = p.first;
      y = p.second;
      if (x + 1 < h) {
        if (flag[x + 1][y] == 0) {
          flag[x + 1][y] = 1;
          q2.push(make_pair(x + 1, y));
        }
      }
      if (y + 1 < w) {
        if (flag[x][y + 1] == 0) {
          flag[x][y + 1] = 1;
          q2.push(make_pair(x, y + 1));
        }
      }
      if (x > 0) {
        if (flag[x - 1][y] == 0) {
          flag[x - 1][y] = 1;
          q2.push(make_pair(x - 1, y));
        }
      }
      if (y > 0) {
        if (flag[x][y - 1] == 0) {
          flag[x][y - 1] = 1;
          q2.push(make_pair(x, y - 1));
        }
      }
    }
    q = q2;
    while (!q2.empty()) {
      q2.pop();
    }
    count += 1;
  }
  int min = 100000000;
  int tmp;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (flag[i][j] == 1) {
        tmp = std::min(i, j);
        tmp = std::min(tmp, h - i - 1);
        tmp = std::min(tmp, w - j - 1);
        if (min > tmp) {
          min = tmp;
        }
      }
    }
  }
  if (min == 0) {
    cout << 1 << endl;
  } else {
    if (min % k == 0) {
      cout << 1 + min / k << endl;
    } else {
      cout << 1 + min / k + 1 << endl;
    }
  }
  return 0;
}