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

#ifndef COMPETITIVE_HEADER
#pragma GCC optimize("Ofast")
#ifdef ONLINE_JUDGE
#pragma GCC target("avx512f")
#else
#pragma GCC target("avx")
#endif
//----------------------------
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
#define int ll

#define FOR(i, j, n) for (int i = (int)(j); i < (n); i++)
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define REPS(i, n) for (int i = 1; i <= (int)(n); i++)
#define REPN(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define REPNS(i, n) for (int i = (int)(n); i > 0; i--)

#define I(n) scanf("%lld", &(n))
#define LL(n) scanf("%lld", &(n))
#define pb(n) push_back((n))
#define mp(i, j) make_pair((i), (j))
#define eb(i, j) emplace_back((i), (j))
#define y0 y3487465
#define y1 y8687969
#define j0 j1347829
#define j1 j234892
#define uniq(v) v.erase(unique(v.begin(), v.end()), v.end())

#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())

typedef vector<int> vi;
typedef pair<int, int> pi;
typedef vector<pi> vpi;
typedef vector<vi> vvi;
typedef vector<vpi> vvpi;
typedef vector<vvi> vvvi;

const int mod = 1000000007;
#endif

int h, w, k;
string s;
int x1, y1, x2, y2;

signed main() {
  cin >> h >> w >> k;
  cin >> x1 >> y1 >> x2 >> y2;
  char table[h + 2][w + 2];
  memset(table, '@', sizeof(table));
  REP(i, h) {
    cin >> s;
    REP(j, w) { table[i + 1][j + 1] = s[j]; }
  }
  int ans[h + 2][w + 2];
  REP(i, h + 2) REP(j, w + 2) ans[i][j] = INT_MAX / 2;
  ans[x1][y1] = 0;
  deque<pi> q;
  q.emplace_back(x1, y1);

  // REP(i,h+2) { REP(j,w+2) cerr << table[i][j]; cerr << endl; }
  while (!q.empty()) {
    auto pos = q.front();
    q.pop_front();
    int x = pos.first;
    int y = pos.second;
    int dist = ans[x][y];
    table[x][y] = '@';
    REPS(ki, k) {
      int xk = x - ki;
      int yk = y;

      if (table[xk][yk] == '@')
        break;
      if (ans[xk][yk] > dist + 1) {
        q.emplace_back(xk, yk);
        ans[xk][yk] = dist + 1;
      }
    }
    REPS(ki, k) {
      int xk = x + ki;
      int yk = y;

      if (table[xk][yk] == '@')
        break;
      if (ans[xk][yk] > dist + 1) {
        q.emplace_back(xk, yk);
        ans[xk][yk] = dist + 1;
      }
    }
    REPS(ki, k) {
      int xk = x;
      int yk = y - ki;

      if (table[xk][yk] == '@')
        break;
      if (ans[xk][yk] > dist + 1) {
        q.emplace_back(xk, yk);
        ans[xk][yk] = dist + 1;
      }
    }
    REPS(ki, k) {
      int xk = x;
      int yk = y + ki;

      if (table[xk][yk] == '@')
        break;
      if (ans[xk][yk] > dist + 1) {
        q.emplace_back(xk, yk);
        ans[xk][yk] = dist + 1;
      }
    }
  }
  cout << (ans[x2][y2] < INT_MAX / 2 ? ans[x2][y2] : -1) << endl;
}

Error: TLE

Fixed submission:

#ifndef COMPETITIVE_HEADER
#pragma GCC optimize("Ofast")
#ifdef ONLINE_JUDGE
#pragma GCC target("avx512f")
#else
#pragma GCC target("avx")
#endif
//----------------------------
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
#define int ll

#define FOR(i, j, n) for (int i = (int)(j); i < (n); i++)
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define REPS(i, n) for (int i = 1; i <= (int)(n); i++)
#define REPN(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define REPNS(i, n) for (int i = (int)(n); i > 0; i--)

#define I(n) scanf("%lld", &(n))
#define LL(n) scanf("%lld", &(n))
#define pb(n) push_back((n))
#define mp(i, j) make_pair((i), (j))
#define eb(i, j) emplace_back((i), (j))
#define y0 y3487465
#define y1 y8687969
#define j0 j1347829
#define j1 j234892
#define uniq(v) v.erase(unique(v.begin(), v.end()), v.end())

#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())

typedef vector<int> vi;
typedef pair<int, int> pi;
typedef vector<pi> vpi;
typedef vector<vi> vvi;
typedef vector<vpi> vvpi;
typedef vector<vvi> vvvi;

const int mod = 1000000007;
#endif

int h, w, k;
string s;
int x1, y1, x2, y2;

signed main() {
  cin >> h >> w >> k;
  cin >> x1 >> y1 >> x2 >> y2;
  char table[h + 2][w + 2];
  memset(table, '@', sizeof(table));
  REP(i, h) {
    cin >> s;
    REP(j, w) { table[i + 1][j + 1] = s[j]; }
  }
  int ans[h + 2][w + 2];
  REP(i, h + 2) REP(j, w + 2) ans[i][j] = INT_MAX / 2;
  ans[x1][y1] = 0;
  deque<pi> q;
  q.emplace_back(x1, y1);

  // REP(i,h+2) { REP(j,w+2) cerr << table[i][j]; cerr << endl; }
  while (!q.empty()) {
    auto pos = q.front();
    q.pop_front();
    int x = pos.first;
    int y = pos.second;
    int dist = ans[x][y];
    REPS(ki, k) {
      int xk = x - ki;
      int yk = y;

      if (table[xk][yk] == '@')
        break;
      if (ans[xk][yk] > dist + 1) {
        q.emplace_back(xk, yk);
        ans[xk][yk] = dist + 1;
      }
    }
    REPS(ki, k) {
      int xk = x + ki;
      int yk = y;

      if (table[xk][yk] == '@')
        break;
      if (ans[xk][yk] > dist + 1) {
        q.emplace_back(xk, yk);
        ans[xk][yk] = dist + 1;
      }
    }
    REPS(ki, k) {
      int xk = x;
      int yk = y - ki;

      if (table[xk][yk] == '@')
        break;
      if (ans[xk][yk] > dist + 1) {
        q.emplace_back(xk, yk);
        ans[xk][yk] = dist + 1;
      }
    }
    REPS(ki, k) {
      int xk = x;
      int yk = y + ki;

      if (table[xk][yk] == '@')
        break;
      if (ans[xk][yk] > dist + 1) {
        q.emplace_back(xk, yk);
        ans[xk][yk] = dist + 1;
      }
    }
  }
  cout << (ans[x2][y2] < INT_MAX / 2 ? ans[x2][y2] : -1) << endl;
}