Problem p02644 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#define dlog(str) cout << "====" << str << endl;
#else
#define dlog(str)
#endif
#define INF 999999999
#define MOD 1000000007
#define REP(i, n) for (int i = 0, i##_l = (n); i < i##_l; i++)
#define FOR(i, s, e) for (int i = s, i##_l = (e); i < i##_l; i++)
#define LLI long long int
#define _min(a, b) ((a < b) ? a : b)
#define _max(a, b) ((a < b) ? b : a)
#define chmax(a, b) a = _max(a, b)
#define chmin(a, b) a = _min(a, b)
#define bit(a, shift) ((a>>shift)&1))
#define pm(a) ((a) ? 1 : -1)
#define SORT(v) sort(v.begin(), v.end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
// int 2.14E±9    lli 9.2E±18    double 1.7E±380

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int main() {
  cout << fixed << setprecision(10);

  int h, w, k;
  cin >> h >> w >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;

  vector<string> m(h);
  REP(i, h) { cin >> m[i]; }

  vector<vector<int>> dist(h, vector<int>(w, INF));

  queue<tuple<int, int, int>> q;
  q.emplace(0, x1, y1);

  dist[x1][y1] = 0;

  while (q.empty() == false) {
    auto [c, x, y] = q.front();
    if (dist[x][y] != c)
      continue;
    q.pop();

    REP(j, 4) {
      int nx = x;
      int ny = y;
      FOR(i, 1, k + 1) {
        nx += dx[j];
        ny += dy[j];
        if (nx < 0 || h <= nx || ny < 0 || w <= ny)
          break;
        if (m[nx][ny] == '@')
          break;

        if (dist[nx][ny] <= c)
          break;

        if (dist[nx][ny] > c + 1) {
          dist[nx][ny] = c + 1;
          q.emplace(c + 1, nx, ny);
        }
      }
    }
  }
  if (dist[x2][y2] == INF) {
    cout << -1;
    return 0;
  }
  cout << dist[x2][y2];
  return 0;
}


Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#define dlog(str) cout << "====" << str << endl;
#else
#define dlog(str)
#endif
#define INF 999999999
#define MOD 1000000007
#define REP(i, n) for (int i = 0, i##_l = (n); i < i##_l; i++)
#define FOR(i, s, e) for (int i = s, i##_l = (e); i < i##_l; i++)
#define LLI long long int
#define _min(a, b) ((a < b) ? a : b)
#define _max(a, b) ((a < b) ? b : a)
#define chmax(a, b) a = _max(a, b)
#define chmin(a, b) a = _min(a, b)
#define bit(a, shift) ((a>>shift)&1))
#define pm(a) ((a) ? 1 : -1)
#define SORT(v) sort(v.begin(), v.end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
// int 2.14E±9    lli 9.2E±18    double 1.7E±380

struct vec2 {
  int x, y;
  vec2(int _x, int _y) : x(_x), y(_y) {}
};

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int main() {
  cout << fixed << setprecision(10);

  int h, w, k;
  cin >> h >> w >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;

  vector<string> m(h);
  REP(i, h) { cin >> m[i]; }

  vector<vector<int>> dist(h, vector<int>(w, -1));

  queue<tuple<int, int, int>> q;
  q.emplace(0, x1, y1);

  dist[x1][y1] = 0;

  while (q.empty() == false) {
    auto [c, x, y] = q.front();
    if (dist[x][y] != c)
      continue;
    q.pop();

    REP(j, 4) {
      int nx = x;
      int ny = y;
      FOR(i, 1, k + 1) {
        nx += dx[j];
        ny += dy[j];
        if (nx < 0 || h <= nx || ny < 0 || w <= ny)
          break;
        if (m[nx][ny] == '@')
          break;

        if (dist[nx][ny] != -1)
          continue;

        dist[nx][ny] = c + 1;
        q.emplace(c + 1, nx, ny);
      }
    }
  }
  cout << dist[x2][y2];
  return 0;
}