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

#include <bits/stdc++.h>
typedef long long ll;

using namespace std;

vector<int> dx = {1, 0, -1, 0}, dy = {0, 1, 0, -1};
int min_steps = INT_MAX;
int main() {
  int n, m, k;
  cin >> n >> m >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  vector<string> grid;
  for (int i = 0; i < n; i++) {
    string cur;
    cin >> cur;
    grid.push_back(cur);
  }
  x1--;
  y1--;
  x2--;
  y2--;
  using P = pair<int, pair<int, int>>;
  queue<P> q;
  q.push(P(0, {x1, y1}));
  vector<vector<int>> dist(n, vector<int>(m, INT_MAX));
  dist[x1][x2] = 0;
  while (!q.empty()) {
    P temp = q.front();
    q.pop();
    int cost = temp.first;
    int x = temp.second.first;
    int y = temp.second.second;
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= k; j++) {
        int ny = y + dy[i] * j;
        int nx = x + dx[i] * j;
        // Check borders
        if (nx < 0 || ny < 0 || nx >= n || ny >= m)
          continue;
        int ncost = cost + 1;
        if (grid[nx][ny] == '@')
          break;
        if (dist[nx][ny] > ncost) {
          dist[nx][ny] = ncost;
          q.push(P(ncost, {nx, ny}));
        } else if (dist[nx][ny] == ncost) {
          continue;
        } else {
          break;
        }
      }
    }
  }
  if (dist[x2][y2] == INT_MAX) {
    cout << -1 << endl;
  } else {
    cout << dist[x2][y2] << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
typedef long long ll;

using namespace std;

void pv(vector<int> &v) {
  cout << "{ ";
  for (auto el : v) {
    cout << el << ", ";
  }
  cout << " }" << endl;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  int m, n, K;
  cin >> m >> n >> K;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;

  vector<string> grid;
  for (int i = 0; i < m; i++) {
    string cur;
    cin >> cur;
    grid.push_back(cur);
  }

  pair<int, int> dest = {x2, y2};
  queue<pair<int, int>> pq;

  vector<vector<int>> dist(m, vector<int>(n, INT_MAX));

  pq.push({x1, y1});
  dist[x1][y1] = 0;

  while (!pq.empty()) {
    auto top = pq.front();
    pq.pop();
    int cx = top.first, cy = top.second;

    //        cout << dist[x1][x2] << endl;
    auto curr_dist = dist[cx][cy];
    //        cout << cx << ", " << cy << ". dist " << curr_dist << endl;

    if (top == dest) {
      cout << curr_dist << endl;
      return 0;
    }

    for (int k = 1; k <= K; k++) {
      if ((cx + k >= m) || grid[cx + k][cy] != '.')
        break;
      if (dist[cx + k][cy] <= curr_dist + 1) {
        continue;
      }
      pq.push({cx + k, cy});
      dist[cx + k][cy] = curr_dist + 1;
    }

    for (int k = -1; k >= -1 * K; k--) {
      if ((cx + k < 0) || grid[cx + k][cy] != '.')
        break;
      if (dist[cx + k][cy] <= curr_dist + 1) {
        continue;
      }
      pq.push({cx + k, cy});
      dist[cx + k][cy] = curr_dist + 1;
    }

    for (int k = 1; k <= K; k++) {
      if ((cy + k >= n) || grid[cx][cy + k] != '.')
        break;
      if (dist[cx][cy + k] <= curr_dist + 1) {
        continue;
      }
      pq.push({cx, cy + k});
      dist[cx][cy + k] = curr_dist + 1;
    }

    for (int k = -1; k >= -1 * K; k--) {
      if ((cy + k < 0) || grid[cx][cy + k] != '.')
        break;
      if (dist[cx][cy + k] <= curr_dist + 1) {
        continue;
      }
      pq.push({cx, cy + k});
      dist[cx][cy + k] = curr_dist + 1;
    }
  }

  cout << -1 << endl;
  return 0;
}