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

#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
typedef long double ld;
typedef long long ll;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
typedef complex<ld> compd;
#define quickIO()                                                              \\
  {                                                                            \\
    cin.tie(0);                                                                \\
    cout.sync_with_stdio(false);                                               \\
  }
#define reach(i, a) for (auto i : a)
#define rep(i, n) for (int i = 0; i < ((int)n); i++)
#define REP(i, n) for (int i = 0; i <= ((int)n); i++)
#define srep(i, a, n) for (int i = a; i < ((int)n); i++)
#define SREP(i, a, n) for (int i = a; i <= ((int)n); i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define RREP(i, n) for (int i = n; i >= 0; i--)
#define all(a) (a).begin(), (a).end()
#define mp(a, b) make_pair(a, b)
#define mt make_tuple
#define pb push_back
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  for (T &x : vec)
    is >> x;
  return is;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) {
  os << "[";
  rep(i, vec.size()) os << (i ? ", " : "") << vec[i];
  os << "]";
  return os;
}
template <typename T> ostream &operator<<(ostream &os, set<T> &s) {
  os << "{";
  for (T x : s)
    os << x << ", ";
  os << "}";
  return os;
}
template <typename T> istream &operator>>(istream &is, pair<T, T> &p) {
  is >> p.first >> p.second;
  return is;
}
template <typename T> ostream &operator<<(ostream &os, pair<T, T> &p) {
  os << p.first << " " << p.second;
  return os;
}
template <typename T> bool operator<(vector<T> &a, vector<T> &b) {
  rep(i, a.size()) {
    if (i == b.size())
      return false;
    if (a[i] < b[i])
      return true;
    else if (a[i] > b[i])
      return false;
  }
  return false;
}
template <typename T> bool operator>(vector<T> &a, vector<T> &b) {
  rep(i, a.size()) {
    if (i == b.size())
      return true;
    if (a[i] < b[i])
      return false;
    else if (a[i] > b[i])
      return true;
  }
  return false;
}
int bitcnt(ll x) {
  x = ((x & 0xAAAAAAAAAAAAAAAA) >> 1) + (x & 0x5555555555555555);
  x = ((x & 0xCCCCCCCCCCCCCCCC) >> 2) + (x & 0x3333333333333333);
  x = ((x & 0xF0F0F0F0F0F0F0F0) >> 4) + (x & 0x0F0F0F0F0F0F0F0F);
  x = ((x & 0xFF00FF00FF00FF00) >> 8) + (x & 0x00FF00FF00FF00FF);
  x = ((x & 0xFFFF0000FFFF0000) >> 16) + (x & 0x0000FFFF0000FFFF);
  x = ((x & 0xFFFFFFFF00000000) >> 32) + (x & 0x00000000FFFFFFFF);
  return x;
}
int bitcnt(int x) {
  x = ((x & 0xAAAAAAAA) >> 1) + (x & 0x55555555);
  x = ((x & 0xCCCCCCCC) >> 2) + (x & 0x33333333);
  x = ((x & 0xF0F0F0F0) >> 4) + (x & 0x0F0F0F0F);
  x = ((x & 0xFF00FF00) >> 8) + (x & 0x00FF00FF);
  x = ((x & 0xFFFF0000) >> 16) + (x & 0x0000FFFF);
  return x;
}
ll sqrtll(ll x) {
  ll left = 0, right = x;
  rep(i, 100) {
    ll mid = (left + right) >> 1;
    if (mid * mid <= x)
      left = mid;
    else
      right = mid;
  }
  return left;
}
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
#define debug(x) printf("Case #%d: ", x)
#undef DEBUG
const ld infl = 1e100;
const ll mod = 998244353;
const ld eps = 1e-9;
const ll inf = 1e15;
const int dx[] = {1, 0, -1, 0, 1, -1};
const int dy[] = {0, 1, 0, -1, 1, -1};

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;
  vector<string> s(h);
  cin >> s;
  vector<vector<vint>> dist(h, vector<vint>(w, vint(4, mod)));
  queue<tuple<int, int, int>> q;
  rep(i, 4) {
    dist[x1][y1][i] = 0;
    q.push(mt(x1, y1, i));
  }
  while (!q.empty()) {
    auto it = q.front();
    q.pop();
    int x, y, d;
    tie(x, y, d) = it;
    if (x == x2 && y == y2) {
      cout << dist[x2][y2][d] << endl;
      return 0;
    }
    rep(i, 4) {
      int tox = x + dx[i];
      int toy = y + dy[i];
      rep(j, k) {
        if (tox < 0 || h <= tox)
          break;
        if (toy < 0 || w <= toy)
          break;
        if (s[tox][toy] == '@')
          break;
        if (dist[tox][toy][i] > dist[x][y][d] + 1) {
          dist[tox][toy][i] = dist[x][y][d] + 1;
          q.push(mt(tox, toy, i));
        } else
          break;
        bool ok = false;
        rep(dir, 4) {
          if (dist[tox][toy][dir] == dist[x][y][d])
            ok |= true;
        }
        if (ok)
          break;
        tox += dx[i];
        toy += dy[i];
      }
    }
  }
  cout << -1 << endl;
  return 0;
}

Error: TLE

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
typedef long double ld;
typedef long long ll;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
typedef complex<ld> compd;
#define quickIO()                                                              \\
  {                                                                            \\
    cin.tie(0);                                                                \\
    cout.sync_with_stdio(false);                                               \\
  }
#define reach(i, a) for (auto i : a)
#define rep(i, n) for (int i = 0; i < ((int)n); i++)
#define REP(i, n) for (int i = 0; i <= ((int)n); i++)
#define srep(i, a, n) for (int i = a; i < ((int)n); i++)
#define SREP(i, a, n) for (int i = a; i <= ((int)n); i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define RREP(i, n) for (int i = n; i >= 0; i--)
#define all(a) (a).begin(), (a).end()
#define mp(a, b) make_pair(a, b)
#define mt make_tuple
#define pb push_back
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  for (T &x : vec)
    is >> x;
  return is;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) {
  os << "[";
  rep(i, vec.size()) os << (i ? ", " : "") << vec[i];
  os << "]";
  return os;
}
template <typename T> ostream &operator<<(ostream &os, set<T> &s) {
  os << "{";
  for (T x : s)
    os << x << ", ";
  os << "}";
  return os;
}
template <typename T> istream &operator>>(istream &is, pair<T, T> &p) {
  is >> p.first >> p.second;
  return is;
}
template <typename T> ostream &operator<<(ostream &os, pair<T, T> &p) {
  os << p.first << " " << p.second;
  return os;
}
template <typename T> bool operator<(vector<T> &a, vector<T> &b) {
  rep(i, a.size()) {
    if (i == b.size())
      return false;
    if (a[i] < b[i])
      return true;
    else if (a[i] > b[i])
      return false;
  }
  return false;
}
template <typename T> bool operator>(vector<T> &a, vector<T> &b) {
  rep(i, a.size()) {
    if (i == b.size())
      return true;
    if (a[i] < b[i])
      return false;
    else if (a[i] > b[i])
      return true;
  }
  return false;
}
int bitcnt(ll x) {
  x = ((x & 0xAAAAAAAAAAAAAAAA) >> 1) + (x & 0x5555555555555555);
  x = ((x & 0xCCCCCCCCCCCCCCCC) >> 2) + (x & 0x3333333333333333);
  x = ((x & 0xF0F0F0F0F0F0F0F0) >> 4) + (x & 0x0F0F0F0F0F0F0F0F);
  x = ((x & 0xFF00FF00FF00FF00) >> 8) + (x & 0x00FF00FF00FF00FF);
  x = ((x & 0xFFFF0000FFFF0000) >> 16) + (x & 0x0000FFFF0000FFFF);
  x = ((x & 0xFFFFFFFF00000000) >> 32) + (x & 0x00000000FFFFFFFF);
  return x;
}
int bitcnt(int x) {
  x = ((x & 0xAAAAAAAA) >> 1) + (x & 0x55555555);
  x = ((x & 0xCCCCCCCC) >> 2) + (x & 0x33333333);
  x = ((x & 0xF0F0F0F0) >> 4) + (x & 0x0F0F0F0F);
  x = ((x & 0xFF00FF00) >> 8) + (x & 0x00FF00FF);
  x = ((x & 0xFFFF0000) >> 16) + (x & 0x0000FFFF);
  return x;
}
ll sqrtll(ll x) {
  ll left = 0, right = x;
  rep(i, 100) {
    ll mid = (left + right) >> 1;
    if (mid * mid <= x)
      left = mid;
    else
      right = mid;
  }
  return left;
}
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
#define debug(x) printf("Case #%d: ", x)
#undef DEBUG
const ld infl = 1e100;
const ll mod = 998244353;
const ld eps = 1e-9;
const ll inf = 1e15;
const int dx[] = {1, 0, -1, 0, 1, -1};
const int dy[] = {0, 1, 0, -1, 1, -1};

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;
  vector<string> s(h);
  cin >> s;
  vector<vint> dist(h, vint(w, mod));
  dist[x1][y1] = 0;
  queue<pii> q;
  q.push(mp(x1, y1));
  while (!q.empty()) {
    auto it = q.front();
    q.pop();
    int x = it.first, y = it.second;
    if (x == x2 && y == y2) {
      cout << dist[x2][y2] << endl;
      return 0;
    }
    rep(i, 4) {
      int tox = x + dx[i];
      int toy = y + dy[i];
      rep(j, k) {
        if (tox < 0 || h <= tox)
          break;
        if (toy < 0 || w <= toy)
          break;
        if (s[tox][toy] == '@')
          break;
        if (dist[tox][toy] > dist[x][y] + 1) {
          dist[tox][toy] = dist[x][y] + 1;
          q.push(mp(tox, toy));
        }
        tox += dx[i];
        toy += dy[i];
      }
    }
  }
  cout << -1 << endl;
  return 0;
}