Problem p02644 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, n, m) for (int i = (int)(n); i <= (int)(m); i++)
#define RFOR(i, n, m) for (int i = (int)(n); i >= (int)(m); i--)
#define ITR(x, c) for (__typeof(c.begin()) x = c.begin(); x != c.end(); x++)
#define RITR(x, c) for (__typeof(c.rbegin()) x = c.rbegin(); x != c.rend(); x++)
#define setp(n) fixed << setprecision(n)

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

#define ll long long
#define vll vector<ll>
#define vi vector<int>
#define pll pair<ll, ll>
#define pi pair<int, int>

#define all(a) (a.begin()), (a.end())
#define rall(a) (a.rbegin()), (a.rend())
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define ins insert

using namespace std;

int H, W, K;

const int INF = INT_MAX;

//-------------------------------------------------
//--Grid BFS
//-------------------------------------------------

template <typename T>
vector<vector<int>> gBFS(int si, int sj, vector<T> &field) {
  int dx[] = {0, 1, 0, -1};
  int dy[] = {1, 0, -1, 0};
  int h = field.size();
  int w = field[0].size();
  using P = pair<int, int>;
  queue<P> que;
  que.emplace(si, sj);
  vector<vector<int>> dist(h, vector<int>(w, INF));
  dist[si][sj] = 0;
  while (que.size()) {
    int x, y;
    tie(y, x) = que.front();
    que.pop();
    for (int i = 0; i < 4; i++) {
      FOR(j, 1, K) {
        int tx = x + j * dx[i];
        int ty = y + j * dy[i];
        if (!(0 <= tx && tx < w && 0 <= ty && ty < h))
          break;
        if (field[ty][tx] == '@')
          break;
        // if (dist[ty][tx]<dist[y][x]+1) break;
        if (dist[ty][tx] != INF)
          continue;
        dist[ty][tx] = dist[y][x] + 1;
        que.emplace(ty, tx);
      }
    }
  }
  return dist;
}

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

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> H >> W >> K;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  vector<string> f(H);
  rep(i, H) cin >> f[i];
  auto dist = gBFS(x1 - 1, y1 - 1, f);
  // rep(i,H){
  //     rep(j,W){
  //         if (dist[i][j]!=INF){
  //             cout<<dist[i][j]<<" ";
  //         }else{
  //             cout<<"I ";
  //         }
  //     }cout<<endl;
  // }
  if (dist[x2 - 1][y2 - 1] != INF) {
    cout << dist[x2 - 1][y2 - 1] << endl;
  } else {
    cout << "-1\n";
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, n, m) for (int i = (int)(n); i <= (int)(m); i++)
#define RFOR(i, n, m) for (int i = (int)(n); i >= (int)(m); i--)
#define ITR(x, c) for (__typeof(c.begin()) x = c.begin(); x != c.end(); x++)
#define RITR(x, c) for (__typeof(c.rbegin()) x = c.rbegin(); x != c.rend(); x++)
#define setp(n) fixed << setprecision(n)

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

#define ll long long
#define vll vector<ll>
#define vi vector<int>
#define pll pair<ll, ll>
#define pi pair<int, int>

#define all(a) (a.begin()), (a.end())
#define rall(a) (a.rbegin()), (a.rend())
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define ins insert

using namespace std;

int H, W, K;

const int INF = INT_MAX;

//-------------------------------------------------
//--Grid BFS
//-------------------------------------------------

template <typename T>
vector<vector<int>> gBFS(int si, int sj, vector<T> &field) {
  int dx[] = {0, 1, 0, -1};
  int dy[] = {1, 0, -1, 0};
  int h = field.size();
  int w = field[0].size();
  using P = pair<int, int>;
  queue<P> que;
  que.emplace(si, sj);
  vector<vector<int>> dist(h, vector<int>(w, INF));
  dist[si][sj] = 0;
  while (que.size()) {
    int x, y;
    tie(y, x) = que.front();
    que.pop();
    for (int i = 0; i < 4; i++) {
      FOR(j, 1, K) {
        int tx = x + j * dx[i];
        int ty = y + j * dy[i];
        if (!(0 <= tx && tx < w && 0 <= ty && ty < h))
          break;
        if (field[ty][tx] == '@')
          break;
        if (dist[ty][tx] < dist[y][x] + 1)
          break;
        if (dist[ty][tx] != INF)
          continue;
        dist[ty][tx] = dist[y][x] + 1;
        que.emplace(ty, tx);
      }
    }
  }
  return dist;
}

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

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> H >> W >> K;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  vector<string> f(H);
  rep(i, H) cin >> f[i];
  auto dist = gBFS(x1 - 1, y1 - 1, f);
  // rep(i,H){
  //     rep(j,W){
  //         if (dist[i][j]!=INF){
  //             cout<<dist[i][j]<<" ";
  //         }else{
  //             cout<<"I ";
  //         }
  //     }cout<<endl;
  // }
  if (dist[x2 - 1][y2 - 1] != INF) {
    cout << dist[x2 - 1][y2 - 1] << endl;
  } else {
    cout << "-1\n";
  }
  return 0;
}