Problem p02644 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define whole(x) (x).begin(), (x).end()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
using P = pair<int, int>;
#define debug(var) cerr << "[" << #var << "] " << var << endl
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
const ll mod = 1000000007;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
const int INF = 1001001001;

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;
  vector<string> c(h);
  rep(i, h) cin >> c[i];
  vector<vector<int>> dist(h, vector<int>(w, INF));
  queue<P> q;
  q.emplace(x1, y1);
  dist[x1][y1] = 0;
  while (!q.empty()) {
    P now = q.front();
    q.pop();
    int nx = now.first;
    int ny = now.second;
    int d = dist[nx][ny];
    rep(i, 4) {
      for (int j = 1; j <= k; j++) {
        int ix = nx + dx[i] * j;
        int iy = ny + dy[i] * j;
        if (ix < 0 || ix >= h || iy < 0 || iy >= w)
          break;
        if (dist[ix][iy] <= d)
          break;
        if (c[ix][iy] == '@')
          break;
        dist[ix][iy] = d + 1;
        q.emplace(ix, iy);
      }
    }
    /*
    rep(i, h) {
        rep(j, w) {
            cout << dist[i][j];
        }
        cout << endl;
    }
    */
  }

  int ans = dist[x2][y2];
  if (ans == INF)
    ans = -1;

  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define whole(x) (x).begin(), (x).end()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
using P = pair<int, int>;
#define debug(var) cerr << "[" << #var << "] " << var << endl
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
const ll mod = 1000000007;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
const int INF = 1001001001;

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;
  vector<string> c(h);
  rep(i, h) cin >> c[i];
  vector<vector<int>> dist(h, vector<int>(w, INF));
  queue<P> q;
  q.emplace(x1, y1);
  dist[x1][y1] = 0;
  while (!q.empty()) {
    P now = q.front();
    q.pop();
    int nx = now.first;
    int ny = now.second;
    int d = dist[nx][ny];
    rep(i, 4) {
      for (int j = 1; j <= k; j++) {
        int ix = nx + dx[i] * j;
        int iy = ny + dy[i] * j;
        if (ix < 0 || ix >= h || iy < 0 || iy >= w)
          break;
        if (dist[ix][iy] <= d)
          break;
        if (c[ix][iy] == '@')
          break;
        if (dist[ix][iy] == d + 1)
          continue;
        dist[ix][iy] = d + 1;
        q.emplace(ix, iy);
      }
    }
    /*
    rep(i, h) {
        rep(j, w) {
            cout << dist[i][j];
        }
        cout << endl;
    }
    */
  }

  int ans = dist[x2][y2];
  if (ans == INF)
    ans = -1;

  cout << ans << endl;
  return 0;
}