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

#include <bits/stdc++.h>
#include <stdlib.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef vector<double> Vec;
typedef vector<Vec> Mat;
typedef pair<ll, ll> P;
typedef pair<double, ll> Pd;
typedef priority_queue<P, vector<P>, greater<P>> P_queue;
typedef priority_queue<Pd, vector<Pd>, greater<Pd>> Pd_queue;

const ll MOD = 998244353;
const ll mod = 1000000007;
const ll INF = 1e15;
const double DEL = 1e-6;
vec dx = {1, 0, -1, 0};
vec dy = {0, 1, 0, -1};

#define REP(i, a, b) for (int i = (int)a; i < (int)b; i++)
#define rep(i, n) REP(i, 0, n)
#define pb push_back
#define mp make_pair
#define ALL(a) a.begin(), a.end()
#define SORT(a) sort(ALL(a))
#define U_ERASE(V) V.erase(unique(ALL(V)), V.end());
#define ADD(a, b) a = (a + b) % mod

int main() {
  ll H, W, K;
  cin >> H >> W >> K;
  ll s[2], g[2];
  rep(i, 2) {
    cin >> s[i];
    s[i]--;
  }
  rep(i, 2) {
    cin >> g[i];
    g[i]--;
  }
  mat ans(H, vec(W, -INF));
  vector<vector<bool>> arrived(H, vector<bool>(W, false));
  queue<P> Q;
  const ll MAXN = 1000010;
  rep(i, H) {
    string S;
    cin >> S;
    rep(j, W) if (S[j] == '.') ans[i][j] = INF;
  }
  Q.push(mp(0, s[0] * MAXN + s[1]));
  ans[s[0]][s[1]] = 0;
  // cout<<"arrived"<<endl;
  while (!Q.empty()) {
    ll h = Q.front().second / MAXN, w = Q.front().second % MAXN;
    ll A = Q.front().first;
    Q.pop();
    if (h == g[0] && w == g[1])
      break;
    if (!arrived[h][w]) {
      arrived[h][w] = true;
      ll hh = h + 1;
      while (hh < H) {
        if (abs(hh - h) > K)
          break;
        if (ans[hh][w] < 0)
          break;
        if (ans[hh][w] == A)
          break;
        if (ans[hh][w] > A + 1) {
          ans[hh][w] = A + 1;
          Q.push(mp(A + 1, hh * MAXN + w));
        }
        hh++;
      }
      hh = h - 1;
      while (hh >= 0) {
        if (abs(hh - h) > K)
          break;
        if (ans[hh][w] < 0)
          break;
        if (ans[hh][w] == A)
          break;
        if (ans[hh][w] > A + 1) {
          ans[hh][w] = A + 1;
          Q.push(mp(A + 1, hh * MAXN + w));
        }
        hh--;
      }
      ll ww = w + 1;
      while (ww < W) {
        if (abs(ww - w) > K)
          break;
        if (ans[h][ww] < 0)
          break;
        if (ans[h][ww] == A)
          break;
        if (ans[h][ww] > A + 1) {
          ans[h][ww] = A + 1;
          Q.push(mp(A + 1, h * MAXN + ww));
        }
        ww++;
      }
      ww = w - 1;
      while (ww >= 0) {
        if (abs(ww - w) > K)
          break;
        if (ans[h][ww] == A)
          break;
        if (ans[h][ww] < 0)
          break;
        if (ans[h][ww] > A + 1) {
          ans[h][ww] = A + 1;
          Q.push(mp(A + 1, h * MAXN + ww));
        }
        ww--;
      }
    }
  }
  if (ans[g[0]][g[1]] < INF)
    cout << ans[g[0]][g[1]] << endl;
  else
    cout << -1 << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <stdlib.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef vector<double> Vec;
typedef vector<Vec> Mat;
typedef pair<ll, ll> P;
typedef pair<double, ll> Pd;
typedef priority_queue<P, vector<P>, greater<P>> P_queue;
typedef priority_queue<Pd, vector<Pd>, greater<Pd>> Pd_queue;

const ll MOD = 998244353;
const ll mod = 1000000007;
const ll INF = 1e15;
const double DEL = 1e-6;
vec dx = {1, 0, -1, 0};
vec dy = {0, 1, 0, -1};

#define REP(i, a, b) for (int i = (int)a; i < (int)b; i++)
#define rep(i, n) REP(i, 0, n)
#define pb push_back
#define mp make_pair
#define ALL(a) a.begin(), a.end()
#define SORT(a) sort(ALL(a))
#define U_ERASE(V) V.erase(unique(ALL(V)), V.end());
#define ADD(a, b) a = (a + b) % mod

int main() {
  ll H, W, K;
  cin >> H >> W >> K;
  ll s[2], g[2];
  rep(i, 2) {
    cin >> s[i];
    s[i]--;
  }
  rep(i, 2) {
    cin >> g[i];
    g[i]--;
  }
  mat ans(H, vec(W, -INF));
  vector<vector<bool>> arrived(H, vector<bool>(W, false));
  queue<P> Q;
  const ll MAXN = 1000010;
  rep(i, H) {
    string S;
    cin >> S;
    rep(j, W) if (S[j] == '.') ans[i][j] = INF;
  }
  Q.push(mp(0, s[0] * MAXN + s[1]));
  ans[s[0]][s[1]] = 0;
  // cout<<"arrived"<<endl;
  while (!Q.empty()) {
    ll h = Q.front().second / MAXN, w = Q.front().second % MAXN;
    ll A = Q.front().first;
    Q.pop();
    if (h == g[0] && w == g[1])
      break;
    if (!arrived[h][w]) {
      arrived[h][w] = true;
      ll hh = h + 1;
      while (hh < H) {
        if (abs(hh - h) > K)
          break;
        if (ans[hh][w] < 0)
          break;
        if (ans[hh][w] > A + 1) {
          ans[hh][w] = A + 1;
          Q.push(mp(A + 1, hh * MAXN + w));
        }
        hh++;
      }
      hh = h - 1;
      while (hh >= 0) {
        if (abs(hh - h) > K)
          break;
        if (ans[hh][w] < 0)
          break;
        if (ans[hh][w] > A + 1) {
          ans[hh][w] = A + 1;
          Q.push(mp(A + 1, hh * MAXN + w));
        }
        hh--;
      }
      ll ww = w + 1;
      while (ww < W) {
        if (abs(ww - w) > K)
          break;
        if (ans[h][ww] < 0)
          break;
        if (ans[h][ww] > A + 1) {
          ans[h][ww] = A + 1;
          Q.push(mp(A + 1, h * MAXN + ww));
        }
        ww++;
      }
      ww = w - 1;
      while (ww >= 0) {
        if (abs(ww - w) > K)
          break;
        if (ans[h][ww] < 0)
          break;
        if (ans[h][ww] > A + 1) {
          ans[h][ww] = A + 1;
          Q.push(mp(A + 1, h * MAXN + ww));
        }
        ww--;
      }
    }
  }
  if (ans[g[0]][g[1]] < INF)
    cout << ans[g[0]][g[1]] << endl;
  else
    cout << -1 << endl;
}