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

#include <bits/stdc++.h>
using namespace std;
#define FAST                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define ll long long int

// power of 2 ...n&(n-1)==0
int h, w, k, x11, y11, x2, y2;
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, 1, -1};

vector<vector<char>> mat;

vector<vector<int>> vis, dis;

void bfs(int x, int y) {
  queue<pair<int, int>> q;
  q.push({x, y});
  vis[x][y] = 1;
  dis[x][y] = 0;
  while (!q.empty()) {
    int X = q.front().first;
    int Y = q.front().second;
    q.pop();
    for (int d = 0; d < 4; d++) {
      for (int len = 1; len <= k; len++) {
        int xn = X + (dx[d]) * len;
        int yn = Y + (dy[d]) * len;
        if (xn < 1 || yn < 1 || xn > h || yn > w)
          break;
        if (dis[X][Y] + 1 > dis[xn][yn] && vis[xn][yn])
          break;
        if (mat[xn][yn] == '@') {
          break;
        }
        if (vis[xn][yn])
          continue;
        vis[xn][yn] = 1;
        dis[xn][yn] = dis[X][Y] + 1;
        q.push({xn, yn});
      }
    }
  }
}
int main() {
  FAST;
  // #ifndef ONLINE_JUDGE
  //	freopen("input.txt","r",stdin);
  //	freopen("output.txt","w",stdout);
  // #endif
  // ll testcase;
  // cin>>testcase;
  // for(ll t=0;t<testcase;t++)
  {
    // use long long
    // donot memeset other than 0 or 1 or -1
    // segmentation fault-> check for uninitialized global variable or out of
    // bounds always check for bottle neck (whether original constraints can be
    // reduced by introducing some condition) dont rush to writing code think
    // through the logic implement dijkstra again ---it is bfs because we vis
    // all the points connected to the current point first from every not
    // visited cell we will update the distances of cell which can be visited
    cin >> h >> w >> k;
    cin >> x11 >> y11 >> x2 >> y2;
    mat = vector<vector<char>>(h + 1, vector<char>(w + 1));
    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        cin >> mat[i][j];
      }
    }

    vis = dis = vector<vector<int>>(h + 1, vector<int>(w + 1, 0));
    bfs(x11, y11);

    if (!vis[x2][y2]) {
      cout << -1 << '\\n';
    } else {
      cout << dis[x2][y2] << '\\n';
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FAST                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define ll long long int

// power of 2 ...n&(n-1)==0
int h, w, k, x11, y11, x2, y2;
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, 1, -1};

vector<vector<char>> mat;

vector<vector<int>> vis, dis;

void bfs(int x, int y) {
  queue<pair<int, int>> q;
  q.push({x, y});
  vis[x][y] = 1;
  dis[x][y] = 0;
  while (!q.empty()) {
    int X = q.front().first;
    int Y = q.front().second;
    q.pop();
    for (int d = 0; d < 4; d++) {
      for (int len = 1; len <= k; len++) {
        int xn = X + (dx[d]) * len;
        int yn = Y + (dy[d]) * len;
        if (xn < 1 || yn < 1 || xn > h || yn > w)
          break;
        if (vis[xn][yn])
          continue;
        if (dis[X][Y] + 1 > dis[xn][yn] && vis[xn][yn])
          break;
        if (mat[xn][yn] == '@') {
          break;
        }
        vis[xn][yn] = 1;
        dis[xn][yn] = dis[X][Y] + 1;
        q.push({xn, yn});
      }
    }
  }
}
int main() {
  FAST;
  // #ifndef ONLINE_JUDGE
  //	freopen("input.txt","r",stdin);
  //	freopen("output.txt","w",stdout);
  // #endif
  // ll testcase;
  // cin>>testcase;
  // for(ll t=0;t<testcase;t++)
  {
    // use long long
    // donot memeset other than 0 or 1 or -1
    // segmentation fault-> check for uninitialized global variable or out of
    // bounds always check for bottle neck (whether original constraints can be
    // reduced by introducing some condition) dont rush to writing code think
    // through the logic implement dijkstra again from every not visited cell we
    // will update the distances of cell which can be visited
    cin >> h >> w >> k;
    cin >> x11 >> y11 >> x2 >> y2;
    mat = vector<vector<char>>(h + 1, vector<char>(w + 1));
    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        cin >> mat[i][j];
      }
    }

    vis = dis = vector<vector<int>>(h + 1, vector<int>(w + 1, 0));
    bfs(x11, y11);

    if (!vis[x2][y2]) {
      cout << -1 << '\\n';
    } else {
      cout << dis[x2][y2] << '\\n';
    }
  }
  return 0;
}