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

#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
typedef int ll; // int:2*10**9
typedef long double ld;
typedef pair<ll, ll> P;
#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPS(i, n) for (ll i = 1; i <= (ll)(n); i++)
#define REPD(i, n) for (ll i = (ll)(n)-1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = (a); i <= (b); i++)
#define FORD(i, a, b) for (ll i = (a); i >= (b); i--)
#define vec2(name, i, j) vector<vector<ll>> name(i, vector<ll>(j))
#define vec2f(name, i, j, k) vector<vector<ll>> name(i, vector<ll>(j, k))
#define vec3(name, i, j, k)                                                    \\
  vector<vector<vector<ll>>> name(i, vector<vector<ll>>(j, vector<ll>(k)))
#define vec3f(name, i, j, k, l)                                                \\
  vector<vector<vector<ll>>> name(i, vector<vector<ll>>(j, vector<ll>(k, l)))
#define vec4(name, i, j, k, l)                                                 \\
  vector<vector<vector<vector<ll>>>> name(                                     \\
      i, vector<vector<vector<ll>>>(j, vector<vector<ll>>(k, vector<ll>(l))))
#define vec4f(name, i, j, k, l, m)                                             \\
  vector<vector<vector<vector<ll>>>> name(                                     \\
      i,                                                                       \\
      vector<vector<vector<ll>>>(j, vector<vector<ll>>(k, vector<ll>(l, m))))
#define pb push_back
#define MOD 1000000007
#define MOD2 998244353
#define PI 3.141592653
#define INF 100000000 // 8
#define N 210000

vector<ll> dx = {-1, 0, 1, 0};
vector<ll> dy = {0, -1, 0, 1};
vector<vector<ll>> dist;
vector<string> field;
ll h, w, k;

void bfs(ll xs, ll ys) {
  queue<P> q;
  dist[xs][ys] = 0;
  q.push(P(xs, ys));
  while (!q.empty()) {
    ll nowx = q.front().first;
    ll nowy = q.front().second;
    // cout << nowx << "now" << nowy << endl;
    q.pop();
    REP(i, 4) {
      FOR(j, 1, k) {
        ll nex = nowx + j * dx[i];
        ll ney = nowy + j * dy[i];
        // cout << nex << "next" << ney << endl;
        if (nex < 0 || nex >= h || ney < 0 || ney >= w)
          break;
        if (field[nex][ney] == '@')
          break;
        if (dist[nex][ney] != -1 && dist[nex][ney] <= dist[nowx][nowy])
          break;
        if (dist[nex][ney] != -1)
          continue;
        dist[nex][ney] = dist[nowx][nowy] + 1;
        q.push(P(nex, ney));
      }
    }
  }
}

int main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);
  cin >> h >> w >> k;
  dist = vector<vector<ll>>(h, vector<ll>(w, -1));
  field = vector<string>(h);
  ll xs, ys, xg, yg;
  cin >> xs >> ys >> xg >> yg;
  xs--;
  ys--;
  xg--;
  yg--;
  REP(i, h) cin >> field[i];
  bfs(xs, ys);
  cout << dist[xg][yg] << endl;
  /*REP(i,h){
      REP(j,w) cout << dist[i][j] << " ";
      cout << endl;
  }*/
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
typedef int ll; // int:2*10**9
typedef long double ld;
#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPS(i, n) for (ll i = 1; i <= (ll)(n); i++)
#define REPD(i, n) for (ll i = (ll)(n)-1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = (a); i <= (b); i++)
#define FORD(i, a, b) for (ll i = (a); i >= (b); i--)
#define vec2(name, i, j) vector<vector<ll>> name(i, vector<ll>(j))
#define vec2f(name, i, j, k) vector<vector<ll>> name(i, vector<ll>(j, k))
#define vec3(name, i, j, k)                                                    \\
  vector<vector<vector<ll>>> name(i, vector<vector<ll>>(j, vector<ll>(k)))
#define vec3f(name, i, j, k, l)                                                \\
  vector<vector<vector<ll>>> name(i, vector<vector<ll>>(j, vector<ll>(k, l)))
#define vec4(name, i, j, k, l)                                                 \\
  vector<vector<vector<vector<ll>>>> name(                                     \\
      i, vector<vector<vector<ll>>>(j, vector<vector<ll>>(k, vector<ll>(l))))
#define vec4f(name, i, j, k, l, m)                                             \\
  vector<vector<vector<vector<ll>>>> name(                                     \\
      i,                                                                       \\
      vector<vector<vector<ll>>>(j, vector<vector<ll>>(k, vector<ll>(l, m))))
#define pb push_back
#define MOD 1000000007
#define MOD2 998244353
#define PI 3.141592653
#define INF 100000000 // 8
#define N 210000

vector<ll> dx = {-1, 0, 1, 0};
vector<ll> dy = {0, -1, 0, 1};

int main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);
  ll h, w, k;
  cin >> h >> w >> k;
  ll xs, ys, xg, yg;
  cin >> xs >> ys >> xg >> yg;
  vec2f(pond, h + 2, w + 2, -1);
  FOR(i, 1, h) FOR(j, 1, w) pond[i][j] = INF;
  REP(i, h) {
    string s;
    cin >> s;
    REP(j, w) if (s[j] == '@') pond[i + 1][j + 1] = -1;
  }
  priority_queue<vector<ll>, vector<vector<ll>>, greater<vector<ll>>> q;
  q.push({0, 0, -1, xs, ys});
  pond[xs][ys] = 0;
  while (q.size() > 0) {
    ll l = q.top().at(0);
    ll z = q.top().at(1);
    ll dir = q.top().at(2);
    ll x = q.top().at(3);
    ll y = q.top().at(4);
    /*FOR(i,1,h){
        FOR(j,1,w) cout << pond[i][j] << " ";
        cout << endl;
    }*/
    pond[x][y] = l;
    if (x == xg && y == yg) {
      cout << pond[xg][yg] << endl;
      return 0;
    }
    if (z == k) {
      REP(i, 4) if (pond[x + dx[i]][y + dy[i]] == INF) {
        q.push({l + 1, 1, i, x + dx[i], y + dy[i]});
      }
    } else {
      REP(i, 4) if (pond[x + dx[i]][y + dy[i]] == INF) {
        if (i == dir) {
          ll p = 1;
          while (p <= k - z) {
            if (pond[x + p * dx[i]][y + p * dy[i]] != INF) {
              break;
            } else {
              pond[x + p * dx[i]][y + p * dy[i]] = l;
              q.push({l, z + p, dir, x + p * dx[i], y + p * dy[i]});
            }
            if (x + p * dx[i] == xg && y + p * dy[i] == yg) {
              cout << l << endl;
              return 0;
            }
            p++;
          }
        } else {
          q.push({l + 1, 1, i, x + dx[i], y + dy[i]});
        }
      }
    }
    q.pop();
  }
  cout << -1 << endl;
  return 0;
}