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

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define int long long
#define double long double
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef vector<pii> VP;
typedef vector<string> VS;
typedef priority_queue<int> PQ;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
#define fore(i, a) for (auto &i : a)
#define REP(i, n) for (int i = 0; i < n; i++)
#define eREP(i, n) for (int i = 0; i <= n; i++)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define eFOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define SORT(c) sort((c).begin(), (c).end())
#define rSORT(c) sort((c).rbegin(), (c).rend())
#define LB(x, a) lower_bound((x).begin(), (x).end(), (a))
#define UB(x, a) upper_bound((x).begin(), (x).end(), (a))
#define INF 10000000000000000
#define LLINF 9223372036854775807
#define mod 1000000007
#define eps 1e-12
// priority_queue<int,vector<int>, greater<int> > q2;

int H, W, K;
int sx, sy, gx, gy;
string S[1000010];
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};

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

  cin >> H >> W >> K;
  cin >> sx >> sy >> gx >> gy;
  sx--, sy--, gx--, gy--;
  REP(i, H) cin >> S[i];

  vector<VI> d(H, VI(W, INF));
  d[sx][sy] = 0;
  queue<pii> Q;
  Q.push(pii(sx, sy));

  while (!Q.empty()) {
    int x = Q.front().first;
    int y = Q.front().second;
    Q.pop();
    REP(k, 4) {
      int co = 0;
      int nx = x, ny = y;
      while (++co <= K) {
        nx += dx[k], ny += dy[k];
        if (nx < 0 || nx >= H || ny < 0 || ny >= W)
          break;
        if (S[nx][ny] == '@')
          break;

        if (d[nx][ny] <= d[x][y])
          break;
        if (d[nx][ny] > d[x][y] + 1) {
          d[nx][ny] = d[x][y] + 1;
          Q.push(pii(nx, ny));
        }
      }
    }
  }

  int ans = d[gx][gy];
  if (ans == INF)
    ans = -1;
  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define int long long
#define double long double
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef vector<pii> VP;
typedef vector<string> VS;
typedef priority_queue<int> PQ;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
#define fore(i, a) for (auto &i : a)
#define REP(i, n) for (int i = 0; i < n; i++)
#define eREP(i, n) for (int i = 0; i <= n; i++)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define eFOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define SORT(c) sort((c).begin(), (c).end())
#define rSORT(c) sort((c).rbegin(), (c).rend())
#define LB(x, a) lower_bound((x).begin(), (x).end(), (a))
#define UB(x, a) upper_bound((x).begin(), (x).end(), (a))
#define INF 10000000000000000
#define LLINF 9223372036854775807
#define mod 1000000007
#define eps 1e-12
// priority_queue<int,vector<int>, greater<int> > q2;

int H, W, K;
int sx, sy, gx, gy;
string S[1000010];
map<pii, int> mp;
int d[1000010];

int c(int a, int b) {
  if (a % b == 0)
    return a / b;
  return a / b + 1;
}

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

  cin >> H >> W >> K;
  cin >> sx >> sy >> gx >> gy;
  sx--, sy--, gx--, gy--;
  REP(i, H) cin >> S[i];
  int k = 0;
  REP(i, H) REP(j, W) {
    d[k] = INF;
    mp[pii(i, j)] = k++;
  }

  d[mp[pii(sx, sy)]] = 0;
  priority_queue<pii, vector<pii>, greater<pii>> Q;
  Q.push(pii(0, mp[pii(sx, sy)]));

  while (!Q.empty()) {
    int now = Q.top().second;
    int cost = Q.top().first;
    Q.pop();
    if (d[now] < cost)
      continue;
    int x = now / W;
    int y = now % W;

    for (int nx = x - 1; nx >= 0; nx--) {
      if (S[nx][y] == '@')
        break;
      int tmp = mp[pii(nx, y)];
      if (d[tmp] <= d[now])
        break;
      if (d[tmp] > d[now] + c(abs(nx - x), K)) {
        d[tmp] = d[now] + c(abs(nx - x), K);
        Q.push(pii(d[tmp], tmp));
      }
    }
    for (int nx = x + 1; nx < H; nx++) {
      if (S[nx][y] == '@')
        break;
      int tmp = mp[pii(nx, y)];
      if (d[tmp] <= d[now])
        break;
      if (d[tmp] > d[now] + c(abs(nx - x), K)) {
        d[tmp] = d[now] + c(abs(nx - x), K);
        Q.push(pii(d[tmp], tmp));
      }
    }

    for (int ny = y - 1; ny >= 0; ny--) {
      if (S[x][ny] == '@')
        break;
      int tmp = mp[pii(x, ny)];
      if (d[tmp] <= d[now])
        break;
      if (d[tmp] > d[now] + c(abs(ny - y), K)) {
        d[tmp] = d[now] + c(abs(ny - y), K);
        Q.push(pii(d[tmp], tmp));
      }
    }
    for (int ny = y + 1; ny < W; ny++) {
      if (S[x][ny] == '@')
        break;
      int tmp = mp[pii(x, ny)];
      if (d[tmp] <= d[now])
        break;
      if (d[tmp] > d[now] + c(abs(ny - y), K)) {
        d[tmp] = d[now] + c(abs(ny - y), K);
        Q.push(pii(d[tmp], tmp));
      }
    }
  }
  /*
  REP(i, H) {
          REP(j, W)cout << d[mp[pii(i, j)]] << " ";
          cout << endl;
  }
  */
  int ans = d[mp[pii(gx, gy)]];
  if (ans == INF)
    ans = -1;
  cout << ans << endl;

  return 0;
}