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

#include <bits/stdc++.h>
#define ll long long
#define inf 1000000000000000008ll
#define xx first
#define yy second
using namespace std;
typedef pair<int, int> pi;
typedef pair<pi, int> pii;
typedef pair<ll, pii> piii;
vector<char> str[1000006];
vector<ll> dis[4][1000006];
char s[1000006];
priority_queue<piii> pq;
int dirx[] = {0, 0, 1, -1};
int diry[] = {1, -1, 0, 0};
int n, m, k;
void dijkstra(int x, int y) {
  dis[0][x][y] = 0;
  dis[1][x][y] = 0;
  dis[2][x][y] = 0;
  dis[3][x][y] = 0;
  pq.push({0, {{x, y}, 0}});
  pq.push({0, {{x, y}, 1}});
  pq.push({0, {{x, y}, 2}});
  pq.push({0, {{x, y}, 3}});
  while (!pq.empty()) {
    piii p = pq.top();
    pq.pop();
    int x = p.yy.xx.xx;
    int y = p.yy.xx.yy;
    int z = p.yy.yy;
    if (-p.xx != dis[z][x][y])
      continue;
    ll c = ((dis[z][x][y] + k - 1) / k) * 1ll * k;
    for (int i = 0; i < 4; i++)
      if (dis[i][x][y] > c)
        dis[i][x][y] = c, pq.push({-c, {{x, y}, i}});
    x = p.yy.xx.xx + dirx[z];
    y = p.yy.xx.yy + diry[z];
    if (x >= 0 && x < n && y >= 0 && y < m && str[x][y] != '@' &&
        dis[z][x][y] > dis[z][p.yy.xx.xx][p.yy.xx.yy] + 1)
      dis[z][x][y] = dis[z][p.yy.xx.xx][p.yy.xx.yy] + 1,
      pq.push({-dis[z][x][y], {{x, y}, z}});
  }
  return;
}
int main() {
  int x1, y1, x2, y2;
  scanf("%d %d %d", &n, &m, &k);
  scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
  x1--, y1--, x2--, y2--;
  for (int i = 0; i < n; i++) {
    scanf("%s", s);
    for (int j = 0; j < m; j++)
      str[i].push_back(s[j]), dis[0][i].push_back(inf),
          dis[1][i].push_back(inf), dis[2][i].push_back(inf),
          dis[3][i].push_back(inf);
  }
  dijkstra(x1, y1);
  ll res = min(min(dis[0][x2][y2], dis[1][x2][y2]),
               min(dis[2][x2][y2], dis[3][x2][y2]));
  if (res == inf)
    puts("-1");
  else {
    res = min(min((dis[0][x2][y2] + k - 1) / k, (dis[1][x2][y2] + k - 1) / k),
              min((dis[2][x2][y2] + k - 1) / k, (dis[3][x2][y2] + k - 1) / k));
    printf("%lld\\n", res);
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define inf 1000000009
#define xx first
#define yy second
using namespace std;
vector<char> str[1000006];
vector<int> dis[1000006];
char s[1000006];
typedef pair<int, int> pi;
queue<pi> q;
int main() {
  int n, m, k, x1, y1, x2, y2;
  scanf("%d %d %d", &n, &m, &k);
  scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
  x1--, y1--, x2--, y2--;
  for (int i = 0; i < n; i++) {
    scanf("%s", s);
    for (int j = 0; j < m; j++)
      str[i].push_back(s[j]), dis[i].push_back(inf);
  }
  dis[x1][y1] = 0;
  q.push({x1, y1});
  while (!q.empty()) {
    pi p = q.front();
    q.pop();
    int cnt = k, x = p.xx - 1, y = p.yy;
    while (cnt-- && x >= 0 && x < n && y >= 0 && y < m && str[x][y] != '@' &&
           dis[x][y] >= dis[p.xx][p.yy] + 1)
      dis[x][y] = dis[p.xx][p.yy] + 1, q.push({x, y}), x--;
    cnt = k, x = p.xx + 1, y = p.yy;
    while (cnt-- && x >= 0 && x < n && y >= 0 && y < m && str[x][y] != '@' &&
           dis[x][y] >= dis[p.xx][p.yy] + 1)
      dis[x][y] = dis[p.xx][p.yy] + 1, q.push({x, y}), x++;
    cnt = k, x = p.xx, y = p.yy - 1;
    while (cnt-- && x >= 0 && x < n && y >= 0 && y < m && str[x][y] != '@' &&
           dis[x][y] >= dis[p.xx][p.yy] + 1)
      dis[x][y] = dis[p.xx][p.yy] + 1, q.push({x, y}), y--;
    cnt = k, x = p.xx, y = p.yy + 1;
    while (cnt-- && x >= 0 && x < n && y >= 0 && y < m && str[x][y] != '@' &&
           dis[x][y] >= dis[p.xx][p.yy] + 1)
      dis[x][y] = dis[p.xx][p.yy] + 1, q.push({x, y}), y++;
  }
  if (dis[x2][y2] == inf)
    puts("-1");
  else
    printf("%d\\n", dis[x2][y2]);
  return 0;
}