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

// #include <bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <vector>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> Pii;
typedef pair<int, ll> Pil;
typedef pair<ll, ll> Pll;
typedef pair<ll, int> Pli;

#define fi first
#define se second

const ll MOD = 1e9 + 7;
const ll MOD2 = 998244353;
const ll MOD3 = 1812447359;
const ll INF = 1ll << 62;
const double PI = 2 * asin(1);

void yes() { printf("yes\\n"); }
void no() { printf("no\\n"); }
void Yes() { printf("Yes\\n"); }
void No() { printf("No\\n"); }
void YES() { printf("YES\\n"); }
void NO() { printf("NO\\n"); }

int H, W, K;
int X1, Y1, X2, Y2;
const int di[4] = {-1, 0, 1, 0};
const int dj[4] = {0, 1, 0, -1};

int main() {
  scanf("%d%d%d", &H, &W, &K);
  scanf("%d%d%d%d", &X1, &Y1, &X2, &Y2);
  X1--;
  Y1--;
  X2--;
  Y2--;

  char C[H][W];
  int Dist[H][W];
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> C[i][j];
      Dist[i][j] = 1e9;
    }
  }

  queue<Pii> que;
  que.push({X1, Y1});
  Dist[X1][Y1] = 0;
  while (!que.empty()) {
    Pii Q = que.front();
    que.pop();
    for (int l = 0; l < 4; l++) {
      for (int k = 1; k <= K; k++) {
        int I = Q.fi + di[l] * k;
        int J = Q.se + dj[l] * k;
        if (I < 0 || H <= I || J < 0 || W <= J)
          break;
        if (C[I][J] == '@')
          break;
        if (Dist[Q.fi][Q.se] >= Dist[I][J])
          break;

        if (Dist[I][J] > Dist[Q.fi][Q.se] + 1) {
          Dist[I][J] = Dist[Q.fi][Q.se] + 1;
          que.push({I, J});
        }
      }
    }
  }

  if (Dist[X2][Y2] == 1e9)
    Dist[X2][Y2] = -1;
  cout << Dist[X2][Y2] << endl;

  return 0;
}


Error: TLE

Fixed submission:

// #include <bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <vector>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> Pii;
typedef pair<int, ll> Pil;
typedef pair<ll, ll> Pll;
typedef pair<ll, int> Pli;

#define fi first
#define se second

const ll MOD = 1e9 + 7;
const ll MOD2 = 998244353;
const ll MOD3 = 1812447359;
const ll INF = 1ll << 62;
const double PI = 2 * asin(1);

void yes() { printf("yes\\n"); }
void no() { printf("no\\n"); }
void Yes() { printf("Yes\\n"); }
void No() { printf("No\\n"); }
void YES() { printf("YES\\n"); }
void NO() { printf("NO\\n"); }

int H, W, K;
int X1, Y1, X2, Y2;
const int di[4] = {-1, 0, 1, 0};
const int dj[4] = {0, 1, 0, -1};

int main() {
  scanf("%d%d%d", &H, &W, &K);
  scanf("%d%d%d%d", &X1, &Y1, &X2, &Y2);
  X1--;
  Y1--;
  X2--;
  Y2--;

  char C[H][W];
  int Dist[H][W];
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> C[i][j];
      Dist[i][j] = 1e9;
    }
  }

  queue<Pii> que;
  que.push({X1, Y1});
  Dist[X1][Y1] = 0;
  while (!que.empty()) {
    Pii Q = que.front();
    que.pop();
    for (int l = 0; l < 4; l++) {
      for (int k = 1; k <= K; k++) {
        int I = Q.fi + di[l] * k;
        int J = Q.se + dj[l] * k;
        if (I < 0 || H <= I || J < 0 || W <= J)
          break;
        if (C[I][J] == '@')
          break;

        if (Dist[Q.fi][Q.se] >= Dist[I][J])
          break;
        Dist[I][J] = Dist[Q.fi][Q.se] + 1;
        que.push({I, J});
      }
    }
  }

  if (Dist[X2][Y2] == 1e9)
    Dist[X2][Y2] = -1;
  cout << Dist[X2][Y2] << endl;

  return 0;
}