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

#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define fs first
#define sc second
#define task "test"
using namespace std;
const ll N = 1e6 + 9;
const ll inf = 1e9 + 7;
typedef pair<ll, ll> LL;
char a[N];
ll h, w, k, n, d[N], x1, x2, Y1, Y2;
ll scr, dest;
ll trans(ll x, ll y) { return (x - 1) * w + y; }
bool chk(ll n) { return (n > 0 && n <= h * w && a[n] != '@'); }
void BFS(ll scr) {
  queue<ll> q;
  fill(d, d + N + 1, inf);
  d[scr] = 0;
  q.push(scr);
  while (!q.empty()) {
    ll t = q.front();
    q.pop();
    ll p = ceil((ld)t / w);
    for (ll i = t + 1; i <= t + k; i++) {
      if (chk(i) && i <= p * w) {
        if (d[i] > d[t] + 1) {
          d[i] = d[t] + 1;
          q.push(i);
        } else if (d[i] == d[t] + 1)
          continue;
        else
          break;
      } else
        break;
    }
    for (ll i = t - 1; i >= t - k; i--) {
      if (chk(i) && i > (p - 1) * w) {
        if (d[i] > d[t] + 1) {
          d[i] = d[t] + 1;
          q.push(i);
        } else if (d[i] == d[t] + 1)
          continue;
        else
          break;
      } else
        break;
    }
    for (ll i = 1; i <= k; i++) {
      if (chk(t + i * w)) {
        if (d[t + i * w] > d[t] + 1) {
          d[t + i * w] = d[t] + 1;
          q.push(t + i * w);
        } else if (d[t + i * w] == d[t] + 1)
          continue;
        else
          break;
      } else
        break;
    }
    for (ll i = 1; i <= k; i++) {
      if (chk(t - i * w)) {
        if (d[t - i * w] > d[t] + 1) {
          d[t - i * w] = d[t] + 1;
          q.push(t - i * w);
        } else if (d[t - i * w] == d[t] + 1)
          continue;
        else
          break;
      } else
        break;
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  if (fopen(task ".inp", "r")) {
    freopen(task ".inp", "r", stdin);
    // freopen(task".out", "w", stdout);
  }
  cin >> h >> w >> k >> x1 >> Y1 >> x2 >> Y2;
  for (ll i = 1; i <= h * w; i++)
    cin >> a[i];
  scr = trans(x1, Y1);
  dest = trans(x2, Y2);
  BFS(scr);
  if (d[dest] != inf)
    cout << d[dest];
  else
    cout << -1;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define fs first
#define sc second
#define task "tst"
using namespace std;
const ll N = 1e6 + 1;
const ll inf = 1e9 + 7;
typedef pair<ll, ll> LL;
char a[N];
ll h, w, k, n, d[N], x1, x2, Y1, Y2;
ll scr, dest;
ll trans(ll x, ll y) { return (x - 1) * w + y; }
bool chk(ll n) { return (n > 0 && n <= h * w && a[n] != '@'); }
void BFS(ll scr) {
  queue<ll> q;
  fill(d, d + N + 1, inf);
  d[scr] = 0;
  q.push(scr);
  while (!q.empty()) {
    ll t = q.front();
    q.pop();
    ll p = ceil((ld)t / w);
    for (ll i = t + 1; i <= t + k; i++) {
      if (chk(i) && i <= p * w) {
        if (d[i] > d[t] + 1) {
          d[i] = d[t] + 1;
          q.push(i);
        }
      } else
        break;
    }
    for (ll i = t - 1; i >= t - k; i--) {
      if (chk(i) && i > (p - 1) * w) {
        if (d[i] > d[t] + 1) {
          d[i] = d[t] + 1;
          q.push(i);
        }
      } else
        break;
    }
    for (ll i = 1; i <= k; i++) {
      if (chk(t + i * w)) {
        if (d[t + i * w] > d[t] + 1) {
          d[t + i * w] = d[t] + 1;
          q.push(t + i * w);
        }
      } else
        break;
    }
    for (ll i = 1; i <= k; i++) {
      if (chk(t - i * w)) {
        if (d[t - i * w] > d[t] + 1) {
          d[t - i * w] = d[t] + 1;
          q.push(t - i * w);
        }
      } else
        break;
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  if (fopen(task ".inp", "r")) {
    freopen(task ".inp", "r", stdin);
    // freopen(task".out", "w", stdout);
  }
  cin >> h >> w >> k;
  cin >> x1 >> Y1 >> x2 >> Y2;
  for (ll i = 1; i <= h * w; i++)
    cin >> a[i];
  scr = trans(x1, Y1);
  dest = trans(x2, Y2);

  BFS(scr);
  // cout<<trans(3,4);
  if (d[dest] != inf)
    cout << d[dest];
  else
    cout << -1;
}