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

#pragma GCC optimize(3)
#include <algorithm>
#include <chrono>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

// const int mod = 1e9+7;
int h, w, k, sx, sy, dx, dy;

int dirx[4] = {-1, 0, 1, 0};
int diry[4] = {0, 1, 0, -1};

inline void quickread() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
}

int he(int x, int y) { return abs(x - dx) / k + abs(y - dy) / k; }

int bfs(vector<vector<char>> &maze, vector<vector<int>> &dist) {
  queue<pii> q;
  q.push({sx, sy});
  while (!q.empty()) {
    auto [x, y] = q.front();
    // cout << "dis: " << x <<" " << y << endl;
    q.pop();
    if (x == dx && y == dy)
      return dist[x][y];
    else {
      for (int i = 0; i < 4; i++) {
        int ddx = dirx[i];
        int ddy = diry[i];
        for (int j = 1; j <= k; j++) {
          int offx = ddx * j;
          int offy = ddy * j;
          if (x + offx < 0 || x + offx >= h || y + offy < 0 || y + offy >= w ||
              maze[x + offx][y + offy] == '@') {
            break;
          }
          if (dist[x + offx][y + offy] > dist[x][y] + 1) {
            dist[x + offx][y + offy] = dist[x][y] + 1;
            q.push({x + offx, y + offy});
          } else if (dist[x + offx][y + offy] <= dist[x][y]) {
            break;
          }
        }
      }
    }
  }
  return -1;
}

int main() {
  quickread();
  cin >> h >> w >> k;
  cin >> sx >> sy >> dx >> dy;
  sx--;
  sy--;
  dx--;
  dy--;
  vector<vector<char>> maze(h, vector<char>(w));
  vector<vector<int>> dist(h, vector<int>(w, 0x3f3f3f3f));
  dist[sx][sy] = 0;
  string line;
  for (int i = 0; i < h; i++) {
    cin >> line;
    for (int j = 0; j < w; j++) {
      maze[i][j] = line[j];
    }
  }
  cout << bfs(maze, dist) << endl;
  return 0;
}


Error: TLE

Fixed submission:

#pragma GCC optimize(3)
#include <algorithm>
#include <chrono>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
// const int mod = 1e9+7;
int h, w, k, sx, sy, dx, dy;

int dirx[4] = {-1, 0, 1, 0};
int diry[4] = {0, 1, 0, -1};

inline void quickread() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
}

int bfs(vector<vector<char>> &maze, vector<vector<int>> &dist) {
  queue<pii> q;
  q.push({sx, sy});
  while (!q.empty()) {
    auto [x, y] = q.front();
    // cout << "dis: " << x <<" " << y << endl;
    q.pop();
    if (x == dx && y == dy)
      return dist[x][y];
    else {
      for (int i = 0; i < 4; i++) {
        int ddx = dirx[i];
        int ddy = diry[i];
        for (int j = 1; j <= k; j++) {
          int offx = ddx * j;
          int offy = ddy * j;
          if (x + offx < 0 || x + offx >= h || y + offy < 0 || y + offy >= w ||
              maze[x + offx][y + offy] == '@') {
            break;
          }
          if (dist[x + offx][y + offy] > dist[x][y] + 1) {
            dist[x + offx][y + offy] = dist[x][y] + 1;
            q.push({x + offx, y + offy});
          }
        }
      }
    }
  }
  return -1;
}

int main() {
  quickread();
  cin >> h >> w >> k;
  cin >> sx >> sy >> dx >> dy;
  sx--;
  sy--;
  dx--;
  dy--;
  vector<vector<char>> maze(h, vector<char>(w));
  vector<vector<int>> dist(h, vector<int>(w, 0x3f3f3f3f));
  dist[sx][sy] = 0;
  string line;
  for (int i = 0; i < h; i++) {
    cin >> line;
    for (int j = 0; j < w; j++) {
      maze[i][j] = line[j];
    }
  }

  cout << bfs(maze, dist) << endl;
  return 0;
}