Problem p02644 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < n; i++)
#define Rep(i, a, b) for (ll i = a; i < b; i++)
#define pb emplace_back
#define allv v.begin(), v.end()
using namespace std;
typedef long long int ll;
typedef unsigned long long ull;
typedef vector<ll> vll;
#define FASTio                                                                 \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define deb(x) cout << (#x) << " " << x << endl
ll mod = 1e9 + 7;
template <typename t> ll Max(ll a, ll b) { return (a > b) ? a : b; }
ll Min(ll a, ll b) { return (a > b) ? b : a; }
ll power(ll a, ll b) {
  ll p = 1;
  a = a % mod;
  b = b % mod;
  while (b) {
    if (b & 1) {
      p = ((p % mod) * (a % mod)) % mod;
    }
    a = ((a % mod) * (a % mod)) % mod;
    b = (b / 2);
  }
  return p % mod;
}
ll h, w, k;
ll ma = 1e15;
bool isValid(ll i, ll j) { return (i >= 1 && i <= h && j >= 1 && j <= w); }
ll direc[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int main() {
  int t;
  t = 1;
  //  cin>>t;
  while (t--) {
    //  ll h,w,k;
    cin >> h >> w >> k;
    ll x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    char mat[h + 1][w + 1];
    Rep(i, 1, h + 1) { Rep(j, 1, w + 1) cin >> mat[i][j]; }
    ll dist[h + 1][w + 1];
    bool visited[h + 1][w + 1];
    // memset(dist,INT_MAX,sizeof(dist));
    for (ll i = 1; i <= h; i++) {
      for (ll j = 1; j <= w; j++) {
        dist[i][j] = -1;
        visited[i][j] = false;
      }
    }
    dist[x1][y1] = 0;
    visited[x1][y1] = true;
    queue<pair<ll, ll>> q;
    q.push(make_pair(x1, y1));
    while (!q.empty()) {
      pair<ll, ll> curr = q.front();
      q.pop();
      ll i = curr.first;
      ll j = curr.second;
      for (ll p = 0; p < 4; p++) {
        for (ll Q = 1; Q <= k; Q++) {
          ll i1 = i + direc[p][0] * Q;
          ll j1 = j + direc[p][1] * Q;
          if (isValid(i1, j1) && mat[i1][j1] != '@') {
            if (dist[i1][j1] == -1) {
              dist[i1][j1] = dist[i][j] + 1;
              q.push(make_pair(i1, j1));
            } else {
              if (dist[i1][j1] >= (dist[i][j] + 1))
                dist[i1][j1] = dist[i][j] + 1;
            }

          } else
            break;
        }
      }
    }

    cout << dist[x2][y2] << endl;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < n; i++)
#define Rep(i, a, b) for (ll i = a; i < b; i++)
#define pb emplace_back
#define allv v.begin(), v.end()
using namespace std;
typedef long long int ll;
typedef unsigned long long ull;
typedef vector<ll> vll;
#define FASTio                                                                 \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define deb(x) cout << (#x) << " " << x << endl
ll mod = 1e9 + 7;
template <typename t> ll Max(ll a, ll b) { return (a > b) ? a : b; }
ll Min(ll a, ll b) { return (a > b) ? b : a; }
ll power(ll a, ll b) {
  ll p = 1;
  a = a % mod;
  b = b % mod;
  while (b) {
    if (b & 1) {
      p = ((p % mod) * (a % mod)) % mod;
    }
    a = ((a % mod) * (a % mod)) % mod;
    b = (b / 2);
  }
  return p % mod;
}
ll h, w, k;
ll ma = 1e15;
bool isValid(ll i, ll j) { return (i >= 1 && i <= h && j >= 1 && j <= w); }
ll direc[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int main() {
  int t;
  t = 1;
  //  cin>>t;
  while (t--) {
    //  ll h,w,k;
    cin >> h >> w >> k;
    ll x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    char mat[h + 1][w + 1];
    Rep(i, 1, h + 1) { Rep(j, 1, w + 1) cin >> mat[i][j]; }
    ll dist[h + 1][w + 1];
    bool visited[h + 1][w + 1];
    // memset(dist,INT_MAX,sizeof(dist));
    for (ll i = 1; i <= h; i++) {
      for (ll j = 1; j <= w; j++) {
        dist[i][j] = -1;
        visited[i][j] = false;
      }
    }
    dist[x1][y1] = 0;
    visited[x1][y1] = true;
    queue<pair<ll, ll>> q;
    q.push(make_pair(x1, y1));
    while (!q.empty()) {
      pair<ll, ll> curr = q.front();
      q.pop();
      ll i = curr.first;
      ll j = curr.second;
      for (ll p = 0; p < 4; p++) {
        for (ll Q = 1; Q <= k; Q++) {
          ll i1 = i + direc[p][0] * Q;
          ll j1 = j + direc[p][1] * Q;
          if (isValid(i1, j1) && mat[i1][j1] != '@') {
            if (dist[i1][j1] == -1) {
              dist[i1][j1] = dist[i][j] + 1;
              q.push(make_pair(i1, j1));
            } else {
              if (dist[i1][j1] >= (dist[i][j] + 1))
                dist[i1][j1] = dist[i][j] + 1;
              else
                break;
            }

          } else
            break;
        }
      }
    }

    cout << dist[x2][y2] << endl;
  }
  return 0;
}