Problem p03725 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define mp make_pair
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n; i >= 0; --i)
const int inf = 1e9 + 7;
const ll mod = 1e9 + 7;
const ll big = 1e18;
const double PI = 2 * asin(1);

int main() {
  int H, W, K;
  cin >> H >> W >> K;
  vector<string> A(H);
  for (int i = 0; i < H; ++i)
    cin >> A[i];
  int sx, sy;
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      if (A[i][j] == 'S') {
        sx = i;
        sy = j;
      }
    }
  }
  queue<pair<int, int>> que;
  queue<int> numque;
  que.push(mp(sx, sy));
  numque.push(0);
  map<pair<int, int>, int> amap;
  pair<int, int> state;
  int x, y, num;
  while (!que.empty()) {
    state = que.front();
    que.pop();
    num = numque.front();
    numque.pop();
    x = state.fi;
    y = state.se;
    if (amap[mp(x, y)] > 0)
      continue;
    if (num > K)
      break;
    A[x][y] = '1';
    if (amap[mp(x - 1, y)] == 0 && x - 1 >= 0 && A[x - 1][y] == '.') {
      que.push(mp(x - 1, y));
      numque.push(num + 1);
    }
    if (amap[mp(x + 1, y)] == 0 && x + 1 < H && A[x + 1][y] == '.') {
      que.push(mp(x + 1, y));
      numque.push(num + 1);
    }
    if (amap[mp(x, y - 1)] == 0 && y - 1 >= 0 && A[x][y - 1] == '.') {
      que.push(mp(x, y - 1));
      numque.push(num + 1);
    }
    if (amap[mp(x, y + 1)] == 0 && y + 1 < W && A[x][y + 1] == '.') {
      que.push(mp(x, y + 1));
      numque.push(num + 1);
    }
  }
  int ans = inf;
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      if (A[i][j] == '#' || A[i][j] == '.')
        continue;
      if (i % K == 0) {
        ans = min(ans, A[i][j] - '0' + i / K);
      } else {
        ans = min(ans, A[i][j] - '0' + i / K + 1);
      }
      if ((H - i - 1) % K == 0) {
        ans = min(ans, A[i][j] - '0' + (H - i - 1) / K);
      } else {
        ans = min(ans, A[i][j] - '0' + (H - i - 1) / K + 1);
      }
      if (j % K == 0) {
        ans = min(ans, A[i][j] - '0' + j / K);
      } else {
        ans = min(ans, A[i][j] - '0' + j / K + 1);
      }
      if ((W - j - 1) % K == 0) {
        ans = min(ans, A[i][j] - '0' + (W - j - 1) / K);
      } else {
        ans = min(ans, A[i][j] - '0' + (W - j - 1) / K + 1);
      }
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define mp make_pair
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n; i >= 0; --i)
const int inf = 1e9 + 7;
const ll mod = 1e9 + 7;
const ll big = 1e18;
const double PI = 2 * asin(1);

int main() {
  int H, W, K;
  cin >> H >> W >> K;
  vector<string> A(H);
  for (int i = 0; i < H; ++i)
    cin >> A[i];
  int sx, sy;
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      if (A[i][j] == 'S') {
        sx = i;
        sy = j;
      }
    }
  }
  queue<pair<int, int>> que;
  queue<int> numque;
  que.push(mp(sx, sy));
  numque.push(0);
  map<pair<int, int>, int> amap;
  pair<int, int> state;
  int x, y, num;
  while (!que.empty()) {
    state = que.front();
    que.pop();
    num = numque.front();
    numque.pop();
    x = state.fi;
    y = state.se;
    if (amap[mp(x, y)] > 0)
      continue;
    if (num > K)
      break;
    A[x][y] = '1';
    amap[mp(x, y)]++;
    if (amap[mp(x - 1, y)] == 0 && x - 1 >= 0 && A[x - 1][y] == '.') {
      que.push(mp(x - 1, y));
      numque.push(num + 1);
    }
    if (amap[mp(x + 1, y)] == 0 && x + 1 < H && A[x + 1][y] == '.') {
      que.push(mp(x + 1, y));
      numque.push(num + 1);
    }
    if (amap[mp(x, y - 1)] == 0 && y - 1 >= 0 && A[x][y - 1] == '.') {
      que.push(mp(x, y - 1));
      numque.push(num + 1);
    }
    if (amap[mp(x, y + 1)] == 0 && y + 1 < W && A[x][y + 1] == '.') {
      que.push(mp(x, y + 1));
      numque.push(num + 1);
    }
  }
  int ans = inf;
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      if (A[i][j] == '#' || A[i][j] == '.')
        continue;
      if (i % K == 0) {
        ans = min(ans, A[i][j] - '0' + i / K);
      } else {
        ans = min(ans, A[i][j] - '0' + i / K + 1);
      }
      if ((H - i - 1) % K == 0) {
        ans = min(ans, A[i][j] - '0' + (H - i - 1) / K);
      } else {
        ans = min(ans, A[i][j] - '0' + (H - i - 1) / K + 1);
      }
      if (j % K == 0) {
        ans = min(ans, A[i][j] - '0' + j / K);
      } else {
        ans = min(ans, A[i][j] - '0' + j / K + 1);
      }
      if ((W - j - 1) % K == 0) {
        ans = min(ans, A[i][j] - '0' + (W - j - 1) / K);
      } else {
        ans = min(ans, A[i][j] - '0' + (W - j - 1) / K + 1);
      }
    }
  }
  cout << ans << endl;
}