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

#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using LD = long double;
using PII = pair<int, int>;
using PLI = pair<long long, int>;
using PLL = pair<long long, long long>;
using TI = tuple<int, int, int>;
mt19937 mrand(random_device{}());
int rnd(int x) { return mrand() % x; }
template <class A, class B>
ostream &operator<<(ostream &out, const pair<A, B> &p) {
  return out << "(" << p.first << ", " << p.second << ")";
}
template <typename T, size_t N>
ostream &operator<<(ostream &out, const array<T, N> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
template <typename T> ostream &operator<<(ostream &out, const vector<T> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
template <typename T> ostream &operator<<(ostream &out, const deque<T> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
template <typename T, class Cmp>
ostream &operator<<(ostream &out, const set<T, Cmp> &a) {
  out << "{";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "}";
  return out;
}
template <typename U, typename T, class Cmp>
ostream &operator<<(ostream &out, const map<U, T, Cmp> &a) {
  out << "{";
  bool first = true;
  for (auto &p : a) {
    out << (first ? "" : ", ");
    out << p.first << ":" << p.second;
    first = 0;
  }
  out << "}";
  return out;
}
template <class T> inline void YES(T condition) {
  if (condition)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}
template <class T> inline void Yes(T condition) {
  if (condition)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}
template <class T> T gcd(T a, T b) {
  while (b) {
    T tmp = b;
    b = a % b;
    a = tmp;
  }
  return a;
}
template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }

#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}

long long qpow(long long a, long long b, int MOD) {
  if (b == 0)
    return 1;
  long long res = 1;
  while (b) {
    if (b & 1)
      res = (res * a) % MOD;
    a = (a * a) % MOD;
    b >>= 1;
  }
  return res;
}

const int MOD = 1e9 + 7;
const int maxn = 2e6;
const int INF = 1 << 30;
int fac[maxn + 5];
int invfac[maxn + 5];
void init() {
  fac[0] = 1;
  for (int i = 1; i <= maxn; i++)
    fac[i] = 1LL * fac[i - 1] * i % MOD;
  invfac[maxn] = (int)(qpow(fac[maxn], MOD - 2, MOD));
  for (int i = maxn - 1; i >= 0; i--)
    invfac[i] = 1LL * invfac[i + 1] * (i + 1) % MOD;
}
int C(int n, int k) {
  assert(0 <= k && n >= k);
  int tmp = (int)(1LL * invfac[k] * invfac[n - k] % MOD);
  return (int)(1LL * fac[n] * tmp % MOD);
}

// struct Node {
//     int x, y, pdi, step, pd;
// };
// bool operator>(Node& a, Node& b) {
//     return a.pd < b.pd;
// }
struct Node {
  int x, y, pdi, step, pd;

  Node(int xx, int yy, int pdii, int stepp, int pdd) {
    x = xx;
    y = yy;
    pdi = pdii;
    step = stepp;
    pd = pdd;
  }

  bool operator<(const Node &other) const { return pd > other.pd; }
};

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int n, m, k;
  cin >> n >> m >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;
  vector<string> mat(n);
  for (int i = 0; i < n; i++) {
    cin >> mat[i];
  }

  vector<vector<int>> d(n, vector<int>(m, INF));
  d[x1][y1] = 0;
  queue<PII> q;
  q.emplace(x1, y1);
  while (!q.empty()) {
    PII p = q.front();
    q.pop();
    int x = p.first, y = p.second;
    for (int di = 0; di < 4; di++) {
      int ncost = d[x][y] + 1;
      for (int i = 1; i <= k; i++) {
        int nx = x + dx[di] * i, ny = y + dy[di] * i;
        if (nx < 0 || nx >= n || ny < 0 || ny >= m)
          break;
        if (mat[nx][ny] == '@')
          break;
        if (d[nx][ny] > ncost) {
          d[nx][ny] = ncost;
          q.emplace(nx, ny);
        } else if (d[nx][ny] == ncost)
          continue;
        else
          break;
      }
    }
  }

  // trace(d);
  cout << (d[x2][y2] == INF ? -1 : d[x2][y2]) << '\\n';

  // Dijkstra
  // vector<vector<vector<int>>> d(n, vector<vector<int>>(m, vector<int>(4,
  // INF))); vector<vector<vector<int>>> vis(n, vector<vector<int>>(m,
  // vector<int>(4, 0))); priority_queue<Node> q; for (int di = 0; di < 4; di++)
  // {
  //     vis[x1][y1][di] = 1;
  //     int nx = x1 + dx[di], ny = y1 + dy[di];
  //     if (nx < 0 || nx >= n || ny < 0 || ny >= m) continue;
  //     if (mat[nx][ny] == '@') continue;
  //     Node tmp(nx, ny, di, 1, 1);
  //     q.push(tmp);
  //     vis[nx][ny][di] = 1;
  //     d[nx][ny][di] = 1;
  // }

  // bool ok = false;
  // int ans = INF;
  // while (!q.empty()) {
  //     Node tmp = q.top(); q.pop();
  //     int x = tmp.x, y = tmp.y, pdi = tmp.pdi, step = tmp.step, pd = tmp.pd;
  //     // trace(x, y, pdi, step, pd);
  //     if (pd >= ans) break;
  //     for (int di = 0; di < 4; di++) {
  //         int nx = x + dx[di], ny = y + dy[di];
  //         if (nx < 0 || nx >= n || ny < 0 || ny >= m) continue;
  //         if (mat[nx][ny] == '@') continue;
  //         if (vis[nx][ny][di]) continue;
  //         vis[nx][ny][di] = 1;
  //         int add = (step == k ? 1 : (pdi == di ? 0 : 1));
  //         int nd = pd + add;
  //         if (nx == x2 && ny == y2 && ans > nd) {
  //             ok = true;
  //             ans = nd;
  //             continue;
  //         }
  //         Node tmp2(nx, ny, di, (step == k ? 1 : (pdi == di ? step + 1 : 1)),
  //         nd); q.push(tmp2);
  //     }
  // }

  // if (!ok) {cout << -1 << '\\n';}
  // else cout << ans << '\\n';

  // deque<pair<PII, PII>> q;

  // vector<vector<vector<int>>> d(n, vector<vector<int>>(m, vector<int>(4,
  // INF))); for (int di = 0; di < 4; di++) d[x1][y1][di] = 0; for (int di = 0;
  // di < 4; di++) {
  //     int nx1 = x1 + dx[di], ny1 = y1 + dy[di];
  //     if (nx1 < 0 || nx1 >= n || ny1 < 0 || ny1 >= m) continue;
  //     if (mat[nx1][ny1] == '@') continue;
  //     q.emplace_back(make_pair(nx1, ny1), make_pair(di, 1));
  //     d[nx1][ny1][di] = 1;
  // }

  // while (!q.empty()) {
  //     auto p = q.front(); q.pop_front();
  //     PII xy = p.first, did = p.second;
  //     int x = xy.first, y = xy.second, pdi = did.first, pd = did.second;
  //     for (int di = 0; di < 4; di++) {
  //         int nx = x + dx[di], ny = y + dy[di];
  //         if (nx < 0 || nx >= n || ny < 0 || ny >= m) continue;
  //         if (mat[nx][ny] == '@') continue;
  //         int pred = d[x][y][pdi];
  //         int add = (pd == k) ? 1 : ((pdi == di) ? 0 : 1);
  //         if (d[nx][ny][di] > pred + add) {
  //             d[nx][ny][di] = pred + add;
  //             q.emplace_back(make_pair(nx, ny), make_pair(di, (pd == k) ? 1 :
  //             (pdi == di ? pd + 1 : 1)));
  //         }
  //     }
  // }

  // // trace(d);
  // int ans = INF;
  // for (int di = 0; di < 4; di++) ans = min(ans, d[x2][y2][di]);
  // cout << (ans == INF ? -1 : ans) << '\\n';

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using LD = long double;
using PII = pair<int, int>;
using PLI = pair<long long, int>;
using PLL = pair<long long, long long>;
using TI = tuple<int, int, int>;
mt19937 mrand(random_device{}());
int rnd(int x) { return mrand() % x; }
template <class A, class B>
ostream &operator<<(ostream &out, const pair<A, B> &p) {
  return out << "(" << p.first << ", " << p.second << ")";
}
template <typename T, size_t N>
ostream &operator<<(ostream &out, const array<T, N> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
template <typename T> ostream &operator<<(ostream &out, const vector<T> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
template <typename T> ostream &operator<<(ostream &out, const deque<T> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
template <typename T, class Cmp>
ostream &operator<<(ostream &out, const set<T, Cmp> &a) {
  out << "{";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "}";
  return out;
}
template <typename U, typename T, class Cmp>
ostream &operator<<(ostream &out, const map<U, T, Cmp> &a) {
  out << "{";
  bool first = true;
  for (auto &p : a) {
    out << (first ? "" : ", ");
    out << p.first << ":" << p.second;
    first = 0;
  }
  out << "}";
  return out;
}
template <class T> inline void YES(T condition) {
  if (condition)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}
template <class T> inline void Yes(T condition) {
  if (condition)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}
template <class T> T gcd(T a, T b) {
  while (b) {
    T tmp = b;
    b = a % b;
    a = tmp;
  }
  return a;
}
template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }

#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}

long long qpow(long long a, long long b, int MOD) {
  if (b == 0)
    return 1;
  long long res = 1;
  while (b) {
    if (b & 1)
      res = (res * a) % MOD;
    a = (a * a) % MOD;
    b >>= 1;
  }
  return res;
}

const int MOD = 1e9 + 7;
const int maxn = 2e6;
const int INF = 1 << 30;
int fac[maxn + 5];
int invfac[maxn + 5];
void init() {
  fac[0] = 1;
  for (int i = 1; i <= maxn; i++)
    fac[i] = 1LL * fac[i - 1] * i % MOD;
  invfac[maxn] = (int)(qpow(fac[maxn], MOD - 2, MOD));
  for (int i = maxn - 1; i >= 0; i--)
    invfac[i] = 1LL * invfac[i + 1] * (i + 1) % MOD;
}
int C(int n, int k) {
  assert(0 <= k && n >= k);
  int tmp = (int)(1LL * invfac[k] * invfac[n - k] % MOD);
  return (int)(1LL * fac[n] * tmp % MOD);
}

// struct Node {
//     int x, y, pdi, step, pd;
// };
// bool operator>(Node& a, Node& b) {
//     return a.pd < b.pd;
// }
struct Node {
  int x, y, pdi, step, pd;

  Node(int xx, int yy, int pdii, int stepp, int pdd) {
    x = xx;
    y = yy;
    pdi = pdii;
    step = stepp;
    pd = pdd;
  }

  bool operator<(const Node &other) const { return pd > other.pd; }
};

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int n, m, k;
  cin >> n >> m >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;
  vector<string> mat(n);
  for (int i = 0; i < n; i++) {
    cin >> mat[i];
  }

  vector<vector<int>> d(n, vector<int>(m, INF));
  d[x1][y1] = 0;
  queue<PII> q;
  q.emplace(x1, y1);
  while (!q.empty()) {
    PII p = q.front();
    q.pop();
    int x = p.first, y = p.second;
    for (int di = 0; di < 4; di++) {
      int ncost = d[x][y] + 1;
      for (int i = 1; i <= k; i++) {
        int nx = x + dx[di] * i, ny = y + dy[di] * i;
        if (nx < 0 || nx >= n || ny < 0 || ny >= m)
          break;
        if (mat[nx][ny] == '@')
          break;
        if (d[nx][ny] > ncost) {
          d[nx][ny] = ncost;
          q.emplace(nx, ny);
        }
      }
    }
  }

  // trace(d);
  cout << (d[x2][y2] == INF ? -1 : d[x2][y2]) << '\\n';

  // Dijkstra
  // vector<vector<vector<int>>> d(n, vector<vector<int>>(m, vector<int>(4,
  // INF))); vector<vector<vector<int>>> vis(n, vector<vector<int>>(m,
  // vector<int>(4, 0))); priority_queue<Node> q; for (int di = 0; di < 4; di++)
  // {
  //     vis[x1][y1][di] = 1;
  //     int nx = x1 + dx[di], ny = y1 + dy[di];
  //     if (nx < 0 || nx >= n || ny < 0 || ny >= m) continue;
  //     if (mat[nx][ny] == '@') continue;
  //     Node tmp(nx, ny, di, 1, 1);
  //     q.push(tmp);
  //     vis[nx][ny][di] = 1;
  //     d[nx][ny][di] = 1;
  // }

  // bool ok = false;
  // int ans = INF;
  // while (!q.empty()) {
  //     Node tmp = q.top(); q.pop();
  //     int x = tmp.x, y = tmp.y, pdi = tmp.pdi, step = tmp.step, pd = tmp.pd;
  //     // trace(x, y, pdi, step, pd);
  //     if (pd >= ans) break;
  //     for (int di = 0; di < 4; di++) {
  //         int nx = x + dx[di], ny = y + dy[di];
  //         if (nx < 0 || nx >= n || ny < 0 || ny >= m) continue;
  //         if (mat[nx][ny] == '@') continue;
  //         if (vis[nx][ny][di]) continue;
  //         vis[nx][ny][di] = 1;
  //         int add = (step == k ? 1 : (pdi == di ? 0 : 1));
  //         int nd = pd + add;
  //         if (nx == x2 && ny == y2 && ans > nd) {
  //             ok = true;
  //             ans = nd;
  //             continue;
  //         }
  //         Node tmp2(nx, ny, di, (step == k ? 1 : (pdi == di ? step + 1 : 1)),
  //         nd); q.push(tmp2);
  //     }
  // }

  // if (!ok) {cout << -1 << '\\n';}
  // else cout << ans << '\\n';

  // deque<pair<PII, PII>> q;

  // vector<vector<vector<int>>> d(n, vector<vector<int>>(m, vector<int>(4,
  // INF))); for (int di = 0; di < 4; di++) d[x1][y1][di] = 0; for (int di = 0;
  // di < 4; di++) {
  //     int nx1 = x1 + dx[di], ny1 = y1 + dy[di];
  //     if (nx1 < 0 || nx1 >= n || ny1 < 0 || ny1 >= m) continue;
  //     if (mat[nx1][ny1] == '@') continue;
  //     q.emplace_back(make_pair(nx1, ny1), make_pair(di, 1));
  //     d[nx1][ny1][di] = 1;
  // }

  // while (!q.empty()) {
  //     auto p = q.front(); q.pop_front();
  //     PII xy = p.first, did = p.second;
  //     int x = xy.first, y = xy.second, pdi = did.first, pd = did.second;
  //     for (int di = 0; di < 4; di++) {
  //         int nx = x + dx[di], ny = y + dy[di];
  //         if (nx < 0 || nx >= n || ny < 0 || ny >= m) continue;
  //         if (mat[nx][ny] == '@') continue;
  //         int pred = d[x][y][pdi];
  //         int add = (pd == k) ? 1 : ((pdi == di) ? 0 : 1);
  //         if (d[nx][ny][di] > pred + add) {
  //             d[nx][ny][di] = pred + add;
  //             q.emplace_back(make_pair(nx, ny), make_pair(di, (pd == k) ? 1 :
  //             (pdi == di ? pd + 1 : 1)));
  //         }
  //     }
  // }

  // // trace(d);
  // int ans = INF;
  // for (int di = 0; di < 4; di++) ans = min(ans, d[x2][y2][di]);
  // cout << (ans == INF ? -1 : ans) << '\\n';

  return 0;
}