Problem p03725 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <assert.h>
#include <cstring>
#include <iterator>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
#pragma warning(disable : 4996)

typedef long long ll;
#define MIN(a, b) ((a) > (b) ? (b) : (a))
#define MAX(a, b) ((a) < (b) ? (b) : (a))
#define LINF 9223300000000000000
#define INF 2140000000
const long long MOD = 1000000007;
using namespace std;

char str[800][805];

int main(int argc, char *argv[]) {
  int h, w, K;
  scanf("%d%d%d", &h, &w, &K);

  vector<int> vis(h * w);

  int si = -1, sj = -1;
  int i, j;
  for (i = 0; i < h; i++) {
    scanf("%s", str[i]);
    for (j = 0; j < w; j++) {
      if (str[i][j] == 'S') {
        si = i;
        sj = j;
      }
    }
  }
  assert(si >= 0 && sj >= 0);

  int dx[4] = {1, -1, 0, 0};
  int dy[4] = {0, 0, 1, -1};
#define P(i, j) ((i)*w + (j))
  queue<pair<int, int>> que0;
  que0.push(make_pair(P(si, sj), 0));

  int min = INF;
  while (!que0.empty()) {
    auto tmp = que0.front();
    int curr = tmp.first;
    int dist = tmp.second;
    que0.pop();
    int i = curr / w;
    int j = curr % w;
    vis[P(i, j)] = 1;
    if (i == 0 || i == h - 1 || j == 0 || j == w - 1) {
      printf("1\n");
      return 0;
    }
    min = MIN(min, i);
    min = MIN(min, h - 1 - i);
    min = MIN(min, j);
    min = MIN(min, w - 1 - j);

    int p;
    for (p = 0; p < 4; p++) {
      int i2 = i + dx[p];
      int j2 = j + dy[p];
      int dist2 = dist + 1;
      if (i2 < 0 || i2 >= h || j2 < 0 || j2 >= w)
        continue;
      if (dist < K && str[i2][j2] == '.' && !vis[P(i2, j2)]) {
        int dist2 = dist + 1;
        que0.push(make_pair(P(i2, j2), dist2));
      }
    }
  }
  printf("%d\n", (min - 1 + K) / K + 1);

  return 0;
}


Fixed submission:
#include <algorithm>
#include <assert.h>
#include <cstring>
#include <iterator>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
#pragma warning(disable : 4996)

typedef long long ll;
#define MIN(a, b) ((a) > (b) ? (b) : (a))
#define MAX(a, b) ((a) < (b) ? (b) : (a))
#define LINF 9223300000000000000
#define INF 2140000000
const long long MOD = 1000000007;
using namespace std;

char str[800][805];

int main(int argc, char *argv[]) {
  int h, w, K;
  scanf("%d%d%d", &h, &w, &K);

  vector<int> vis(h * w);

  int si = -1, sj = -1;
  int i, j;
  for (i = 0; i < h; i++) {
    scanf("%s", str[i]);
    for (j = 0; j < w; j++) {
      if (str[i][j] == 'S') {
        si = i;
        sj = j;
      }
    }
  }
  assert(si >= 0 && sj >= 0);

  int dx[4] = {1, -1, 0, 0};
  int dy[4] = {0, 0, 1, -1};
#define P(i, j) ((i)*w + (j))
  queue<pair<int, int>> que0;
  que0.push(make_pair(P(si, sj), 0));

  int min = INF;
  while (!que0.empty()) {
    auto tmp = que0.front();
    int curr = tmp.first;
    int dist = tmp.second;
    que0.pop();
    int i = curr / w;
    int j = curr % w;
    vis[P(i, j)] = 1;
    if (i == 0 || i == h - 1 || j == 0 || j == w - 1) {
      printf("1\n");
      return 0;
    }
    min = MIN(min, i);
    min = MIN(min, h - 1 - i);
    min = MIN(min, j);
    min = MIN(min, w - 1 - j);

    int p;
    for (p = 0; p < 4; p++) {
      int i2 = i + dx[p];
      int j2 = j + dy[p];
      int dist2 = dist + 1;
      if (i2 < 0 || i2 >= h || j2 < 0 || j2 >= w)
        continue;
      if (dist < K && str[i2][j2] == '.' && !vis[P(i2, j2)]) {
        int dist2 = dist + 1;
        que0.push(make_pair(P(i2, j2), dist2));
        vis[P(i2, j2)] = 1;
      }
    }
  }
  printf("%d\n", (min - 1 + K) / K + 1);

  return 0;
}