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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ordered_set                                                            \\
  tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
#define int long long
typedef unsigned long long lu;
typedef vector<ll> v;
typedef vector<vector<ll>> vv;
typedef vector<string> vs;
typedef vector<pair<ll, ll>> vpr;
typedef long double ld;
#define f(i, n) for (ll i = 0; i < n; i++)
#define ff(i, n) for (ll i = 1; i <= n; i++)
#define pb push_back
#define mp make_pair
#define endl "\\n"
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define amax(x, y)                                                             \\
  if (y > x)                                                                   \\
    x = y;
#define amin(x, y)                                                             \\
  if (y < x)                                                                   \\
    x = y;
#define bg(x) x.begin()
#define sz(x) (ll) x.size()
#define in(x, n)                                                               \\
  for (ll i = 0; i < n; i++)                                                   \\
  cin >> x[i]
#define out(x, n)                                                              \\
  for (ll i = 0; i < n; i++)                                                   \\
  cout << x[i] << " "
#define mxt(a) *(max_element(a.begin(), a.end()))
#define mnt(a) *(min_element(a.begin(),a.end())
#define tc                                                                     \\
  ll t;                                                                        \\
  cin >> t;                                                                    \\
  while (t--)
typedef pair<ll, ll> pi;
#define yes cout << "YES\\n";
#define no cout << "NO\\n";
#define yesno(f)                                                               \\
  if (f)                                                                       \\
  yes else no
const v dx = {1, -1, 0, 0};
const v dy = {0, 0, 1, -1};
const ld PI = 2 * acos(0.0);
ll cel(ll x1, ll y1) {
  if ((x1 % y1) == 0)
    return x1 / y1;
  else
    return x1 / y1 + 1;
}
ll power(ll a, ll b, ll m) {
  if (b == 0)
    return 1;
  ll d = power(a, b / 2, m);
  d = (d * d) % m;
  if (b & 1)
    d = (d * a) % m;
  return d;
}
const ll mod = 1e9 + 7;
int MOD(int a) {
  if (a < 0)
    a += mod;
  if (a >= mod)
    a %= mod;
  return a;
}
// set_name.find_by_order(k) It returns to an iterator to the kth element
// (counting from zero) in the set in O(logn) time set_name.order_of_key(k) It
// returns to the number of items that are strictly smaller than our item k in
// O(logn) time.
/*string operations :
str.substr (x,y) : returns a substring str[x],str[x+1],...str[x+y-1]
str.substr (x) : returns a substring str[x],... end of string

str.find(qtr) : returns the first occurenece of qtr in str */
#define valid(i, j) (i >= 0 && i < h && j >= 0 && j < w && grid[i][j] == '.')
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int h, w, k;
  cin >> h >> w >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  x2--;
  y1--;
  y2--;
  vs grid(h);
  in(grid, h);
  queue<pi> que;
  que.push({x1, y1});
  const int inf = 1e9;
  vv dist(h, v(w, inf));
  dist[x1][y1] = 0;
  vector<vector<bool>> vst(h, vector<bool>(w, false));
  vst[x1][y1] = true;
  while (!que.empty()) {
    pi u = que.front();
    que.pop();
    int x = u.fi, y = u.se;
    for (int i = 1; i <= k; i++) {
      if (!valid(x + i, y))
        break;
      if (dist[x + i][y] <= dist[x][y])
        break;
      dist[x + i][y] = 1 + dist[x][y];
      if (!vst[x + i][y]) {
        que.push({x + i, y});
        vst[x + i][y] = true;
      }
    }
    for (int i = 1; i <= k; i++) {
      if (!valid(x - i, y))
        break;
      if (dist[x - i][y] <= dist[x][y])
        break;
      dist[x - i][y] = 1 + dist[x][y];
      if (!vst[x - i][y]) {
        que.push({x - i, y});
        vst[x - i][y] = true;
      }
    }
    for (int i = 1; i <= k; i++) {
      if (!valid(x, y + i))
        break;
      if (dist[x][y + i] <= dist[x][y])
        break;
      dist[x][y + i] = 1 + dist[x][y];
      if (!vst[x][y + i]) {
        que.push({x, y + i});
        vst[x][y + i] = true;
      }
    }
    for (int i = 1; i <= k; i++) {
      if (!valid(x, y - i))
        break;
      if (dist[x][y - i] <= dist[x][y])
        break;
      dist[x][y - i] = 1 + dist[x][y];
      if (!vst[x][y - i]) {
        que.push({x, y - i});
        vst[x][y - i] = true;
      }
    }
  }
  if (dist[x2][y2] >= inf) {
    cout << -1 << endl;
  } else
    cout << dist[x2][y2] << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ordered_set                                                            \\
  tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
#define int long long
typedef unsigned long long lu;
typedef vector<ll> v;
typedef vector<vector<ll>> vv;
typedef vector<string> vs;
typedef vector<pair<ll, ll>> vpr;
typedef long double ld;
#define f(i, n) for (ll i = 0; i < n; i++)
#define ff(i, n) for (ll i = 1; i <= n; i++)
#define pb push_back
#define mp make_pair
#define endl "\\n"
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define amax(x, y)                                                             \\
  if (y > x)                                                                   \\
    x = y;
#define amin(x, y)                                                             \\
  if (y < x)                                                                   \\
    x = y;
#define bg(x) x.begin()
#define sz(x) (ll) x.size()
#define in(x, n)                                                               \\
  for (ll i = 0; i < n; i++)                                                   \\
  cin >> x[i]
#define out(x, n)                                                              \\
  for (ll i = 0; i < n; i++)                                                   \\
  cout << x[i] << " "
#define mxt(a) *(max_element(a.begin(), a.end()))
#define mnt(a) *(min_element(a.begin(),a.end())
#define tc                                                                     \\
  ll t;                                                                        \\
  cin >> t;                                                                    \\
  while (t--)
typedef pair<ll, ll> pi;
#define yes cout << "YES\\n";
#define no cout << "NO\\n";
#define yesno(f)                                                               \\
  if (f)                                                                       \\
  yes else no
const v dx = {1, -1, 0, 0};
const v dy = {0, 0, 1, -1};
const ld PI = 2 * acos(0.0);
ll cel(ll x1, ll y1) {
  if ((x1 % y1) == 0)
    return x1 / y1;
  else
    return x1 / y1 + 1;
}
ll power(ll a, ll b, ll m) {
  if (b == 0)
    return 1;
  ll d = power(a, b / 2, m);
  d = (d * d) % m;
  if (b & 1)
    d = (d * a) % m;
  return d;
}
const ll mod = 1e9 + 7;
int MOD(int a) {
  if (a < 0)
    a += mod;
  if (a >= mod)
    a %= mod;
  return a;
}
// set_name.find_by_order(k) It returns to an iterator to the kth element
// (counting from zero) in the set in O(logn) time set_name.order_of_key(k) It
// returns to the number of items that are strictly smaller than our item k in
// O(logn) time.
/*string operations :
str.substr (x,y) : returns a substring str[x],str[x+1],...str[x+y-1]
str.substr (x) : returns a substring str[x],... end of string

str.find(qtr) : returns the first occurenece of qtr in str */
#define valid(i, j) (i >= 0 && i < h && j >= 0 && j < w && grid[i][j] == '.')
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int h, w, k;
  cin >> h >> w >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  x2--;
  y1--;
  y2--;
  vs grid(h);
  in(grid, h);
  queue<pi> que;
  que.push({x1, y1});
  const int inf = 1e9;
  vv dist(h, v(w, inf));
  dist[x1][y1] = 0;
  while (!que.empty()) {
    pi u = que.front();
    que.pop();
    int x = u.fi, y = u.se;
    for (int i = 1; i <= k; i++) {
      if (!valid(x + i, y))
        break;
      if (dist[x + i][y] <= dist[x][y])
        break;
      dist[x + i][y] = 1 + dist[x][y];
      que.push({x + i, y});
    }
    for (int i = 1; i <= k; i++) {
      if (!valid(x - i, y))
        break;
      if (dist[x - i][y] <= dist[x][y])
        break;
      dist[x - i][y] = 1 + dist[x][y];
      que.push({x - i, y});
    }
    for (int i = 1; i <= k; i++) {
      if (!valid(x, y + i))
        break;
      if (dist[x][y + i] <= dist[x][y])
        break;
      dist[x][y + i] = 1 + dist[x][y];
      que.push({x, y + i});
    }
    for (int i = 1; i <= k; i++) {
      if (!valid(x, y - i))
        break;
      if (dist[x][y - i] <= dist[x][y])
        break;
      dist[x][y - i] = 1 + dist[x][y];
      que.push({x, y - i});
    }
  }
  if (dist[x2][y2] >= inf) {
    cout << -1 << endl;
  } else
    cout << dist[x2][y2] << endl;
  return 0;
}