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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> PP;
#define MOD 1000000007
// #define MOD 998244353
#define INF 1145141919810893364
// #define INF 810114514
#define PI 3.141592653589
#define setdouble setprecision
#define REP(i, n) for (ll i = 0; i < (n); ++i)
#define OREP(i, n) for (ll i = 1; i <= (n); ++i)
#define RREP(i, n) for (ll i = (n)-1; i >= 0; --i)
#define GOODBYE                                                                \\
  do {                                                                         \\
    cout << "0" << endl;                                                       \\
    return 0;                                                                  \\
  } while (false)
#define MM << " " <<
#define Endl endl
#define debug false
#define debug2 false

int main(void) {

  ll H, W, K;
  ll x1, y1, x2, y2;
  vector<string> C;

  cin >> H >> W >> K;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;
  C.resize(H);
  REP(i, H) { cin >> C[i]; }

  vector<vector<ll>> cost(H, vector<ll>(W, INF));
  cost[x1][y1] = 0;
  queue<pair<ll, ll>> bfs;
  bfs.push({x1, y1});

  ll dx[4] = {1, 0, -1, 0};
  ll dy[4] = {0, 1, 0, -1};

  while (!bfs.empty()) {
    pair<ll, ll> t = bfs.front();
    bfs.pop();

    // cout << t.first MM t.second MM cost[t.first][t.second] << endl;

    REP(i, 4) {
      ll x = t.first, y = t.second;
      REP(j, K) {
        x += dx[i];
        y += dy[i];

        if (x < 0 || H <= x)
          break;
        if (y < 0 || W <= y)
          break;
        if (C[x][y] == '@')
          break;
        if (cost[x][y] <= cost[t.first][t.second])
          break;

        if (cost[x][y] > cost[t.first][t.second] + 1) {
          cost[x][y] = cost[t.first][t.second] + 1;
          bfs.push({x, y});
        }
      }
    }
  }

  if (cost[x2][y2] == INF) {
    cout << -1 << endl;
  } else {
    cout << cost[x2][y2] << endl;
  }

#if debug
  REP(i, H) {
    REP(j, W) {
      if (cost[i][j] == INF) {
        cout << -1 << " ";
      } else {
        cout << cost[i][j] << " ";
      }
    }
    cout << endl;
  }
#endif

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> PP;
#define MOD 1000000007
// #define MOD 998244353
#define INF 1145141919810893364
// #define INF 810114514
#define PI 3.141592653589
#define setdouble setprecision
#define REP(i, n) for (ll i = 0; i < (n); ++i)
#define OREP(i, n) for (ll i = 1; i <= (n); ++i)
#define RREP(i, n) for (ll i = (n)-1; i >= 0; --i)
#define GOODBYE                                                                \\
  do {                                                                         \\
    cout << "0" << endl;                                                       \\
    return 0;                                                                  \\
  } while (false)
#define MM << " " <<
#define Endl endl
#define debug false
#define debug2 false

int main(void) {

  ll H, W, K;
  ll x1, y1, x2, y2;
  vector<string> C;

  cin >> H >> W >> K;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;
  C.resize(H);
  REP(i, H) { cin >> C[i]; }

  vector<vector<ll>> cost(H, vector<ll>(W, INF));
  cost[x1][y1] = 0;
  queue<pair<ll, ll>> bfs;
  bfs.push({x1, y1});

  ll dx[4] = {1, 0, -1, 0};
  ll dy[4] = {0, 1, 0, -1};

  while (!bfs.empty()) {
    pair<ll, ll> t = bfs.front();
    bfs.pop();

    // cout << t.first MM t.second MM cost[t.first][t.second] << endl;

    REP(i, 4) {
      ll x = t.first, y = t.second;
      REP(j, K) {
        x += dx[i];
        y += dy[i];

        if (x < 0 || H <= x)
          break;
        if (y < 0 || W <= y)
          break;
        if (C[x][y] == '@')
          break;
        if (cost[x][y] <= cost[t.first][t.second])
          break;

        if (cost[x][y] >= cost[t.first][t.second] + 1) {
          cost[x][y] = cost[t.first][t.second] + 1;
          bfs.push({x, y});
        }
      }
    }
  }

  if (cost[x2][y2] == INF) {
    cout << -1 << endl;
  } else {
    cout << cost[x2][y2] << endl;
  }

  /*
  REP(i,H){
      REP(j,W){
          if(cost[i][j]==INF){
              cout << -1 << " ";
          }else{
              cout << cost[i][j] << " ";
          }
      }cout << endl;
  }*/

  return 0;
}