Problem p02644 (C++)

Buggy submission (status: Time Limit Exceeded):
// https://atcoder.jp/contests/abc170/tasks/abc170_f

#include <bits/stdc++.h>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, b, e) for (int i = (b); i <= (int)(e); i++)
#define DEBUG 0
#if DEBUG
#define _GLIBCXX_DEBUG
#define DUMP(a)                                                                \
  REP(_i, a.size()) cout << a[_i] << (_i + 1 == a.size() ? "\n" : " ")
#define DUMP2D(b)                                                              \
  REP(_j, b.size()) DUMP(b[_j]);                                               \
  cout << endl
#else
#define DUMP(a)
#define DUMP2D(b)
#endif

const int H_MAX = 1e6;
const int W_MAX = 1e6;
const int K_MAX = 1e6;
const int INF = 1e9;
const int D[4][2] = {{-1, 0}, {0, -1}, {0, 1}, {1, 0}};

using P = pair<int, int>;

int H, W, K;
int x_1, y_1, x_2, y_2;
vector<string> c;
vector<vector<int>> d;

int bfs() {
  queue<P> que;
  que.push({x_1, y_1});
  d = vector<vector<int>>(H, vector<int>(W, INF));
  d[x_1][y_1] = 0;
  while (!que.empty()) {
    P p = que.front();
    que.pop();
    int i = p.first;
    int j = p.second;
    if (i == x_2 && j == y_2)
      break;
    REP(k, 4) {
      int i2 = i;
      int j2 = j;
      FOR(l, 1, K) {
        i2 += D[k][0];
        j2 += D[k][1];
        if (i2 < 0 || i2 >= H || j2 < 0 || j2 >= W)
          break;
        if (c[i2][j2] == '@')
          break;
        if (d[i2][j2] < INF)
          continue;
        que.push({i2, j2});
        d[i2][j2] = d[i][j] + 1;
      }
    }
  }
  return d[x_2][y_2];
}

int main() {
  cin >> H >> W >> K;
  cin >> x_1 >> y_1 >> x_2 >> y_2;
  x_1--;
  y_1--;
  x_2--;
  y_2--;
  c = vector<string>(H);
  REP(i, H) cin >> c[i];

  int ans = bfs();
  DUMP2D(d);
  if (ans < INF)
    cout << ans << endl;
  else
    cout << -1 << endl;
}


Fixed submission:
// https://atcoder.jp/contests/abc170/tasks/abc170_f

#include <bits/stdc++.h>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, b, e) for (int i = (b); i <= (int)(e); i++)
#define DEBUG 0
#if DEBUG
#define _GLIBCXX_DEBUG
#define DUMP(a)                                                                \
  REP(_i, a.size()) cout << a[_i] << (_i + 1 == a.size() ? "\n" : " ")
#define DUMP2D(b)                                                              \
  REP(_j, b.size()) DUMP(b[_j]);                                               \
  cout << endl
#else
#define DUMP(a)
#define DUMP2D(b)
#endif

const int H_MAX = 1e6;
const int W_MAX = 1e6;
const int K_MAX = 1e6;
const int INF = 1e9;
const int D[4][2] = {{-1, 0}, {0, -1}, {0, 1}, {1, 0}};

using P = pair<int, int>;

int H, W, K;
int x_1, y_1, x_2, y_2;
vector<string> c;
vector<vector<int>> d;

int bfs() {
  queue<P> que;
  que.push({x_1, y_1});
  d = vector<vector<int>>(H, vector<int>(W, INF));
  d[x_1][y_1] = 0;
  while (!que.empty()) {
    P p = que.front();
    que.pop();
    int i = p.first;
    int j = p.second;
    if (i == x_2 && j == y_2)
      break;
    REP(k, 4) {
      int i2 = i;
      int j2 = j;
      FOR(l, 1, K) {
        i2 += D[k][0];
        j2 += D[k][1];
        if (i2 < 0 || i2 >= H || j2 < 0 || j2 >= W)
          break;
        if (c[i2][j2] == '@' || d[i2][j2] <= d[i][j])
          break;
        if (d[i2][j2] < INF)
          continue;
        que.push({i2, j2});
        d[i2][j2] = d[i][j] + 1;
      }
    }
  }
  return d[x_2][y_2];
}

int main() {
  cin >> H >> W >> K;
  cin >> x_1 >> y_1 >> x_2 >> y_2;
  x_1--;
  y_1--;
  x_2--;
  y_2--;
  c = vector<string>(H);
  REP(i, H) cin >> c[i];

  int ans = bfs();
  DUMP2D(d);
  if (ans < INF)
    cout << ans << endl;
  else
    cout << -1 << endl;
}