Problem p02644 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define all(x) (x).begin(), (x).end()
#define P_B push_back
#define PO_B pop_back
#define E_B emplace_back
#define M_P make_pair
#define fi first
#define se second
#define debug(x) cout << #x << ": " << x << endl

using namespace std;
using ll = long long;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;
using VI = vector<int>;
using VL = vector<ll>;
using VVI = vector<vector<int>>;
using VVL = vector<vector<ll>>;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
// const ll LINF = (ll)1e18 - 1;
// const int INF = 1e9 - 1;
// int:±2*10^9くらい　ll:±9*10^18くらい

void Main() {
  int H, W, K, x1, x2, y1, y2;
  bool done = false;

  cin >> H >> W >> K >> x1 >> y1 >> x2 >> y2;

  vector<string> c(H + 2);
  VVI D(H + 2, VI(W + 2, -1));

  rep(i, W + 2) {
    c[0] += '@';
    c[H + 1] += '@';
  }
  rep(i, H) {
    string C;
    cin >> C;
    c[i + 1] = '@' + C + '@';
  }

  queue<Pii> Q;
  D[x1][y1] = 0;

  Q.push(M_P(x1, y1));

  while (!Q.empty()) {
    int xp, yp, xn, yn;
    xp = Q.front().fi;
    yp = Q.front().se;
    Q.pop();

    rep(i, 4) {
      xn = xp;
      yn = yp;
      rep(k, K) {
        xn += dx[i];
        yn += dy[i];
        if (c[xn][yn] == '@')
          break;
        if (D[xn][yn] >= 0 && D[xn][yn] <= D[xp][yp])
          break;
        if (D[xn][yn] != -1)
          continue;
        D[xn][yn] = D[xp][yp] + 1;
        if (x2 == xn && y2 == yn) {
          done = true;
          break;
        }
        Q.push(M_P(xn, yn));
      }
      if (done)
        break;
    }
    if (done)
      break;
  }

  if (done)
    cout << D[x2][y2] << endl;
  else
    cout << -1 << endl;
}

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Main();

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define all(x) (x).begin(), (x).end()
#define P_B push_back
#define PO_B pop_back
#define E_B emplace_back
#define M_P make_pair
#define fi first
#define se second
#define debug(x) cout << #x << ": " << x << endl

using namespace std;
using ll = long long;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;
using VI = vector<int>;
using VL = vector<ll>;
using VVI = vector<vector<int>>;
using VVL = vector<vector<ll>>;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
// const ll LINF = (ll)1e18 - 1;
// const int INF = 1e9 - 1;
// int:±2*10^9くらい　ll:±9*10^18くらい

void Main() {
  int H, W, K, x1, x2, y1, y2;
  bool done = false;

  cin >> H >> W >> K >> x1 >> y1 >> x2 >> y2;

  vector<string> c(H + 2);
  VVI D(H + 2, VI(W + 2, -1));

  rep(i, W + 2) {
    c[0] += '@';
    c[H + 1] += '@';
  }
  rep(i, H) {
    string C;
    cin >> C;
    c[i + 1] = '@' + C + '@';
  }

  queue<Pii> Q;
  D[x1][y1] = 0;

  Q.push(M_P(x1, y1));

  while (!Q.empty()) {
    int xp, yp, xn, yn;
    xp = Q.front().fi;
    yp = Q.front().se;
    Q.pop();

    rep(i, 4) {
      xn = xp;
      yn = yp;
      rep(k, K) {
        xn += dx[i];
        yn += dy[i];
        if (c[xn][yn] == '@')
          break;
        if (D[xn][yn] >= 0 && D[xn][yn] < D[xp][yp])
          break;
        if (D[xn][yn] != -1)
          continue;
        D[xn][yn] = D[xp][yp] + 1;
        if (x2 == xn && y2 == yn) {
          done = true;
          break;
        }
        Q.push(M_P(xn, yn));
      }
      if (done)
        break;
    }
    if (done)
      break;
  }

  if (done)
    cout << D[x2][y2] << endl;
  else
    cout << -1 << endl;
}

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Main();

  return 0;
}