Problem p02644 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, n) for (int i = 1; i < n; i++)
#define rev(i, n) for (int i = n - 1; i >= 0; i--)
#define all(v) v.begin(), v.end()
#define P pair<int, int>
#define len(s) (int)s.size()
#define pb push_back

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
constexpr int mod = 1e9 + 7;
constexpr int inf = 3e18;

int H, W, K;
int sx, sy, gx, gy;
vector<char> c[1000005];
vector<int> dis[1000005];
int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
signed main() {
  cin >> H >> W >> K;
  cin >> sx >> sy >> gx >> gy;
  sx--;
  sy--;
  gx--;
  gy--;
  rep(i, H) {
    c[i].resize(W);
    dis[i].resize(W, inf);
  }
  rep(i, H) rep(j, W) cin >> c[i][j];
  queue<P> que;
  que.push({sx, sy});
  dis[sx][sy] = 0;
  while (!que.empty()) {
    P q = que.front();
    que.pop();
    rep(i, 4) {
      REP(j, K + 1) {
        int nx = q.first + dx[i] * j;
        int ny = q.second + dy[i] * j;
        if (nx < 0 || ny < 0 || nx >= H || ny >= W)
          break;
        if (c[nx][ny] == '@')
          break;
        int ne = dis[q.first][q.second] + 1;
        if (dis[nx][ny] < ne)
          break;
        dis[nx][ny] = ne;
        que.push({nx, ny});
      }
    }
  }
  /*rep(i,H){
          rep(j,W)cout<<dis[i][j]<<" ";
          cout<<endl;
  }*/
  if (dis[gx][gy] == inf)
    cout << -1 << endl;
  else
    cout << dis[gx][gy] << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, n) for (int i = 1; i < n; i++)
#define rev(i, n) for (int i = n - 1; i >= 0; i--)
#define all(v) v.begin(), v.end()
#define P pair<int, int>
#define len(s) (int)s.size()
#define pb push_back

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
constexpr int mod = 1e9 + 7;
constexpr int inf = 3e18;

int H, W, K;
int sx, sy, gx, gy;
vector<char> c[1000005];
vector<int> dis[1000005];
int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
signed main() {
  cin >> H >> W >> K;
  cin >> sx >> sy >> gx >> gy;
  sx--;
  sy--;
  gx--;
  gy--;
  rep(i, H) {
    c[i].resize(W);
    dis[i].resize(W, inf);
  }
  rep(i, H) rep(j, W) cin >> c[i][j];
  queue<P> que;
  que.push({sx, sy});
  dis[sx][sy] = 0;
  while (!que.empty()) {
    P q = que.front();
    que.pop();
    rep(i, 4) {
      REP(j, K + 1) {
        int nx = q.first + dx[i] * j;
        int ny = q.second + dy[i] * j;
        if (nx < 0 || ny < 0 || nx >= H || ny >= W)
          break;
        if (c[nx][ny] == '@')
          break;
        int ne = dis[q.first][q.second] + 1;
        if (dis[nx][ny] < ne)
          break;
        if (dis[nx][ny] > ne) {
          dis[nx][ny] = ne;
          que.push({nx, ny});
        }
      }
    }
  }
  /*rep(i,H){
          rep(j,W)cout<<dis[i][j]<<" ";
          cout<<endl;
  }*/
  if (dis[gx][gy] == inf)
    cout << -1 << endl;
  else
    cout << dis[gx][gy] << endl;
}