Problem p02644 (C++)

Buggy submission (status: Time Limit Exceeded):

#pragma GCC optimize("O3")

#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include <prettyprint.hpp>
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]: ", d_err(__VA_ARGS__);
#else
#define debug(...) 83;
#endif

void d_err() { cerr << endl; }

template <typename H, typename... T> void d_err(H h, T... t) {
  cerr << h << " ";
  d_err(t...);
}

template <typename T> void print(T x) { cout << x << endl; }

#define ALL(x) (x).begin(), (x).end()
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define bcnt __builtin_popcountll

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<ll, ll> Pll;
typedef pair<int, int> Pin;

ll INF = 1e16;
int inf = 1e9;
ll MOD = 1e9 + 7;

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(20);

  int H, W, K;
  cin >> H >> W >> K;

  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--, y1--, x2--, y2--;

  vector<vector<char>> m(H, vector<char>(W));
  REP(i, H) REP(j, W) cin >> m[i][j];

  vi dx = {1, 0, -1, 0}, dy = {0, 1, 0, -1};

  queue<pair<ll, Pin>> q;
  q.push({0, {x1, y1}});

  vector<vll> dist(H, vll(W, INF));

  while (!q.empty()) {
    auto p = q.front();
    q.pop();

    ll cost = p.fi;
    int x = p.se.fi, y = p.se.se;

    REP(i, 4) {
      FOR(k, 1, K + 1) {
        int nx = x + dx[i] * k, ny = y + dy[i] * k;
        if (nx < 0 || nx >= H || ny < 0 || ny >= W)
          continue;
        if (m[nx][ny] == '@')
          break;

        if (cost + 1 < dist[nx][ny]) {
          q.push({cost + 1, {nx, ny}});
          dist[nx][ny] = cost + 1;
        }
      }
    }
  }

  print(dist[x2][y2] == INF ? -1 : dist[x2][y2]);
}


Fixed submission:

#pragma GCC optimize("O3")

#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include <prettyprint.hpp>
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]: ", d_err(__VA_ARGS__);
#else
#define debug(...) 83;
#endif

void d_err() { cerr << endl; }

template <typename H, typename... T> void d_err(H h, T... t) {
  cerr << h << " ";
  d_err(t...);
}

template <typename T> void print(T x) { cout << x << endl; }

#define ALL(x) (x).begin(), (x).end()
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define bcnt __builtin_popcountll

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<ll, ll> Pll;
typedef pair<int, int> Pin;

ll INF = 1e16;
int inf = 1e9;
ll MOD = 1e9 + 7;

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(20);

  int H, W, K;
  cin >> H >> W >> K;

  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--, y1--, x2--, y2--;

  vector<vector<char>> m(H, vector<char>(W));
  REP(i, H) REP(j, W) cin >> m[i][j];

  vi dx = {1, 0, -1, 0}, dy = {0, 1, 0, -1};

  queue<pair<ll, Pin>> q;
  q.push({0, {x1, y1}});

  vector<vll> dist(H, vll(W, INF));

  while (!q.empty()) {
    auto p = q.front();
    q.pop();

    ll cost = p.fi;
    int x = p.se.fi, y = p.se.se;

    REP(i, 4) {
      FOR(k, 1, K + 1) {
        int nx = x + dx[i] * k, ny = y + dy[i] * k;
        if (nx < 0 || nx >= H || ny < 0 || ny >= W)
          continue;
        if (m[nx][ny] == '@')
          break;

        if (cost + 1 < dist[nx][ny]) {
          q.push({cost + 1, {nx, ny}});
          dist[nx][ny] = cost + 1;
        }
        if (dist[nx][ny] == cost + 1)
          continue;
        else
          break;
      }
    }
  }

  print(dist[x2][y2] == INF ? -1 : dist[x2][y2]);
}