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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef long int li;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
typedef pair<int, int> P;
typedef tuple<ll, int, int> T;
#define PI 3.14159265358979

int main() {
  int x1, y11, x2, y2;
  int H, W, K;
  cin >> H >> W >> K >> x1 >> y11 >> x2 >> y2;
  x1--;
  y11--;
  x2--;
  y2--;
  char dp[H][W];
  vector<vector<int>> memo(H, vector<int>(W, 1000000));
  rep(i, H) {
    rep(j, W) { cin >> dp[i][j]; }
  }
  memo[x1][y11] = 0;
  queue<P> qu;
  qu.push(P(x1, y11));
  while (!qu.empty()) {
    auto t = qu.front();
    qu.pop();
    int x = t.first, y = t.second;
    for (int i = 1; i <= K; i++) {
      if (x + i >= H) {
        break;
      }
      if (dp[x + i][y] == '@') {
        break;
      }
      if (memo[x + i][y] > memo[x][y] + 1) {
        memo[x + i][y] = memo[x][y] + 1;
        qu.push(P(x + i, y));
      } else if (memo[x + i][y] < memo[x][y] + 1) {
        break;
      }
    }
    for (int i = 1; i <= K; i++) {
      if (y + i >= W) {
        break;
      }
      if (dp[x][y + i] == '@') {
        break;
      }
      if (memo[x][y + i] > memo[x][y] + 1) {
        memo[x][y + i] = memo[x][y] + 1;
        qu.push(P(x, y + i));
      } else if (memo[x][y + i] < memo[x][y] + 1) {
        break;
      }
    }
    for (int i = 1; i <= K; i++) {
      if (x - i < 0) {
        break;
      }
      if (dp[x - i][y] == '@') {
        break;
      }
      if (memo[x - i][y] > memo[x][y] + 1) {
        memo[x - i][y] = memo[x][y] + 1;
        qu.push(P(x - i, y));
      } else if (memo[x - i][y] < memo[x][y] + 1) {
        break;
      }
    }
    for (int i = 1; i <= K; i++) {
      if (y - i < 0) {
        break;
      }
      if (dp[x][y - i] == '@') {
        break;
      }
      if (memo[x][y - i] > memo[x][y] + 1) {
        memo[x][y - i] = memo[x][y] + 1;
        qu.push(P(x, y - i));
      } else if (memo[x][y - i] < memo[x][y] + 1) {
        break;
      }
    }
  }

  if (memo[x2][y2] != 1000000) {
    cout << memo[x2][y2];
  } else {
    cout << -1;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef long int li;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
typedef pair<int, int> P;
typedef tuple<ll, int, int> T;
#define PI 3.14159265358979

int main() {
  int x1, y11, x2, y2;
  int H, W, K;
  cin >> H >> W >> K >> x1 >> y11 >> x2 >> y2;
  x1--;
  y11--;
  x2--;
  y2--;
  char dp[H][W];
  vector<vector<int>> memo(H, vector<int>(W, 1000000));
  rep(i, H) {
    rep(j, W) { cin >> dp[i][j]; }
  }
  memo[x1][y11] = 0;
  queue<P> qu;
  qu.push(P(x1, y11));
  while (!qu.empty()) {
    auto t = qu.front();
    qu.pop();
    int x = t.first, y = t.second;
    for (int i = 1; i <= K; i++) {
      if (x + i < H) {
        if (dp[x + i][y] == '@') {
          break;
        }
        if (memo[x + i][y] > memo[x][y] + 1) {
          memo[x + i][y] = memo[x][y] + 1;
          qu.push(P(x + i, y));
        }
      }
    }
    for (int i = 1; i <= K; i++) {
      if (y + i < W) {
        if (dp[x][y + i] == '@') {
          break;
        }
        if (memo[x][y + i] > memo[x][y] + 1) {
          memo[x][y + i] = memo[x][y] + 1;
          qu.push(P(x, y + i));
        }
      }
    }
    for (int i = 1; i <= K; i++) {
      if (x - i < 0) {
        break;
      }
      if (dp[x - i][y] == '@') {
        break;
      }
      if (memo[x - i][y] > memo[x][y] + 1) {
        memo[x - i][y] = memo[x][y] + 1;
        qu.push(P(x - i, y));
      }
    }
    for (int i = 1; i <= K; i++) {
      if (y - i < 0) {
        break;
      }
      if (dp[x][y - i] == '@') {
        break;
      }
      if (memo[x][y - i] > memo[x][y] + 1) {
        memo[x][y - i] = memo[x][y] + 1;
        qu.push(P(x, y - i));
      }
    }
  }

  if (memo[x2][y2] != 1000000) {
    cout << memo[x2][y2];
  } else {
    cout << -1;
  }
}