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

#include <bits/stdc++.h>
#define syosu(x) fixed << setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<string> vs;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<P, int> pip;
typedef vector<pip> vip;
const int inf = 1 << 30;
const ll INF = 1ll << 60;
const double pi = acos(-1);
const double eps = 1e-8;
const ll mod = 1e9 + 7;
const int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, -1, 0, 1};

ll Pow(ll n, ll p) {
  ll r = 1;
  for (; p > 0; p >>= 1) {
    if (p & 1)
      r = (r * n) % mod;
    n = (n * n) % mod;
  }
  return r;
}

const int M = 2000005;
ll F[M];

void Init() {
  F[0] = 1;
  for (int i = 1; i < M; i++)
    F[i] = F[i - 1] * i % mod;
}

ll Div(ll n, ll m) { return n * Pow(m, mod - 2) % mod; }

ll nCk(ll n, ll k) { return Div(F[n], F[n - k] * F[k] % mod); }

ll nPk(ll n, ll k) { return nCk(n, k) * F[k] % mod; }

int h, w, k, sx, sy, gx, gy;
vs a;
vvi c;

int main() {
  cin >> h >> w >> k >> sx >> sy >> gx >> gy;
  sx--, sy--, gx--, gy--;
  a = vs(h);
  c = vvi(h, vi(w, inf));
  for (int i = 0; i < h; i++)
    cin >> a[i];
  c[sx][sy] = 0;
  queue<P> q;
  q.push({sx, sy});
  while (!q.empty()) {
    P p = q.front();
    q.pop();
    int x = p.first, y = p.second;
    for (int i = 0; i < 4; i++) {
      int X = x, Y = y;
      for (int j = 1; j <= k; j++) {
        X += dx[i], Y += dy[i];
        if (X < 0 || X >= h || Y < 0 || Y >= w || a[X][Y] == '@' ||
            c[X][Y] <= c[x][y])
          break;
        if (c[X][Y] == inf) {
          c[X][Y] = c[x][y] + 1;
          q.push({X, Y});
        }
      }
    }
  }
  /*	for(int i=0;i<h;i++){
                  for(auto j:c[i]) cout<<(j==inf?9:j)<<' ';
                  cout<<endl;
          }*/
  int t = c[gx][gy];
  if (t == inf)
    t = -1;
  cout << t << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define syosu(x) fixed << setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<string> vs;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<P, int> pip;
typedef vector<pip> vip;
const int inf = 1 << 30;
const ll INF = 1ll << 60;
const double pi = acos(-1);
const double eps = 1e-8;
const ll mod = 1e9 + 7;
const int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, -1, 0, 1};

ll Pow(ll n, ll p) {
  ll r = 1;
  for (; p > 0; p >>= 1) {
    if (p & 1)
      r = (r * n) % mod;
    n = (n * n) % mod;
  }
  return r;
}

const int M = 2000005;
ll F[M];

void Init() {
  F[0] = 1;
  for (int i = 1; i < M; i++)
    F[i] = F[i - 1] * i % mod;
}

ll Div(ll n, ll m) { return n * Pow(m, mod - 2) % mod; }

ll nCk(ll n, ll k) { return Div(F[n], F[n - k] * F[k] % mod); }

ll nPk(ll n, ll k) { return nCk(n, k) * F[k] % mod; }

int h, w, k, sx, sy, gx, gy;
vs a;
vvi c;

int main() {
  cin >> h >> w >> k >> sx >> sy >> gx >> gy;
  sx--, sy--, gx--, gy--;
  a = vs(h);
  c = vvi(h, vi(w, inf));
  for (int i = 0; i < h; i++)
    cin >> a[i];
  c[sx][sy] = 0;
  queue<P> q;
  q.push({sx, sy});
  while (!q.empty()) {
    P p = q.front();
    q.pop();
    int x = p.first, y = p.second;
    for (int i = 0; i < 4; i++) {
      int X = x, Y = y;
      for (int j = 1; j <= k; j++) {
        X += dx[i], Y += dy[i];
        if (X < 0 || X >= h || Y < 0 || Y >= w || a[X][Y] == '@' ||
            c[X][Y] <= c[x][y])
          break;
        c[X][Y] = c[x][y] + 1;
        q.push({X, Y});
      }
    }
  }
  /*	for(int i=0;i<h;i++){
                  for(auto j:c[i]) cout<<(j==inf?9:j)<<' ';
                  cout<<endl;
          }*/
  int t = c[gx][gy];
  if (t == inf)
    t = -1;
  cout << t << endl;
}