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

#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
using ld = long double;
using vll = vector<ll>;
using vvll = vector<vll>;
using vc = vector<char>;
using vvc = vector<vc>;
using vb = vector<bool>;
using vvb = vector<vb>;
using pll = pair<ll, ll>;
#define all(v) v.begin(), v.end()
const ll INF = 1e18;
const ll mod = 1e9 + 7;
const double pie = acos(-1);
vll dx4 = {-1, 0, 1, 0};
vll dy4 = {0, -1, 0, 1};
vll dx8 = {-1, 0, 1, 1, 1, 0, -1, -1};
vll dy8 = {-1, -1, -1, 0, 1, 1, 1, 0};

void fix_cout() { cout << fixed << setprecision(20); }
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
void chmax(ll &a, ll b) {
  if (a < b)
    a = b;
}
void chmin(ll &a, ll b) {
  if (a > b)
    a = b;
}

void DFS(vvc &g, vvb &ok, int h, int w) {
  ok.at(h).at(w) = true;
  for (int i = 0; i < 4; i++) {
    ll y = h + dy4.at(i);
    ll x = w + dx4.at(i);
    if (g.at(y).at(x) == '.' && !ok.at(y).at(x))
      DFS(g, ok, y, x);
  }
}

int main() {
  ll h, w, k, sx, sy, gx, gy;
  cin >> h >> w >> k >> sy >> sx >> gy >> gx;
  vvc g(h + 2, vc(w + 2, '@'));
  for (int i = 1; i <= h; i++)
    for (int j = 1; j <= w; j++)
      cin >> g.at(i).at(j);
  vvb ok(h + 2, vb(w + 2, false));
  DFS(g, ok, sy, sx);
  if (ok.at(gy).at(gx)) {
    vector<vvll> cnt(h + 2, vvll(w + 2, vll(4, INF)));
    priority_queue<pair<pll, pll>, vector<pair<pll, pll>>,
                   greater<pair<pll, pll>>>
        pq;
    auto push = [&](ll a, ll b, ll c, ll d) {
      pair<pll, pll> tmp = {{a, b}, {c, d}};
      pq.emplace(tmp);
    };
    push(0, 0, sy, sx);
    while (!pq.empty()) {
      pair<pll, pll> cur = pq.top();
      pq.pop();
      ll y = cur.second.first;
      ll x = cur.second.second;
      ll dir = cur.first.second;
      ll d = cur.first.first;
      if (cnt.at(y).at(x).at(dir) <= d)
        continue;
      cnt.at(y).at(x).at(dir) = d;
      for (int i = 0; i < 4; i++) {
        if (i == dir) {
          y += dy4.at(i);
          x += dx4.at(i);
          if (g.at(y).at(x) == '.' && cnt.at(y).at(x).at(dir) == INF) {
            push(d + 1, dir, y, x);
          }
          y -= dy4.at(i);
          x -= dx4.at(i);
        } else {
          if (cnt.at(y).at(x).at(i) == INF) {
            push((d + k - 1) / k * k, i, y, x);
          }
        }
      }
    }
    ll ans = INF;
    for (int i = 0; i < 4; i++)
      chmin(ans, cnt.at(gy).at(gx).at(i));
    cout << (ans + k - 1) / k << endl;
    // for(int i=1;i<=h;i++){
    // for(int j=1;j<=w;j++){
    // ll sub = INF ;
    // for(int k=0;k<4;k++) chmin(sub,cnt.at(i).at(j).at(k)) ;
    // cout << sub << " " ;
    // }
    // cout << endl ;
    // }
  } else {
    cout << -1 << endl;
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
using ld = long double;
using vll = vector<ll>;
using vvll = vector<vll>;
using vc = vector<char>;
using vvc = vector<vc>;
using vb = vector<bool>;
using vvb = vector<vb>;
using pll = pair<ll, ll>;
#define all(v) v.begin(), v.end()
const ll INF = 1e18;
const ll mod = 1e9 + 7;
const double pie = acos(-1);
vll dx4 = {-1, 0, 1, 0};
vll dy4 = {0, -1, 0, 1};
vll dx8 = {-1, 0, 1, 1, 1, 0, -1, -1};
vll dy8 = {-1, -1, -1, 0, 1, 1, 1, 0};

void fix_cout() { cout << fixed << setprecision(20); }
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
void chmax(ll &a, ll b) {
  if (a < b)
    a = b;
}
void chmin(ll &a, ll b) {
  if (a > b)
    a = b;
}

void DFS(vvc &g, vvb &ok, int h, int w) {
  if (ok.at(h).at(w))
    return;
  if (g.at(h).at(w) == '@')
    return;
  ok.at(h).at(w) = true;
  for (int i = 0; i < 4; i++) {
    ll y = h + dy4.at(i);
    ll x = w + dx4.at(i);
    DFS(g, ok, y, x);
  }
}

int main() {
  ll h, w, k, sx, sy, gx, gy;
  cin >> h >> w >> k >> sy >> sx >> gy >> gx;
  vvc g(h + 2, vc(w + 2, '@'));
  for (int i = 1; i <= h; i++)
    for (int j = 1; j <= w; j++)
      cin >> g.at(i).at(j);
  vvb ok(h + 2, vb(w + 2, false));
  DFS(g, ok, sy, sx);
  if (ok.at(gy).at(gx)) {
    queue<ll> q;
    vvll cnt(h + 2, vll(w + 2, INF));
    cnt.at(sy).at(sx) = 0;
    q.push(sy);
    q.push(sx);
    while (!q.empty()) {
      ll y = q.front();
      q.pop();
      ll x = q.front();
      q.pop();
      ll d = cnt.at(y).at(x);
      for (int i = 1; i <= k; i++) {
        if (g.at(y).at(x + i) == '@')
          break;
        if (cnt.at(y).at(x + i) <= d + 1)
          continue;
        cnt.at(y).at(x + i) = d + 1;
        q.push(y);
        q.push(x + i);
      }
      for (int i = 1; i <= k; i++) {
        if (g.at(y).at(x - i) == '@')
          break;
        if (cnt.at(y).at(x - i) <= d + 1)
          continue;
        cnt.at(y).at(x - i) = d + 1;
        q.push(y);
        q.push(x - i);
      }
      for (int i = 1; i <= k; i++) {
        if (g.at(y + i).at(x) == '@')
          break;
        if (cnt.at(y + i).at(x) <= d + 1)
          continue;
        cnt.at(y + i).at(x) = d + 1;
        q.push(y + i);
        q.push(x);
      }
      for (int i = 1; i <= k; i++) {
        if (g.at(y - i).at(x) == '@')
          break;
        if (cnt.at(y - i).at(x) <= d + 1)
          continue;
        cnt.at(y - i).at(x) = d + 1;
        q.push(y - i);
        q.push(x);
      }
    }
    cout << cnt.at(gy).at(gx) << endl;
  } else {
    cout << -1 << endl;
  }
}