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

#include "bits/stdc++.h"
#define pb push_back
#define pii pair<int, int>
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
#define F first
#define S second
#define LINE cout << "--------------------------\\n";
#define IOS ios::sync_with_stdio(false), cin.tie(0);
using namespace std;
int dx[] = {0, 0, -1, 1}, dy[] = {1, -1, 0, 0};
signed main() {
  IOS;
  int n, m, k;
  cin >> n >> m >> k;
  int x, y, x2, y2;
  cin >> x >> y >> x2 >> y2, --x, --y, --x2, --y2;
  vector<string> c(n);
  vector<vector<int>> dis(n, vector<int>(m, 1e9));
  for (int i = 0; i < n; ++i)
    cin >> c[i];
  queue<pii> pq;
  if (c[x][y] != '@')
    pq.push({x, y}), dis[x][y] = 0;
  while (pq.size()) {
    int x = pq.front().F, y = pq.front().S, d = dis[x][y] + 1;
    pq.pop();
    for (int i = 0; i < 4; ++i) {
      int xx = x, yy = y;
      for (int _ = 1; _ <= k; ++_) {
        xx += dx[i], yy += dy[i];
        if (xx < 0 || xx >= n || yy < 0 || yy >= m || c[xx][yy] == '@' ||
            dis[xx][yy] < d)
          break;
        if (dis[xx][yy] > d)
          dis[xx][yy] = d, pq.push({xx, yy});
      }
    }
  }
  cout << (dis[x2][y2] == 1e9 ? -1 : dis[x2][y2]) << '\\n';
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
// #include<cstdio>
// #pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math")

inline int read() {
  int res = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9')
    ch = getchar();
  while (ch >= '0' && ch <= '9')
    res = (res << 3) + (res << 1) + (ch ^ 48), ch = getchar();
  return res;
}

#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define IOS ios::sync_with_stdio(false), cin.tie(0);
#define pii pair<int, int>
#define endl cout << '\\n';
#define LINE cout << "-----------------------\\n";
using namespace std;
const int maxn = 1e6 + 5;
vector<vector<char>> c;
vector<vector<bool>> vis;
struct T {
  int F, S, d;
  T() {}
  T(int _F, int _S, int _d) { F = _F, S = _S, d = _d; }
};
signed main() {
  // int n=read() , m=read() , k=read();
  // int x1=read() , y1=read() , x2=read() , y2=read();
  int n, m, k, x1, x2, y1, y2;
  cin >> n >> m >> k;
  cin >> x1 >> y1 >> x2 >> y2, --x1, --y1, --x2, --y2;
  c.resize(n, vector<char>(m));
  vis.resize(n, vector<bool>(m));
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      cin >> c[i][j];
      if (c[i][j] == '@')
        vis[i][j] = 1;
    }
  }
  queue<T> q;
  if (!vis[x1][y1])
    q.push({x1, y1, 0}), vis[x1][y1] = 1;
  while (q.size()) {
    int x = q.front().F, y = q.front().S, d = q.front().d;
    q.pop();
    if (x == x2 and y == y2)
      return cout << d << '\\n', 0;
    bool t[5];
    t[0] = t[1] = t[2] = t[3] = 1;
    for (int i = 1; i <= k; ++i) {
      if (x - i >= 0 and !vis[x - i][y] and t[0])
        q.push({x - i, y, d + 1}), vis[x - i][y] = 1;
      else if (x - i >= 0 and c[x - i][y] == '@')
        t[0] = 0;
      if (x + i < n and !vis[x + i][y] and t[1])
        q.push({x + i, y, d + 1}), vis[x + i][y] = 1;
      else if (x + i < n and c[x + i][y] == '@')
        t[1] = 0;
      if (y - i >= 0 and !vis[x][y - i] and t[2])
        q.push({x, y - i, d + 1}), vis[x][y - i] = 1;
      else if (y - i >= 0 and c[x][y - i] == '@')
        t[2] = 0;
      if (y + i < m and !vis[x][y + i] and t[3])
        q.push({x, y + i, d + 1}), vis[x][y + i] = 1;
      else if (y + i < m and c[x][y + i] == '@')
        t[3] = 0;
    }
  }
  cout << -1 << '\\n';
}