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

#include <bits/stdc++.h>

#define ff first
#define ss second
#define ad pair<int, pair<int, int>>
#define int long long
using namespace std;

vector<string> a;
int h, wi, k, x1, y11, x2, y2;
bool isValid(int x, int y) {
  if (x < h && x >= 0 && y < wi && y >= 0)
    return true;
  return false;
}
int dx[] = {0, -1, 1, 0};
int dy[] = {1, 0, 0, -1};
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  // #ifndef ONLINE_JUDGE
  // 	freopen("_input.txt", "r", stdin);
  // freopen("_output.txt", "w", stdout);
  // #endif
  cin >> h >> wi >> k;
  cin >> x1 >> y11 >> x2 >> y2;
  // cout<<h<<" "<<wi<<"\\n";
  x1--, y11--, x2--, y2--;
  a.resize(h);
  int w[h][wi];
  for (int i = 0; i < h; i++)
    for (int j = 0; j < wi; j++)
      w[i][j] = LLONG_MAX;
  for (int i = 0; i < h; i++)
    cin >> a[i];
  priority_queue<ad, vector<ad>, greater<ad>> pq;
  pq.push({0, {x1, y11}});
  w[x1][y11] = 0;
  while (!pq.empty()) {
    ad pp = pq.top();
    pq.pop();

    int x = pp.ss.ff, y = pp.ss.ss, dist = pp.ff;
    // cout<<x<<" "<<y<<"\\n";
    if (x == x2 && y == y2) {
      cout << dist << "\\n";
      return 0;
    }
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= k; j++) {
        int new_x = x + dx[i] * j, new_y = y + dy[i] * j;
        bool f = isValid(new_x, new_y);
        // cout<<new_x<<" "<<new_y<<" "<<f<<"\\n";
        if (f && a[new_x][new_y] == '@')
          break;
        if (f) {
          if (w[x][y] + 1 < w[new_x][new_y]) {
            w[new_x][new_y] = w[x][y] + 1;
            pq.push({w[new_x][new_y], {new_x, new_y}});
          } else if (w[x][y] + 1 > w[new_x][new_y])
            break;
        } else
          break;
      }
    }
  }
  cout << "-1\\n";
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define ff first
#define ss second
#define ad pair<int, pair<int, int>>
#define int long long
using namespace std;

vector<string> a;
int h, wi, k, x1, y11, x2, y2;
bool isValid(int x, int y) {
  if (x < h && x >= 0 && y < wi && y >= 0)
    return true;
  return false;
}
int dx[] = {0, -1, 1, 0};
int dy[] = {1, 0, 0, -1};
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  // #ifndef ONLINE_JUDGE
  // 	freopen("_input.txt", "r", stdin);
  // freopen("_output.txt", "w", stdout);
  // #endif
  cin >> h >> wi >> k;
  cin >> x1 >> y11 >> x2 >> y2;
  // cout<<h<<" "<<wi<<"\\n";
  x1--, y11--, x2--, y2--;
  a.resize(h);
  int w[h][wi];
  for (int i = 0; i < h; i++)
    for (int j = 0; j < wi; j++)
      w[i][j] = LLONG_MAX;
  for (int i = 0; i < h; i++)
    cin >> a[i];
  priority_queue<ad, vector<ad>, greater<ad>> pq;
  pq.push({0, {x1, y11}});
  w[x1][y11] = 0;
  while (!pq.empty()) {
    ad pp = pq.top();
    pq.pop();

    int x = pp.ss.ff, y = pp.ss.ss, dist = pp.ff;
    // cout<<x<<" "<<y<<"\\n";
    if (x == x2 && y == y2) {
      cout << dist << "\\n";
      return 0;
    }
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= k; j++) {
        int new_x = x + dx[i] * j, new_y = y + dy[i] * j;
        bool f = isValid(new_x, new_y);
        // cout<<new_x<<" "<<new_y<<" "<<f<<"\\n";
        if (f && a[new_x][new_y] == '@')
          break;
        if (f) {
          if (dist + 1 < w[new_x][new_y]) {
            w[new_x][new_y] = dist + 1;
            pq.push({w[new_x][new_y], {new_x, new_y}});
          }
        } else
          break;
      }
    }
  }
  cout << "-1\\n";
}