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

// #include <bits/stdc++.h>
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;

#include "boost/multiprecision/cpp_dec_float.hpp"
#include "boost/multiprecision/cpp_int.hpp"
namespace mp = boost::multiprecision;
typedef mp::cpp_int LL;
typedef mp::number<mp::cpp_dec_float<1024>>
    DD; // 仮数部が1024ビットの浮動小数点数型(TLEしたら小さくする)

typedef long double dd;
#define i_7 (ll)(1E9 + 7)
// #define i_7 998244353
#define i_5 i_7 - 2
ll mod(ll a) {
  ll c = a % i_7;
  if (c >= 0)
    return c;
  return c + i_7;
}
typedef pair<ll, ll> l_l;
typedef pair<dd, dd> d_d;
ll inf = (ll)1E16;
#define rep(i, l, r) for (ll i = l; i <= r; i++)
#define rrep(i, r, l) for (ll i = r; i >= l; i--)
#define pb push_back
ll max(ll a, ll b) {
  if (a < b)
    return b;
  else
    return a;
}
ll min(ll a, ll b) {
  if (a > b)
    return b;
  else
    return a;
}
void Max(ll &pos, ll val) { pos = max(pos, val); } // Max(dp[n],dp[n-1]);
void Min(ll &pos, ll val) { pos = min(pos, val); }
void Add(ll &pos, ll val) { pos = mod(pos + val); }
dd EPS = 1E-9;
#define fastio                                                                 \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define fi first
#define se second
#define endl "\\n"
#define SORT(v) sort(v.begin(), v.end())
#define ERASE(v) v.erase(unique(v.begin(), v.end()), v.end())
#define POSL(v, x) (lower_bound(v.begin(), v.end(), x) - v.begin())
#define POSU(v, x) (upper_bound(v.begin(), v.end(), x) - v.begin())
template <class T, class S> inline bool chmax(T &a, S b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T, class S> inline bool chmin(T &a, S b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

void mod_print(ll k) {
  ll P = 1000;
  rep(y, 1, P) {
    ll x = mod(y * k);
    if (x + P >= i_7) {
      x -= i_7;
    }
    if (abs(x) <= P) {
      cout << x << "/" << y << endl;
      return;
    }
  }
  cout << "nun" << endl;
}
#define all(c) c.begin(), c.end()
//////////////////////////

ll h, w, K;
bool valid(ll i, ll j) {
  if (0 <= i && i <= h - 1 && 0 <= j && j <= w - 1)
    return true;
  return false;
}
int main() {
  fastio cin >> h >> w >> K;
  ll x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  x2--;
  y1--;
  y2--;
  string s[h];
  rep(i, 0, h - 1) cin >> s[i];
  ll dp[h][w];
  rep(i, 0, h - 1) rep(j, 0, w - 1) dp[i][j] = inf;
  dp[x1][y1] = 0;
  queue<l_l> q;
  q.push({x1, y1});
  ll kan[h][w][4];
  memset(kan, 0, sizeof(kan));
  rep(i, 0, h - 1) {
    rep(j, 0, w - 1) {
      if (s[i][j] != '.') {
        kan[i][j][0] = 0;
        continue;
      }
      if (i == 0) {
        kan[i][j][0] = 0;
      } else {
        kan[i][j][0] = kan[i - 1][j][0] + 1;
      }
    }
  }
  rrep(i, h - 1, 0) {
    rep(j, 0, w - 1) {
      if (s[i][j] != '.') {
        kan[i][j][1] = 0;
        continue;
      }
      if (i == h - 1) {
        kan[i][j][1] = 0;
      } else {
        kan[i][j][1] = kan[i + 1][j][1] + 1;
      }
    }
  }
  rep(i, 0, h - 1) {
    rep(j, 0, w - 1) {
      if (s[i][j] != '.') {
        kan[i][j][2] = 0;
        continue;
      }
      if (j == 0) {
        kan[i][j][2] = 0;
      } else {
        kan[i][j][2] = kan[i][j - 1][2] + 1;
      }
    }
  }
  rep(i, 0, h - 1) {
    rrep(j, w - 1, 0) {
      if (s[i][j] != '.') {
        kan[i][j][3] = 0;
        continue;
      }
      if (j == w - 1) {
        kan[i][j][3] = 0;
      } else {
        kan[i][j][3] = kan[i][j + 1][3] + 1;
      }
    }
  }
  // rep(z,0,3){rep(i,0,h-1){rep(j,0,w-1)cout<<kan[i][j][z]<<"
  // ";cout<<endl;}cout<<endl;}

  while (!q.empty()) {
    l_l f = q.front();
    q.pop();

    rep(i, 1, K) {
      if (kan[f.fi][f.se][1] < i)
        break;
      ll nx = f.fi + i;
      ll ny = f.se;
      if (valid(nx, ny) && s[nx][ny] == '.') {
        if (chmin(dp[nx][ny], dp[f.fi][f.se] + 1)) {
          q.push({nx, ny});
        } else if (dp[nx][ny] < dp[f.fi][f.se] + 1) {
          break;
        }
      } else {
        break;
      }
    }
    rep(i, 1, K) {
      if (kan[f.fi][f.se][0] < i)
        break;
      ll nx = f.fi - i;
      ll ny = f.se;
      if (valid(nx, ny) && s[nx][ny] == '.') {
        if (chmin(dp[nx][ny], dp[f.fi][f.se] + 1)) {
          q.push({nx, ny});
        } else if (dp[nx][ny] < dp[f.fi][f.se] + 1) {
          break;
        }
      } else {
        break;
      }
    }
    rep(i, 1, K) {
      if (kan[f.fi][f.se][3] < i)
        break;
      ll nx = f.fi;
      ll ny = f.se + i;
      if (valid(nx, ny) && s[nx][ny] == '.') {
        if (chmin(dp[nx][ny], dp[f.fi][f.se] + 1)) {
          q.push({nx, ny});
        } else if (dp[nx][ny] < dp[f.fi][f.se] + 1) {
          break;
        }
      } else {
        break;
      }
    }
    rep(i, 1, K) {
      if (kan[f.fi][f.se][2] < i)
        break;
      ll nx = f.fi;
      ll ny = f.se - i;
      if (valid(nx, ny) && s[nx][ny] == '.') {
        if (chmin(dp[nx][ny], dp[f.fi][f.se] + 1)) {
          q.push({nx, ny});
        } else if (dp[nx][ny] < dp[f.fi][f.se] + 1) {
          break;
        }
      } else {
        break;
      }
    }
  }
  if (dp[x2][y2] >= inf / 2)
    dp[x2][y2] = -1;
  cout << dp[x2][y2] << endl;

  return 0;
}


Error: TLE

Fixed submission:

// #include <bits/stdc++.h>
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;

#include "boost/multiprecision/cpp_dec_float.hpp"
#include "boost/multiprecision/cpp_int.hpp"
namespace mp = boost::multiprecision;
typedef mp::cpp_int LL;
typedef mp::number<mp::cpp_dec_float<1024>>
    DD; // 仮数部が1024ビットの浮動小数点数型(TLEしたら小さくする)

typedef long double dd;
#define i_7 (ll)(1E9 + 7)
// #define i_7 998244353
#define i_5 i_7 - 2
ll mod(ll a) {
  ll c = a % i_7;
  if (c >= 0)
    return c;
  return c + i_7;
}
typedef pair<ll, ll> l_l;
typedef pair<dd, dd> d_d;
ll inf = (ll)1E16;
#define rep(i, l, r) for (ll i = l; i <= r; i++)
#define rrep(i, r, l) for (ll i = r; i >= l; i--)
#define pb push_back
ll max(ll a, ll b) {
  if (a < b)
    return b;
  else
    return a;
}
ll min(ll a, ll b) {
  if (a > b)
    return b;
  else
    return a;
}
void Max(ll &pos, ll val) { pos = max(pos, val); } // Max(dp[n],dp[n-1]);
void Min(ll &pos, ll val) { pos = min(pos, val); }
void Add(ll &pos, ll val) { pos = mod(pos + val); }
dd EPS = 1E-9;
#define fastio                                                                 \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define fi first
#define se second
#define endl "\\n"
#define SORT(v) sort(v.begin(), v.end())
#define ERASE(v) v.erase(unique(v.begin(), v.end()), v.end())
#define POSL(v, x) (lower_bound(v.begin(), v.end(), x) - v.begin())
#define POSU(v, x) (upper_bound(v.begin(), v.end(), x) - v.begin())
template <class T, class S> inline bool chmax(T &a, S b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T, class S> inline bool chmin(T &a, S b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

void mod_print(ll k) {
  ll P = 1000;
  rep(y, 1, P) {
    ll x = mod(y * k);
    if (x + P >= i_7) {
      x -= i_7;
    }
    if (abs(x) <= P) {
      cout << x << "/" << y << endl;
      return;
    }
  }
  cout << "nun" << endl;
}
#define all(c) c.begin(), c.end()
//////////////////////////

ll h, w, k;
bool valid(ll i, ll j) {
  if (0 <= i && i <= h - 1 && 0 <= j && j <= w - 1)
    return true;
  return false;
}

int main() {
  fastio cin >> h >> w >> k;
  ll x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  x2--;
  y1--;
  y2--;
  string s[h];
  rep(i, 0, h - 1) cin >> s[i];
  ll dp[h][w];
  rep(i, 0, h - 1) rep(j, 0, w - 1) dp[i][j] = inf;
  dp[x1][y1] = 0;
  queue<l_l> q;
  q.push({x1, y1});
  while (!q.empty()) {
    l_l f = q.front();
    q.pop();
    rep(i, 1, k) {
      ll nx = f.fi + i;
      ll ny = f.se;
      if (valid(nx, ny) && s[nx][ny] == '.') {
        if (chmin(dp[nx][ny], dp[f.fi][f.se] + 1)) {
          q.push({nx, ny});
        }
      } else {
        break;
      }
    }
    rep(i, 1, k) {
      ll nx = f.fi - i;
      ll ny = f.se;
      if (valid(nx, ny) && s[nx][ny] == '.') {
        if (chmin(dp[nx][ny], dp[f.fi][f.se] + 1)) {
          q.push({nx, ny});
        }
      } else {
        break;
      }
    }
    rep(i, 1, k) {
      ll nx = f.fi;
      ll ny = f.se + i;
      if (valid(nx, ny) && s[nx][ny] == '.') {
        if (chmin(dp[nx][ny], dp[f.fi][f.se] + 1)) {
          q.push({nx, ny});
        }
      } else {
        break;
      }
    }
    rep(i, 1, k) {
      ll nx = f.fi;
      ll ny = f.se - i;
      if (valid(nx, ny) && s[nx][ny] == '.') {
        if (chmin(dp[nx][ny], dp[f.fi][f.se] + 1)) {
          q.push({nx, ny});
        }
      } else {
        break;
      }
    }
  }
  if (dp[x2][y2] >= inf / 2)
    dp[x2][y2] = -1;
  cout << dp[x2][y2] << endl;

  return 0;
}