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

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <ctime>
#include <deque>
#include <float.h>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;

unsigned euclidean_gcd(unsigned a, unsigned b) {
  if (a < b)
    return euclidean_gcd(b, a);
  unsigned r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}

class UnionFind {
public:
  vector<ll> par;
  vector<ll> siz;
  UnionFind(ll sz_) : par(sz_), siz(sz_, 1LL) {
    for (ll i = 0; i < sz_; ++i)
      par[i] = i;
  }
  void init(ll sz_) {
    par.resize(sz_);
    siz.assign(sz_, 1LL);
    for (ll i = 0; i < sz_; ++i)
      par[i] = i;
  }
  ll root(ll x) {
    while (par[x] != x) {
      x = par[x] = par[par[x]];
    }
    return x;
  }
  bool merge(ll x, ll y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (siz[x] < siz[y])
      swap(x, y);
    siz[x] += siz[y];
    par[y] = x;
    return true;
  }

  bool issame(ll x, ll y) { return root(x) == root(y); }

  ll size(ll x) { return siz[root(x)]; }
};

long long modpow(long long a, long long n, long long mod) {
  long long res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

long long modinv(long long a, long long mod) { return modpow(a, mod - 2, mod); }

int main() {
  ll h, w, k;
  cin >> h >> w >> k;
  ll a, b, cc, dd;
  cin >> a >> b >> cc >> dd;
  vector<vector<char>> z(h + 2, vector<char>(w + 2, '@'));
  for (int i = 1; i < h + 1; i++) {
    string x;
    cin >> x;
    for (int j = 1; j < w + 1; j++) {
      z[i][j] = x[j - 1];
    }
  }
  queue<pair<ll, ll>> q;
  vector<vector<ll>> ans(h + 2, vector<ll>(w + 2, 123456789));
  q.push(make_pair(a, b));
  ans[a][b] = 0;
  while (!(q.empty())) {
    ll c = q.front().first;
    ll d = q.front().second;
    for (int i = 1; i < k + 1; i++) {
      if (z[c + i][d] == '@' || ans[c + i][d] <= ans[c][d])
        break;
      if (ans[c + i][d] == 123456789)
        q.push(make_pair(c + i, d));
      ans[c + i][d] = ans[c][d] + 1;
    }
    for (int i = 1; i < k + 1; i++) {
      if (z[c - i][d] == '@' || ans[c - i][d] <= ans[c][d])
        break;
      if (ans[c - i][d] == 123456789)
        q.push(make_pair(c - i, d));
      ans[c - i][d] = ans[c][d] + 1;
    }
    for (int i = 1; i < k + 1; i++) {
      if (z[c][d + i] == '@' || ans[c][d + i] <= ans[c][d])
        break;
      if (ans[c][d + i] == 123456789)
        q.push(make_pair(c, d + i));
      ans[c][d + i] = ans[c][d] + 1;
    }
    for (int i = 1; i < k + 1; i++) {
      if (z[c][d - i] == '@' || ans[c][d - i] <= ans[c][d])
        break;
      if (ans[c][d - i] == 123456789)
        q.push(make_pair(c, d - i));
      ans[c][d - i] = ans[c][d] + 1;
    }
    q.pop();
  }
  if (ans[cc][dd] == 123456789)
    ans[cc][dd] = -1;
  cout << ans[cc][dd] << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <ctime>
#include <deque>
#include <float.h>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;

unsigned euclidean_gcd(unsigned a, unsigned b) {
  if (a < b)
    return euclidean_gcd(b, a);
  unsigned r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}

class UnionFind {
public:
  vector<ll> par;
  vector<ll> siz;
  UnionFind(ll sz_) : par(sz_), siz(sz_, 1LL) {
    for (ll i = 0; i < sz_; ++i)
      par[i] = i;
  }
  void init(ll sz_) {
    par.resize(sz_);
    siz.assign(sz_, 1LL);
    for (ll i = 0; i < sz_; ++i)
      par[i] = i;
  }
  ll root(ll x) {
    while (par[x] != x) {
      x = par[x] = par[par[x]];
    }
    return x;
  }
  bool merge(ll x, ll y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (siz[x] < siz[y])
      swap(x, y);
    siz[x] += siz[y];
    par[y] = x;
    return true;
  }

  bool issame(ll x, ll y) { return root(x) == root(y); }

  ll size(ll x) { return siz[root(x)]; }
};

long long modpow(long long a, long long n, long long mod) {
  long long res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

long long modinv(long long a, long long mod) { return modpow(a, mod - 2, mod); }

int main() {
  ll h, w, k;
  cin >> h >> w >> k;
  ll a, b, cc, dd;
  cin >> a >> b >> cc >> dd;
  vector<vector<char>> z(h + 2, vector<char>(w + 2, '@'));
  for (int i = 1; i < h + 1; i++) {
    string x;
    cin >> x;
    for (int j = 1; j < w + 1; j++) {
      z[i][j] = x[j - 1];
    }
  }
  queue<pair<ll, ll>> q;
  vector<vector<ll>> ans(h + 2, vector<ll>(w + 2, 123456789));
  q.push(make_pair(a, b));
  ans[a][b] = 0;
  while (!(q.empty())) {
    ll c = q.front().first;
    ll d = q.front().second;
    for (int i = 1; i < k + 1; i++) {
      if (z[c + i][d] == '@' || ans[c + i][d] <= ans[c][d])
        break;
      ans[c + i][d] = ans[c][d] + 1;
      q.push(make_pair(c + i, d));
    }
    for (int i = 1; i < k + 1; i++) {
      if (z[c - i][d] == '@' || ans[c - i][d] <= ans[c][d])
        break;
      ans[c - i][d] = ans[c][d] + 1;
      q.push(make_pair(c - i, d));
    }
    for (int i = 1; i < k + 1; i++) {
      if (z[c][d + i] == '@' || ans[c][d + i] <= ans[c][d])
        break;
      ans[c][d + i] = ans[c][d] + 1;
      q.push(make_pair(c, d + i));
    }
    for (int i = 1; i < k + 1; i++) {
      if (z[c][d - i] == '@' || ans[c][d - i] <= ans[c][d])
        break;
      ans[c][d - i] = ans[c][d] + 1;
      q.push(make_pair(c, d - i));
    }
    q.pop();
  }
  if (ans[cc][dd] == 123456789)
    ans[cc][dd] = -1;
  cout << ans[cc][dd] << endl;
}