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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef pair<ll, ll> pll;
const ll mod = 1e9 + 7;
// const ll mod=998244353;
const ll inf = 1LL << 61;

typedef pair<ll, pll> ppll;

int main() {
  ll h, w, k;
  cin >> h >> w >> k;
  ll sx, sy, gx, gy;
  cin >> sy >> sx >> gy >> gx;
  sx--, sy--, gx--, gy--;
  vector<string> maze(h);
  for (ll i = 0; i < h; i++)
    cin >> maze[i];
  mat d(h, vec(w, -1));
  vector<mat> al(h, mat(w, vec(4, 0)));
  queue<pll> q;
  d[sy][sx] = 0;
  q.push({sy, sx});
  while (q.size()) {
    ll y = q.front().first;
    ll x = q.front().second;
    ll dis = d[y][x];
    q.pop();
    for (ll i = 1; x + i < w && i <= k; i++) {
      ll nx = x + i;
      if (al[y][nx][0] || (d[y][nx] < dis + 1 && d[y][nx] != -1) ||
          maze[y][nx] == '@')
        break;
      d[y][nx] = dis + 1;
      al[y][nx][0] = 1;
      q.push({y, nx});
    }
    for (ll i = 1; x - i >= 0 && i <= k; i++) {
      ll nx = x - i;
      if (al[y][nx][1] || (d[y][nx] < dis + 1 && d[y][nx] != -1) ||
          maze[y][nx] == '@')
        break;
      d[y][nx] = dis + 1;
      al[y][nx][1] = 1;
      q.push({y, nx});
    }
    for (ll i = 1; y + i < h && i <= k; i++) {
      ll ny = y + i;
      if (al[ny][x][2] || (d[ny][x] < dis + 1 && d[ny][x] != -1) ||
          maze[ny][x] == '@')
        break;
      d[ny][x] = dis + 1;
      al[ny][x][2] = 1;
      q.push({ny, x});
    }
    for (ll i = 1; y - i >= 0 && i <= k; i++) {
      ll ny = y - i;
      if (al[ny][x][3] || (d[ny][x] < dis + 1 && d[ny][x] != -1) ||
          maze[ny][x] == '@')
        break;
      d[ny][x] = dis + 1;
      al[ny][x][3] = 1;
      q.push({ny, x});
    }
  }
  cout << d[gy][gx] << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef pair<ll, ll> pll;
const ll mod = 1e9 + 7;
// const ll mod=998244353;
const ll inf = 1LL << 61;

typedef pair<ll, pll> ppll;

int main() {
  ll h, w, k;
  cin >> h >> w >> k;
  ll sx, sy, gx, gy;
  cin >> sy >> sx >> gy >> gx;
  sx--, sy--, gx--, gy--;
  vector<string> maze(h);
  for (ll i = 0; i < h; i++)
    cin >> maze[i];
  mat d(h, vec(w, -1));
  queue<pll> q;
  d[sy][sx] = 0;
  q.push({sy, sx});
  while (q.size()) {
    ll y = q.front().first;
    ll x = q.front().second;
    ll dis = d[y][x];
    q.pop();
    for (ll i = 1; x + i < w && i <= k; i++) {
      ll nx = x + i;
      if ((d[y][nx] < dis + 1 && d[y][nx] != -1) || maze[y][nx] == '@')
        break;
      d[y][nx] = dis + 1;
      q.push({y, nx});
    }
    for (ll i = 1; x - i >= 0 && i <= k; i++) {
      ll nx = x - i;
      if ((d[y][nx] < dis + 1 && d[y][nx] != -1) || maze[y][nx] == '@')
        break;
      d[y][nx] = dis + 1;
      q.push({y, nx});
    }
    for (ll i = 1; y + i < h && i <= k; i++) {
      ll ny = y + i;
      if ((d[ny][x] < dis + 1 && d[ny][x] != -1) || maze[ny][x] == '@')
        break;
      d[ny][x] = dis + 1;
      q.push({ny, x});
    }
    for (ll i = 1; y - i >= 0 && i <= k; i++) {
      ll ny = y - i;
      if ((d[ny][x] < dis + 1 && d[ny][x] != -1) || maze[ny][x] == '@')
        break;
      d[ny][x] = dis + 1;
      q.push({ny, x});
    }
  }
  cout << d[gy][gx] << endl;
}