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

#include <bits/stdc++.h>
#define db(a) cout << a << endl
#define db2(a, b) cout << a << " " << b << endl
#define dbd(a) cout << #a << ": " << a << endl
#define dbd2(a, b) cout << #a << ": " << a << ", " << #b << ": " << b << endl
#define dbp(a) cout << a.first << " " << a.second << endl
#define adb(a)                                                                 \\
  for (auto _i : a)                                                            \\
    cout << _i << " ";                                                         \\
  cout << endl
#define adbp(a)                                                                \\
  for (auto _i : a)                                                            \\
    cout << _i.first << " " << _i.second << endl;
#define fastIO                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define mod 998244353
#define N 1000010
#define M 100010
using namespace std;

vector<vector<char>> mat;
vector<vector<bool>> vis;
vector<vector<int>> dist;
int dx[] = {0, 1, 0, -1}, dy[] = {-1, 0, 1, 0};

bool chk(int x, int y, int n, int m) {
  return (x >= 0 && x < n && y >= 0 && y < m && mat[x][y] != '@');
}

void bfs(int x, int y, int n, int m, int k) {
  queue<pair<int, int>> q;
  q.push({x, y});
  dist[x][y] = 0;
  vis[x][y] = true;

  while (!q.empty()) {
    int xx = q.front().first, yy = q.front().second;
    q.pop();

    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= k; j++) {
        int nx = xx + dx[i] * j, ny = yy + dy[i] * j;

        if (!chk(nx, ny, n, m))
          break;
        if (vis[nx][ny] && dist[nx][ny] < dist[xx][yy] + 1)
          break;
        if (vis[nx][ny])
          continue;

        vis[nx][ny] = true;
        dist[nx][ny] = dist[xx][yy] + 1;
        q.push({nx, ny});
      }
    }
  }
}

int main() {
  fastIO;
  int h, w, k;
  cin >> h >> w >> k;

  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--, y1--, x2--, y2--;

  mat = vector<vector<char>>(h, vector<char>(w));
  vis = vector<vector<bool>>(h, vector<bool>(w, false));
  dist = vector<vector<int>>(h, vector<int>(w, 0));

  int i, j;

  for (i = 0; i < h; i++)
    for (j = 0; j < w; j++)
      cin >> mat[i][j];

  bfs(x1, y1, h, w, k);

  if (!vis[x2][y2])
    cout << "-1" << endl;
  else
    cout << dist[x2][y2] << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define db(a) cout << a << endl
#define db2(a, b) cout << a << " " << b << endl
#define dbd(a) cout << #a << ": " << a << endl
#define dbd2(a, b) cout << #a << ": " << a << ", " << #b << ": " << b << endl
#define dbp(a) cout << a.first << " " << a.second << endl
#define adb(a)                                                                 \\
  for (auto _i : a)                                                            \\
    cout << _i << " ";                                                         \\
  cout << endl
#define adbp(a)                                                                \\
  for (auto _i : a)                                                            \\
    cout << _i.first << " " << _i.second << endl;
#define fastIO                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define mod 998244353
#define N 1000010
#define M 100010
using namespace std;

vector<vector<char>> mat;
vector<vector<bool>> vis;
vector<vector<int>> dist;
int dx[] = {0, 1, 0, -1}, dy[] = {-1, 0, 1, 0};

bool chk(int x, int y, int n, int m) {
  return (x >= 0 && x < n && y >= 0 && y < m && mat[x][y] != '@');
}

void bfs(int x, int y, int n, int m, int k) {
  queue<pair<int, int>> q;
  q.push({x, y});
  dist[x][y] = 0;
  vis[x][y] = true;

  while (!q.empty()) {
    int xx = q.front().first, yy = q.front().second;
    q.pop();

    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= k; j++) {
        int nx = xx + dx[i] * j, ny = yy + dy[i] * j;

        if (!chk(nx, ny, n, m))
          break;
        if (vis[nx][ny] && dist[nx][ny] < dist[x][y] + 1)
          break;
        if (vis[nx][ny])
          continue;

        vis[nx][ny] = true;
        dist[nx][ny] = dist[xx][yy] + 1;
        q.push({nx, ny});
      }
    }
  }
}

int main() {
  fastIO;
  int h, w, k;
  cin >> h >> w >> k;

  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--, y1--, x2--, y2--;

  mat = vector<vector<char>>(h, vector<char>(w));
  vis = vector<vector<bool>>(h, vector<bool>(w));
  dist = vector<vector<int>>(h, vector<int>(w));

  int i, j;

  for (i = 0; i < h; i++)
    for (j = 0; j < w; j++)
      cin >> mat[i][j];

  bfs(x1, y1, h, w, k);

  if (!vis[x2][y2])
    cout << "-1" << endl;
  else
    cout << dist[x2][y2] << endl;
}