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

//	ZapZu's code hohoho
//	#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define dfs_black 1
#define dfs_white -1
#define pr pair
#define vt vector
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

typedef double db;
typedef long long li;
typedef long double ld;

typedef pr<int, int> ii;
typedef pr<ld, ld> dd;

typedef vt<int> vi;
typedef vt<li> vli;
typedef vt<ld> vld;
typedef vt<ii> vii;

typedef map<int, int> mii;
typedef map<int, bool> mib;
typedef map<int, char> mic;

typedef set<int> s_i;
typedef set<char> s_c;

const int MOD = 1e9 + 7;
const li INF = 1e18;
const ld PI = 4 * atan((ld)1);
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
signed main() {
  //	freopen(".inp", "r", stdin);
  //	freopen(".out", "w", stdout);
  fastio;
  int n, m, k;
  cin >> n >> m >> k;
  int d[n + 5][m + 5];
  int x1, x2, y1, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      char c;
      cin >> c;
      if (c == '.')
        d[i][j] = INT_MAX;
      else
        d[i][j] = -1;
    }
  }
  d[x1][y1] = 0;
  queue<pr<int, ii>> q;
  q.push({0, {x1, y1}});
  while (!q.empty()) {
    int xu = q.front().se.fi, yu = q.front().se.se, dist = q.front().fi;
    q.pop();
    if (dist > d[xu][yu])
      continue;
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= k; j++) {
        int xv = xu + dx[i] * j, yv = yu + dy[i] * j;
        if (1 <= xv and xv <= n and 1 <= yv and yv <= m and d[xv][yv] != -1 and
            d[xv][yv] > d[xu][yu] + 1) {
          d[xv][yv] = d[xu][yu] + 1;
          q.push({d[xv][yv], {xv, yv}});
        } else if (d[xv][yv] <= d[xu][yu])
          break;
      }
    }
  }
  if (-1 < d[x2][y2] and d[x2][y2] < INT_MAX)
    cout << d[x2][y2];
  else
    cout << -1;
}


Error: TLE

Fixed submission:

//	ZapZu's code hohoho
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define dfs_black 1
#define dfs_white -1
#define pr pair
#define vt vector
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

typedef double db;
typedef long long li;
typedef long double ld;

typedef pr<int, int> ii;
typedef pr<ld, ld> dd;

typedef vt<int> vi;
typedef vt<li> vli;
typedef vt<ld> vld;
typedef vt<ii> vii;

typedef map<int, int> mii;
typedef map<int, bool> mib;
typedef map<int, char> mic;

typedef set<int> s_i;
typedef set<char> s_c;

const int MOD = 1e9 + 7;
const li INF = 1e18;
const ld PI = 4 * atan((ld)1);
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
vector<vi> d;
signed main() {
  //	freopen(".inp", "r", stdin);
  //	freopen(".out", "w", stdout);
  fastio;
  int n, m, k;
  cin >> n >> m >> k;
  d.assign(n + 5, vi(m + 5, 0));
  int x1, x2, y1, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      char c;
      cin >> c;
      if (c == '.')
        d[i][j] = INT_MAX;
      else
        d[i][j] = -1;
    }
  }
  d[x1][y1] = 0;
  queue<pr<int, ii>> q;
  q.push({0, {x1, y1}});
  while (!q.empty()) {
    int xu = q.front().se.fi, yu = q.front().se.se, dist = q.front().fi;
    q.pop();
    if (dist > d[xu][yu])
      continue;
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= k; j++) {
        int xv = xu + dx[i] * j, yv = yu + dy[i] * j;
        if (1 <= xv and xv <= n and 1 <= yv and yv <= m and d[xv][yv] != -1 and
            d[xv][yv] >= d[xu][yu] + 1) {
          d[xv][yv] = d[xu][yu] + 1;
          q.push({d[xv][yv], {xv, yv}});
        } else
          break;
      }
    }
  }
  if (-1 < d[x2][y2] and d[x2][y2] < INT_MAX)
    cout << d[x2][y2];
  else
    cout << -1;
}