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

#include <bits/stdc++.h>
using namespace std;
#define vb __int128
#define ll long long
#define ld long double
#define full(a) a.begin(), a.end()
#define fr(i, a, b) for (int i = a; i < b; i++)
#define stoink stack<ll>
#define quu queue<ll>
#define quee queue<pair<ll, ll>>
#define vec vector<ll>
#define vg vector<vector<ll>>
#define eg vector<pair<ll, ll>>
#define vgw vector<vector<pair<ll, ll>>>
#define pa pair<ll, ll>
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define pre 0.00000000000000000001
#define inf 100000000000000000
#define mod 998244353
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll n, m, k;
  cin >> n >> m >> k;
  ll x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;
  vector<string> arr(n);
  fr(i, 0, n) cin >> arr[i];
  queue<pair<ll, pa>> pq;
  vg dist(n, vec(m, inf));
  dist[x1][y1] = 0;
  pq.push({0, {x1, y1}});
  while (!pq.empty()) {
    pair<ll, pa> ver = pq.front();
    pq.pop();
    ll x = ver.ss.ff;
    ll y = ver.ss.ss;
    ll weight = ver.ff;
    ll i;
    for (i = 1; i <= k; i++) {
      ll xx = x + i;
      ll yy = y;
      if (xx < n && xx >= 0 && yy < m && yy >= 0) {
        if (arr[xx][yy] == '@' || dist[xx][yy] <= dist[x][y])
          break;
        if (dist[xx][yy] == inf) {
          dist[xx][yy] = weight + 1;
          pq.push({weight + 1, {xx, yy}});
        }
      } else
        break;
    }
    for (i = 1; i <= k; i++) {
      ll xx = x;
      ll yy = y - i;
      if (xx < n && xx >= 0 && yy < m && yy >= 0) {
        if (arr[xx][yy] == '@' || dist[xx][yy] <= dist[x][y])
          break;
        if (dist[xx][yy] == inf) {
          dist[xx][yy] = weight + 1;
          pq.push({weight + 1, {xx, yy}});
        }

      } else
        break;
    }
    for (i = 1; i <= k; i++) {
      ll xx = x;
      ll yy = y + i;
      if (xx < n && xx >= 0 && yy < m && yy >= 0) {
        if (arr[xx][yy] == '@' || dist[xx][yy] <= dist[x][y])
          break;
        if (dist[xx][yy] == inf) {
          dist[xx][yy] = weight + 1;
          pq.push({weight + 1, {xx, yy}});
        }
      } else
        break;
    }
    for (i = 1; i <= k; i++) {
      ll xx = x - i;
      ll yy = y;
      if (xx < n && xx >= 0 && yy < m && yy >= 0) {
        if (arr[xx][yy] == '@' || dist[xx][yy] <= dist[x][y])
          break;
        if (dist[xx][yy] == inf) {
          dist[xx][yy] = weight + 1;
          pq.push({weight + 1, {xx, yy}});
        }
      } else
        break;
    }
  }
  if (dist[x2][y2] == inf)
    cout << -1;
  else
    cout << dist[x2][y2];
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define vb __int128
#define ll long long
#define ld long double
#define full(a) a.begin(), a.end()
#define fr(i, a, b) for (int i = a; i < b; i++)
#define stoink stack<ll>
#define quu queue<ll>
#define quee queue<pair<ll, ll>>
#define vec vector<ll>
#define vg vector<vector<ll>>
#define eg vector<pair<ll, ll>>
#define vgw vector<vector<pair<ll, ll>>>
#define pa pair<ll, ll>
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define pre 0.00000000000000000001
#define inf 100000000000000000
#define mod 998244353
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll n, m, k;
  cin >> n >> m >> k;
  ll x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;
  vector<string> arr(n);
  fr(i, 0, n) cin >> arr[i];
  queue<pair<ll, pa>> pq;
  vg dist(n, vec(m, inf));
  dist[x1][y1] = 0;
  pq.push({0, {x1, y1}});
  while (!pq.empty()) {
    pair<ll, pa> ver = pq.front();
    pq.pop();
    ll x = ver.ss.ff;
    ll y = ver.ss.ss;
    ll weight = ver.ff;
    ll i;
    for (i = 1; i <= k; i++) {
      ll xx = x + i;
      ll yy = y;
      if (xx < n && xx >= 0 && yy < m && yy >= 0) {
        if (arr[xx][yy] == '@')
          break;
        if (dist[xx][yy] > weight + 1) {
          dist[xx][yy] = weight + 1;
          pq.push({weight + 1, {xx, yy}});
        }
      }
    }
    for (i = 1; i <= k; i++) {
      ll xx = x;
      ll yy = y - i;
      if (xx < n && xx >= 0 && yy < m && yy >= 0) {
        if (arr[xx][yy] == '@')
          break;
        if (dist[xx][yy] > weight + 1) {
          dist[xx][yy] = weight + 1;
          pq.push({weight + 1, {xx, yy}});
        }
      }
    }
    for (i = 1; i <= k; i++) {
      ll xx = x;
      ll yy = y + i;
      if (xx < n && xx >= 0 && yy < m && yy >= 0) {
        if (arr[xx][yy] == '@')
          break;
        if (dist[xx][yy] > weight + 1) {
          dist[xx][yy] = weight + 1;
          pq.push({weight + 1, {xx, yy}});
        }
      }
    }
    for (i = 1; i <= k; i++) {
      ll xx = x - i;
      ll yy = y;
      if (xx < n && xx >= 0 && yy < m && yy >= 0) {
        if (arr[xx][yy] == '@')
          break;
        if (dist[xx][yy] > weight + 1) {
          dist[xx][yy] = weight + 1;
          pq.push({weight + 1, {xx, yy}});
        }
      } else
        break;
    }
  }
  if (dist[x2][y2] == inf)
    cout << -1;
  else
    cout << dist[x2][y2];
  return 0;
}