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

#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;
#define rep(i, begin, n) for (int i = begin; i < n; i++)
#define repe(i, begin, n) for (int i = begin; i <= n; i++)
#define repr(i, begin, n) for (int i = begin; i > begin - n; i--)
#define repre(i, begin, end) for (int i = begin; i >= end; i--)

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const int inf = INT_MAX;
const int MOD = 1000000007;
const long long INF = LLONG_MAX;

// -------------------------------------------------------

ll H, W, K;
ll sx, sy, gx, gy;
vector<ll> c[1010101];
ll DX[] = {0, 1, 0, -1};
ll DY[] = {1, 0, -1, 0};

int main() {
  cin >> H >> W >> K;
  cin >> sx >> sy >> gx >> gy;
  repe(i, 1, H) {
    string s;
    cin >> s;
    c[i].push_back(-1);
    for (auto e : s) {
      if (e == '@') {
        c[i].push_back(-1);
      } else {
        c[i].push_back(inf);
      }
    }
    c[i].push_back(-1);
  }
  repe(i, 0, W + 1) {
    c[0].push_back(-1);
    c[H + 1].push_back(-1);
  }

  queue<Pll> que;
  que.push(Pll(sx, sy));
  c[sx][sy] = 0;

  while (!que.empty()) {
    auto elem = que.front();
    que.pop();
    ll cx = elem.first;
    ll cy = elem.second;

    rep(i, 0, 4) {
      ll dx = DX[i];
      ll dy = DY[i];

      repe(j, 1, K) {
        ll nx = cx + dx * j;
        ll ny = cy + dy * j;

        // 移動中に蓮にあたった場合
        if (c[nx][ny] == -1) {
          break;
        }
        if (c[nx][ny] != inf) {
          if (c[nx][ny] <= c[cx][cy]) {
            break;
          }
          continue;
        }

        c[nx][ny] = c[cx][cy] + 1;
        que.push(Pll(nx, ny));
      }
    }
  }

  if (c[gx][gy] == inf) {
    cout << -1;
    return 0;
  }

  cout << c[gx][gy];
}


Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;
#define rep(i, begin, n) for (int i = begin; i < n; i++)
#define repe(i, begin, n) for (int i = begin; i <= n; i++)
#define repr(i, begin, n) for (int i = begin; i > begin - n; i--)
#define repre(i, begin, end) for (int i = begin; i >= end; i--)

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const int inf = INT_MAX;
const int MOD = 1000000007;
const long long INF = LLONG_MAX;

// -------------------------------------------------------

ll H, W, K;
ll sx, sy, gx, gy;
vector<ll> c[1010101];
ll DX[] = {0, 1, 0, -1};
ll DY[] = {1, 0, -1, 0};

int main() {
  cin >> H >> W >> K;
  cin >> sx >> sy >> gx >> gy;
  repe(i, 1, H) {
    string s;
    cin >> s;
    c[i].push_back(-1);
    for (auto e : s) {
      if (e == '@') {
        c[i].push_back(-1);
      } else {
        c[i].push_back(inf);
      }
    }
    c[i].push_back(-1);
  }
  repe(i, 0, W + 1) {
    c[0].push_back(-1);
    c[H + 1].push_back(-1);
  }

  queue<Pll> que;
  que.push(Pll(sx, sy));
  c[sx][sy] = 0;

  while (!que.empty()) {
    auto elem = que.front();
    que.pop();
    ll cx = elem.first;
    ll cy = elem.second;

    rep(i, 0, 4) {
      ll dx = DX[i];
      ll dy = DY[i];

      repe(j, 1, K) {
        ll nx = cx + dx * j;
        ll ny = cy + dy * j;

        // 移動中に蓮にあたった場合
        if (c[nx][ny] == -1) {
          break;
        }
        if (c[nx][ny] != inf) {
          continue;
        }

        c[nx][ny] = c[cx][cy] + 1;
        que.push(Pll(nx, ny));
      }
    }
  }

  if (c[gx][gy] == inf) {
    cout << -1;
    return 0;
  }

  cout << c[gx][gy];
}