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

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <utility>
#include <vector>

using namespace std;

typedef long long LL;
typedef pair<int, int> pii;
typedef pair<int, pii> tmp;
typedef pair<int, tmp> state;

int H, W, K;
int sx, sy, ex, ey;
// U, R, D, L
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
priority_queue<state, vector<state>, greater<state>> pq;

int main() {
  scanf("%d%d%d", &H, &W, &K);
  scanf("%d%d%d%d", &sx, &sy, &ex, &ey);
  sx--;
  sy--;
  ex--;
  ey--;
  vector<vector<char>> mat(H, vector<char>(W, '.'));
  vector<vector<int>> dist(H, vector<int>(W, -1));
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      scanf(" %c", &mat[i][j]);
    }
  }

  dist[sx][sy] = 0;
  pq.push(state(0, tmp(-1, pii(sx, sy))));
  while (pq.size()) {
    int d = pq.top().first;
    int prv = pq.top().second.first;
    int x = pq.top().second.second.first, y = pq.top().second.second.second;
    pq.pop();
    if (d != dist[x][y])
      continue;
    if (x == ex && y == ey) {
      break;
    }
    for (int i = 0; i < 4; i++) {
      if ((i + 2) % 4 == prv || i == prv)
        continue;
      for (int k = 1; k <= max(H, W); k++) {
        int nx = x + dx[i] * k, ny = y + dy[i] * k;
        if (nx < 0 || ny < 0 || nx >= H || ny >= W)
          break;
        if (mat[nx][ny] == '@')
          break;
        int nd = d + (k + K - 1) / K;
        if (dist[nx][ny] == -1 || dist[nx][ny] > nd) {
          dist[nx][ny] = nd;
          pq.push(state(nd, tmp(i, pii(nx, ny))));
        }
      }
    }
  }
  printf("%d\\n", dist[ex][ey]);
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <utility>
#include <vector>

using namespace std;

typedef long long LL;
typedef pair<int, int> pii;
typedef pair<int, pii> tmp;
typedef pair<int, tmp> state;

int H, W, K;
int sx, sy, ex, ey;
// U, R, D, L
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
priority_queue<state, vector<state>, greater<state>> pq;

int main() {
  scanf("%d%d%d", &H, &W, &K);
  scanf("%d%d%d%d", &sx, &sy, &ex, &ey);
  sx--;
  sy--;
  ex--;
  ey--;
  vector<vector<char>> mat(H, vector<char>(W, '.'));
  vector<vector<int>> dist(H, vector<int>(W, -1));
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      scanf(" %c", &mat[i][j]);
    }
  }

  dist[sx][sy] = 0;
  pq.push(state(0, tmp(-1, pii(sx, sy))));
  while (pq.size()) {
    int d = pq.top().first;
    int prv = pq.top().second.first;
    int x = pq.top().second.second.first, y = pq.top().second.second.second;
    pq.pop();
    if (d != dist[x][y])
      continue;
    if (x == ex && y == ey) {
      break;
    }
    for (int i = 0; i < 4; i++) {
      if ((i + 2) % 4 == prv)
        continue;
      for (int k = 1; k <= K; k++) {
        int nx = x + dx[i] * k, ny = y + dy[i] * k;
        if (nx < 0 || ny < 0 || nx >= H || ny >= W)
          break;
        if (mat[nx][ny] == '@')
          break;
        if (dist[nx][ny] == -1 || dist[nx][ny] > d + 1) {
          dist[nx][ny] = d + 1;
          pq.push(state(d + 1, tmp(i, pii(nx, ny))));
        }
      }
    }
  }
  printf("%d\\n", dist[ex][ey]);
}