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

#include <bits/stdc++.h>
using namespace std;

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;
}

using ll = long long;

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int INF = 1 << 30;
const ll mod = 1000000007LL;

int main() {
  int H, W, K, A, B, C, D;
  cin >> H >> W >> K >> A >> B >> C >> D;
  A--;
  B--;
  C--;
  D--;
  vector<string> g(H);
  for (int i = 0; i < H; i++)
    cin >> g[i];
  vector<vector<int>> d(H, vector<int>(W, -1));
  queue<tuple<int, int, int>> bfs;
  bfs.push(make_tuple(A, B, -1));
  d[A][B] = 0;
  while (!bfs.empty() && d[C][D] == -1) {
    tuple<int, int, int> p = bfs.front();
    bfs.pop();
    if (get<2>(p) != 1) {
      for (int i = 1; i <= K; i++) {
        int nx = get<0>(p) + i;
        int ny = get<1>(p);
        if (nx >= H)
          break;
        if (g[nx][ny] == '@')
          break;
        if (d[nx][ny] != -1)
          continue;
        bfs.push(make_tuple(nx, ny, 0));
        d[nx][ny] = d[get<0>(p)][get<1>(p)] + 1;
      }
    }
    if (get<2>(p) != 0) {
      for (int i = 1; i <= K; i++) {
        int nx = get<0>(p) - i;
        int ny = get<1>(p);
        if (nx < 0)
          break;
        if (g[nx][ny] == '@')
          break;
        if (d[nx][ny] != -1)
          continue;
        bfs.push(make_tuple(nx, ny, 1));
        d[nx][ny] = d[get<0>(p)][get<1>(p)] + 1;
      }
    }
    if (get<2>(p) != 3) {
      for (int i = 1; i <= K; i++) {
        int nx = get<0>(p);
        int ny = get<1>(p) + i;
        if (ny >= W)
          break;
        if (g[nx][ny] == '@')
          break;
        if (d[nx][ny] != -1)
          continue;
        bfs.push(make_tuple(nx, ny, 2));
        d[nx][ny] = d[get<0>(p)][get<1>(p)] + 1;
      }
    }
    if (get<2>(p) != 2) {
      for (int i = 1; i <= K; i++) {
        int nx = get<0>(p);
        int ny = get<1>(p) - i;
        if (ny < 0)
          break;
        if (g[nx][ny] == '@')
          break;
        if (d[nx][ny] != -1)
          continue;
        bfs.push(make_tuple(nx, ny, 3));
        d[nx][ny] = d[get<0>(p)][get<1>(p)] + 1;
      }
    }
  }
  cout << d[C][D] << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

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;
}

using ll = long long;

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int INF = 1 << 30;
const ll mod = 1000000007LL;

int main() {
  int H, W, K, A, B, C, D;
  cin >> H >> W >> K >> A >> B >> C >> D;
  A--;
  B--;
  C--;
  D--;
  vector<string> g(H);
  for (int i = 0; i < H; i++)
    cin >> g[i];
  vector<vector<int>> d(H, vector<int>(W, -1));
  queue<tuple<int, int, int>> bfs;
  bfs.push(make_tuple(A, B, -1));
  d[A][B] = 0;
  while (!bfs.empty()) {
    tuple<int, int, int> p = bfs.front();
    bfs.pop();
    if (get<2>(p) != 1) {
      for (int i = 1; i <= K; i++) {
        int nx = get<0>(p) + i;
        int ny = get<1>(p);
        if (nx >= H)
          break;
        if (g[nx][ny] == '@')
          break;
        if (d[nx][ny] != -1)
          continue;
        bfs.push(make_tuple(nx, ny, 0));
        d[nx][ny] = d[get<0>(p)][get<1>(p)] + 1;
      }
    }
    if (get<2>(p) != 0) {
      for (int i = 1; i <= K; i++) {
        int nx = get<0>(p) - i;
        int ny = get<1>(p);
        if (nx < 0)
          break;
        if (g[nx][ny] == '@')
          break;
        if (d[nx][ny] != -1)
          continue;
        bfs.push(make_tuple(nx, ny, 1));
        d[nx][ny] = d[get<0>(p)][get<1>(p)] + 1;
      }
    }
    if (get<2>(p) != 3) {
      for (int i = 1; i <= K; i++) {
        int nx = get<0>(p);
        int ny = get<1>(p) + i;
        if (ny >= W)
          break;
        if (g[nx][ny] == '@')
          break;
        if (d[nx][ny] != -1)
          continue;
        bfs.push(make_tuple(nx, ny, 2));
        d[nx][ny] = d[get<0>(p)][get<1>(p)] + 1;
      }
    }
    if (get<2>(p) != 2) {
      for (int i = 1; i <= K; i++) {
        int nx = get<0>(p);
        int ny = get<1>(p) - i;
        if (ny < 0)
          break;
        if (g[nx][ny] == '@')
          break;
        if (d[nx][ny] != -1)
          continue;
        bfs.push(make_tuple(nx, ny, 3));
        d[nx][ny] = d[get<0>(p)][get<1>(p)] + 1;
      }
    }
  }
  cout << d[C][D] << endl;
}