Problem p02644 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

int main() {
  int H, W, K;
  cin >> H >> W >> K;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  string M[H];
  x1--;
  y1--;
  x2--;
  y2--;
  for (int i = 0; i < H; i++)
    cin >> M[i];
  int dist[H][W];
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      dist[i][j] = INT_MAX / 2;
    }
  }
  queue<pair<int, pair<int, int>>> pq;
  dist[x1][y1] = 0;
  pq.push(make_pair(0, make_pair(x1, y1)));
  while (!pq.empty()) {
    pair<int, pair<int, int>> top = pq.front();
    pq.pop();
    int di = -top.first;
    int curX = top.second.first;
    int curY = top.second.second;
    if (curX == x2 && curY == y2) {
      cout << di << endl;
      return 0;
    }
    if (dist[curX][curY] < di)
      continue;
    int dx[] = {1, 0, -1, 0};
    int dy[] = {0, 1, 0, -1};
    for (int i = 0; i < 4; i++) {
      for (int d = 1; d <= K; d++) {
        int nx = curX + dx[i] * d;
        int ny = curY + dy[i] * d;
        if (nx < 0 || ny < 0 || nx >= H || ny >= W)
          break;
        if (M[nx][ny] == '@')
          break;
        if (dist[nx][ny] > di + 1) {
          dist[nx][ny] = di + 1;
          pq.push(make_pair(-di - 1, make_pair(nx, ny)));
        }
      }
    }
  }
  cout << -1 << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

int main() {
  int H, W, K;
  cin >> H >> W >> K;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  string M[H];
  x1--;
  y1--;
  x2--;
  y2--;
  for (int i = 0; i < H; i++)
    cin >> M[i];
  int dist[H][W];
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      dist[i][j] = INT_MAX / 2;
    }
  }
  queue<pair<int, pair<int, int>>> pq;
  dist[x1][y1] = 0;
  pq.push(make_pair(0, make_pair(x1, y1)));
  while (!pq.empty()) {
    pair<int, pair<int, int>> top = pq.front();
    pq.pop();
    int di = -top.first;
    int curX = top.second.first;
    int curY = top.second.second;
    if (curX == x2 && curY == y2) {
      cout << di << endl;
      return 0;
    }
    if (dist[curX][curY] < di)
      continue;
    int dx[] = {1, 0, -1, 0};
    int dy[] = {0, 1, 0, -1};
    for (int i = 0; i < 4; i++) {
      for (int d = 1; d <= K; d++) {
        int nx = curX + dx[i] * d;
        int ny = curY + dy[i] * d;
        if (nx < 0 || ny < 0 || nx >= H || ny >= W)
          break;
        if (M[nx][ny] == '@')
          break;
        if (dist[nx][ny] == di) {
          break;
        }
        if (dist[nx][ny] > di + 1) {
          dist[nx][ny] = di + 1;
          pq.push(make_pair(-di - 1, make_pair(nx, ny)));
        }
      }
    }
  }
  cout << -1 << endl;
  return 0;
}