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

#include <bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define eb emplace_back
#define mt make_tuple
#define all(x) (x).begin(), (x).end()
#define MOD 1000000007

typedef long long ll;
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef long double ld;

const ll INF = LLONG_MAX;
int h, w, k;
int sx, sy, ex, ey;
const int dx[] = {0, -1, 0, 1};
const int dy[] = {-1, 0, 1, 0};

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  // freopen("input.txt","r",stdin); freopen("output.txt","w",stdout);
  cin >> h >> w >> k >> sx >> sy >> ex >> ey;
  sx--;
  sy--;
  ex--;
  ey--;
  char grid[h][w];
  vll dist(h * w * 4, ll(1e18));
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      cin >> grid[i][j];
  priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>>
      pq; // dist,pos
  auto getid = [&](int x, int y, int dir) { return (x * w + y) * 4 + dir; };
  auto push = [&](int x, int y, int dir, ll d) {
    int id = getid(x, y, dir);
    if (dist[id] <= d)
      return;
    dist[id] = d;
    pq.emplace(d, id);
  };
  auto modk = [&](ll x) { return (x + k - 1) / k * k; };
  for (int i = 0; i < 4; i++)
    push(sx, sy, i, 0LL);
  while (!pq.empty()) {
    ll dis = pq.top().fi;
    int id = pq.top().se;
    pq.pop();
    if (dist[id] != dis)
      continue;
    int dir = id % 4;
    int y = ((id - dir) / 4) % w;
    int x = ((id - dir) / 4 - y) / w;
    for (int i = 0; i < 4; i++)
      push(x, y, i, modk(dis));

    int X = x + dx[dir];
    int Y = y + dy[dir];
    if (X < 0 || X >= h)
      continue;
    if (Y < 0 || Y >= w)
      continue;
    if (grid[X][Y] == '@')
      continue;
    push(X, Y, dir, (dis + 1));
  }
  ll ans = INF;
  for (int i = 0; i < 4; i++) {
    ans = min(ans, dist[getid(ex, ey, i)]);
  }
  if (ans == 1e18)
    ans = -1;
  else
    ans = modk(ans) / k;
  cout << ans << endl;
}

// READ & UNDERSTAND
// ll, int overflow, array bounds, memset(0)
// special cases (n=1?), n+1 (1-index)
// do smth instead of nothing & stay organized
// WRITE STUFF DOWN


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define eb emplace_back
#define mt make_tuple
#define all(x) (x).begin(), (x).end()
#define MOD 1000000007

typedef long long ll;
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef long double ld;
typedef pair<int, ii> iii;

const ll INF = LLONG_MAX;
int h, w, k;
int x1, x2, yy1, y2;
const int dx[] = {0, -1, 0, 1};
const int dy[] = {-1, 0, 1, 0};

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  // freopen("input.txt","r",stdin); freopen("output.txt","w",stdout);
  cin >> h >> w >> k;
  cin >> x1 >> yy1 >> x2 >> y2;
  x1--;
  yy1--;
  x2--;
  y2--;
  char grid[h][w];
  int vis[h][w], ans[h][w];
  memset(vis, -1, sizeof(vis));
  memset(ans, -1, sizeof(ans));
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      cin >> grid[i][j];
  priority_queue<pair<int, iii>, vector<pair<int, iii>>,
                 greater<pair<int, iii>>>
      pq; // dist,pos
  pq.push({0, {0, {x1, yy1}}});
  while (!pq.empty()) {
    int dist = pq.top().fi;
    int cnt = pq.top().se.fi;
    int x = pq.top().se.se.fi;
    int y = pq.top().se.se.se;
    // cout << x << ' ' << y << ' ' << cnt << ' ' << dist << endl;
    pq.pop();
    if (ans[x][y] != -1 && cnt > ans[x][y])
      continue;
    vis[x][y] = dist;
    ans[x][y] = cnt;
    for (int i = 0; i < 4; i++) {
      int X = x;
      int Y = y;
      for (int j = 0; j < k; j++) {
        X += dx[i];
        Y += dy[i];
        if (X < 0 || X >= h)
          break;
        if (Y < 0 || Y >= w)
          break;
        if (grid[X][Y] == '@')
          break;
        if (ans[X][Y] != -1 && cnt + 1 > ans[X][Y])
          continue;
        pq.push({dist + j, {cnt + 1, {X, Y}}});
      }
    }
  }
  cout << ans[x2][y2] << endl;
}

// READ & UNDERSTAND
// ll, int overflow, array bounds, memset(0)
// special cases (n=1?), n+1 (1-index)
// do smth instead of nothing & stay organized
// WRITE STUFF DOWN