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

/*input
3 3 1
2 1 2 3
.@.
.@.
.@.
*/
/*

**************************
*						 *
* AUTHOR :: knight_coder *
*						 *
**************************

*/
#include <bits/stdc++.h>
#define lld long long int
#define pb push_back
#define ppb pop_back
#define endl '\\n'
#define mii map<lld, lld>
#define msi map<string, lld>
#define mis map<lld, string>
#define mpi map<pair<lld, lld>, lld>
#define pii pair<lld, lld>
#define vi vector<lld>
#define vpi vector<pii>
#define vs vector<string>
#define all(a) (a).begin(), (a).end()
#define F first
#define S second
#define sz(x) (lld) x.size()
#define hell 1000000007
#define rrep(i, m, n) for (lld i = m; i >= n; i--)
#define rep(i, m, n) for (lld i = m; i < n; i++)
#define lbnd lower_bound
#define ubnd upper_bound
#define bs binary_search
#define mp make_pair
#define what_is(x) cerr << #x << " is " << x << endl;
#define pr(x) cout << x << " ";
#define pre(x) cout << x << endl;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// change null_type to get ordered_map
#define N 100005
lld h, w, k;
bool check(lld x, lld y) {
  if (x < 0 or x >= h or y < 0 or y >= w) {
    return false;
  }
  return true;
}
void solve() {
  cin >> h >> w >> k;
  lld x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--, y1--, x2--, y2--;
  char c[h][w];
  rep(i, 0, h) {
    rep(j, 0, w) { cin >> c[i][j]; }
  }
  bool vis[h][w];
  rep(i, 0, h) {
    rep(j, 0, w) { vis[i][j] = false; }
  }
  lld dst[h][w];
  rep(i, 0, h) {
    rep(j, 0, w) { dst[i][j] = 1e18; }
  }
  priority_queue<pair<lld, pii>> pq;
  pq.push({0, {x1, y1}});
  while (!pq.empty()) {
    pair<lld, pii> p = pq.top();
    pq.pop();
    lld X = p.S.F, Y = p.S.S;
    dst[X][Y] = -p.F;
    if (X == x2 && Y == y2) {
      cout << -p.F << endl;
      return;
    }
    rep(i, 1, k + 1) {
      if (check(X + i, Y) && !vis[X + i][Y] && c[X + i][Y] == '.') {
        vis[X + i][Y] = true;
        pq.push({p.F - 1, {X + i, Y}});
      } else if (!check(X + i, Y) or c[X + i][Y] == '@') {
        break;
      }
      if (dst[X + i][Y] < dst[X][Y] + 1) {
        break;
      }
    }
    rep(i, 1, k + 1) {
      if (check(X - i, Y) && !vis[X - i][Y] && c[X - i][Y] == '.') {
        vis[X - i][Y] = true;
        pq.push({p.F - 1, {X - i, Y}});
      } else if (!check(X - i, Y) or c[X - i][Y] == '@') {
        break;
      }
      if (dst[X - i][Y] < dst[X][Y] + 1) {
        break;
      }
    }
    rep(i, 1, k + 1) {
      if (check(X, Y + i) && !vis[X][Y + i] && c[X][Y + i] == '.') {
        vis[X][Y + i] = true;
        pq.push({p.F - 1, {X, Y + i}});
      } else if (!check(X, Y + i) or c[X][Y + i] == '@') {
        break;
      }
      if (dst[X][Y + i] < dst[X][Y] + 1) {
        break;
      }
    }
    rep(i, 1, k + 1) {
      if (check(X, Y - i) && !vis[X][Y - i] && c[X][Y - i] == '.') {
        vis[X][Y - i] = true;
        pq.push({p.F - 1, {X, Y - i}});
      } else if (!check(X, Y - i) or c[X][Y - i] == '@') {
        break;
      }
      if (dst[X][Y - i] < dst[X][Y] + 1) {
        break;
      }
    }
  }
  cout << -1 << endl;
  return;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int tests = 1;
  // cin>>tests;
  while (tests--) {
    solve();
  }
}


Error: TLE

Fixed submission:

/*input
3 3 1
2 1 2 3
.@.
.@.
.@.
*/
/*

**************************
*						 *
* AUTHOR :: knight_coder *
*						 *
**************************

*/
#include <bits/stdc++.h>
#define lld long long int
#define pb push_back
#define ppb pop_back
#define endl '\\n'
#define mii map<lld, lld>
#define msi map<string, lld>
#define mis map<lld, string>
#define mpi map<pair<lld, lld>, lld>
#define pii pair<lld, lld>
#define vi vector<lld>
#define vpi vector<pii>
#define vs vector<string>
#define all(a) (a).begin(), (a).end()
#define F first
#define S second
#define sz(x) (lld) x.size()
#define hell 1000000007
#define rrep(i, m, n) for (lld i = m; i >= n; i--)
#define rep(i, m, n) for (lld i = m; i < n; i++)
#define lbnd lower_bound
#define ubnd upper_bound
#define bs binary_search
#define mp make_pair
#define what_is(x) cerr << #x << " is " << x << endl;
#define pr(x) cout << x << " ";
#define pre(x) cout << x << endl;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// change null_type to get ordered_map
#define N 100005
lld h, w, k;
bool check(lld x, lld y) {
  if (x < 0 or x >= h or y < 0 or y >= w) {
    return false;
  }
  return true;
}
void solve() {
  cin >> h >> w >> k;
  lld x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--, y1--, x2--, y2--;
  char c[h][w];
  rep(i, 0, h) {
    rep(j, 0, w) { cin >> c[i][j]; }
  }
  bool vis[h][w];
  rep(i, 0, h) {
    rep(j, 0, w) { vis[i][j] = false; }
  }
  priority_queue<pair<lld, pii>> pq;
  pq.push({0, {x1, y1}});
  while (!pq.empty()) {
    pair<lld, pii> p = pq.top();
    pq.pop();
    lld X = p.S.F, Y = p.S.S;
    if (X == x2 && Y == y2) {
      cout << -p.F << endl;
      return;
    }
    rep(i, 1, k + 1) {
      if (check(X + i, Y) && !vis[X + i][Y] && c[X + i][Y] == '.') {
        vis[X + i][Y] = true;
        pq.push({p.F - 1, {X + i, Y}});
      } else if (!check(X + i, Y) or c[X + i][Y] == '@') {
        break;
      }
    }
    rep(i, 1, k + 1) {
      if (check(X - i, Y) && !vis[X - i][Y] && c[X - i][Y] == '.') {
        vis[X - i][Y] = true;
        pq.push({p.F - 1, {X - i, Y}});
      } else if (!check(X - i, Y) or c[X - i][Y] == '@') {
        break;
      }
    }
    rep(i, 1, k + 1) {
      if (check(X, Y + i) && !vis[X][Y + i] && c[X][Y + i] == '.') {
        vis[X][Y + i] = true;
        pq.push({p.F - 1, {X, Y + i}});
      } else if (!check(X, Y + i) or c[X][Y + i] == '@') {
        break;
      }
    }
    rep(i, 1, k + 1) {
      if (check(X, Y - i) && !vis[X][Y - i] && c[X][Y - i] == '.') {
        vis[X][Y - i] = true;
        pq.push({p.F - 1, {X, Y - i}});
      } else if (!check(X, Y - i) or c[X][Y - i] == '@') {
        break;
      }
    }
  }
  cout << -1 << endl;
  return;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int tests = 1;
  // cin>>tests;
  while (tests--) {
    solve();
  }
}