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

#include <bits/stdc++.h>
using namespace std;
#define pb push_back

#define mp make_pair
#define ll long long
#define fi first
#define se second
#define vi vector<int>
#define pii pair<int, int>
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
multiset<int> s[200005];
set<pair<int, int>> g;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m, k, x1, x2, y1, y2;
  cin >> n >> m >> k;
  vector<vector<bool>> v(n + 1, vector<bool>(m + 1)),
      vis(n + 1, vector<bool>(m + 1));
  vector<vector<int>> dis(n + 1, vector<int>(m + 1));
  cin >> x1 >> y1 >> x2 >> y2;
  char c;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      cin >> c;
      v[i][j] = (c == '.');
      if (c != '.')
        vis[i][j] = 1;
    }
  if (!v[x1][y1] || !v[x2][y2]) {
    cout << -1;
    exit(0);
  }
  queue<pair<int, int>> q;
  q.push({x1, y1});
  vis[x1][y1] = 1;
  while (!q.empty()) {
    int x = q.front().fi;
    int y = q.front().se;
    q.pop();
    if (x == x2 && y == y2) {
      cout << dis[x][y];
      exit(0);
    }
    for (int i = 0; i < 4; i++) {
      for (int f = 1; f <= k; f++) {
        int ax = dx[i] * f + x;
        int ay = dy[i] * f + y;
        if (ax <= 0 || ay <= 0 || ax > n || ay > m)
          break;
        if (vis[ax][ay] && dis[ax][ay] <= dis[x][y])
          break;
        if (vis[ax][ay] && dis[ax][ay] == dis[x][y] + 1)
          continue;
        vis[ax][ay] = 1;
        dis[ax][ay] = dis[x][y] + 1;
        q.push({ax, ay});
      }
    }
  }
  cout << -1;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define pb push_back

#define mp make_pair
#define ll long long
#define fi first
#define se second
#define vi vector<int>
#define pii pair<int, int>
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
multiset<int> s[200005];
set<pair<int, int>> g;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m, k, x1, x2, y1, y2;
  cin >> n >> m >> k;
  vector<vector<bool>> v(n + 1, vector<bool>(m + 1)),
      vis(n + 1, vector<bool>(m + 1));
  vector<vector<int>> dis(n + 1, vector<int>(m + 1));
  cin >> x1 >> y1 >> x2 >> y2;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      char c;
      cin >> c;
      v[i][j] = (c == '.');
      if (c != '.')
        vis[i][j] = 1;
    }
  if (!v[x1][y1] || !v[x2][y2]) {
    cout << -1;
    exit(0);
  }
  queue<pair<int, int>> q;
  q.push({x1, y1});
  vis[x1][y1] = 1;
  while (!q.empty()) {
    int x = q.front().fi;
    int y = q.front().se;
    q.pop();
    if (x == x2 && y == y2) {
      cout << dis[x][y];
      exit(0);
    }
    for (int i = 0; i < 4; i++) {
      for (int f = 1; f <= k; f++) {
        int ax = dx[i] * f + x;
        int ay = dy[i] * f + y;
        if (ax <= 0 || ay <= 0 || ax > n || ay > m)
          break;
        if (vis[ax][ay] && dis[ax][ay] <= dis[x][y])
          break;
        vis[ax][ay] = 1;
        dis[ax][ay] = dis[x][y] + 1;
        q.push({ax, ay});
      }
    }
  }
  cout << -1;
}