Problem p02644 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <queue>
#include <string>
#include <utility>
#include <vector>
using namespace std;

int H, W, K;
pair<int, int> st, ter;
vector<vector<char>> c;
vector<vector<int>> d;
queue<pair<long long, long long>> q;
vector<pair<int, int>> direction = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};

long long bfs() {
  pair<long long, long long> node;
  q.push(st);
  d[st.first][st.second] = 0;
  while (!q.empty()) {
    int size = q.size();
    for (int i = 0; i < size; i++) {
      node = q.front();
      // cout<<node.first<<" "<<node.second<<endl;
      q.pop();
      for (int j = 0; j < 4; j++) {
        for (long long k = 1; k <= K; k++) {
          long long a = node.first + k * direction[j].first;
          long long b = node.second + k * direction[j].second;
          if (a < 0 || a >= H || b < 0 || b >= W || c[a][b] == '@' ||
              d[a][b] <= d[node.first][node.second]) {
            break;
          } else {
            q.push({a, b});
            d[a][b] = d[node.first][node.second] + 1;
            if (a == ter.first && b == ter.second) {
              return d[a][b];
            }
          }
        }
      }
    }
  }
  return -1;
}

int main() {
  cin >> H >> W >> K;
  cin >> st.first >> st.second >> ter.first >> ter.second;
  st.first--;
  st.second--;
  ter.first--;
  ter.second--;

  c.assign(H, vector<char>(W));
  d.assign(H, vector<int>(W, H * W + 1));
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> c[i][j];
    }
  }
  cout << bfs() << endl;

  return 0;
}

Fixed submission:
#include <iostream>
#include <queue>
#include <string>
#include <utility>
#include <vector>
using namespace std;

int H, W, K;
pair<int, int> st, ter;
vector<vector<char>> c;
vector<vector<int>> d;
queue<pair<long long, long long>> q;
vector<pair<int, int>> direction = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};

long long bfs() {
  pair<long long, long long> node;
  q.push(st);
  d[st.first][st.second] = 0;
  while (!q.empty()) {
    int size = q.size();
    for (int i = 0; i < size; i++) {
      node = q.front();
      // cout<<node.first<<" "<<node.second<<endl;
      q.pop();
      for (int j = 0; j < 4; j++) {
        for (long long k = 1; k <= K; k++) {
          long long a = node.first + k * direction[j].first;
          long long b = node.second + k * direction[j].second;
          if (a < 0 || a >= H || b < 0 || b >= W || c[a][b] == '@' ||
              d[a][b] <= d[node.first][node.second]) {
            break;
          } else {
            if (d[a][b] > d[node.first][node.second] + 1) {
              q.push({a, b});
              d[a][b] = d[node.first][node.second] + 1;
              if (a == ter.first && b == ter.second) {
                return d[a][b];
              }
            }
          }
        }
      }
    }
  }
  return -1;
}

int main() {
  cin >> H >> W >> K;
  cin >> st.first >> st.second >> ter.first >> ter.second;
  st.first--;
  st.second--;
  ter.first--;
  ter.second--;

  c.assign(H, vector<char>(W));
  d.assign(H, vector<int>(W, H * W + 1));
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> c[i][j];
    }
  }
  cout << bfs() << endl;

  return 0;
}