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

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstring> //memset(dp,0,sizeof(dp))
#include <functional>
#include <iomanip>
#include <iostream>
#include <locale>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define ll long long
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define fi first
#define se second
#define pb push_back
#define ALL(a) (a).begin(), (a).end()
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;

#define fout(num) cout << fixed << setprecision(20) << (num) << endl
// s[i]=tolower(s[i]); islower(s[i]); cout << tolower(s[i])はバグ
constexpr int INF = 1e7;
signed main() {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  int h, w, k;
  cin >> h >> w >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  x2--;
  y1--;
  y2--;
  char c[h][w];
  rep(i, h) {
    string s;
    cin >> s;
    rep(j, w) { c[i][j] = s[j]; }
  }
  int ok[h][w];
  rep(i, h) rep(j, w) ok[i][j] = 0;
  queue<P> q2;
  q2.push({x1, y1});
  ok[x1][y1] = 1;
  while (!q2.empty()) {
    P fr = q2.front();
    q2.pop();
    if (fr.fi + 1 < h && ok[fr.fi + 1][fr.se] == 0 &&
        c[fr.fi + 1][fr.se] != '@') {
      ok[fr.fi + 1][fr.se] = 1;
      q2.push({fr.fi + 1, fr.se});
    }
    if (0 <= fr.fi - 1 && ok[fr.fi - 1][fr.se] == 0 &&
        c[fr.fi - 1][fr.se] != '@') {
      ok[fr.fi - 1][fr.se] = 1;
      q2.push({fr.fi - 1, fr.se});
    }
    if (fr.se + 1 < w && ok[fr.fi][fr.se + 1] == 0 &&
        c[fr.fi][fr.se + 1] != '@') {
      ok[fr.fi][fr.se + 1] = 1;
      q2.push({fr.fi, fr.se + 1});
    }
    if (0 <= fr.se - 1 && ok[fr.fi][fr.se - 1] == 0 &&
        c[fr.fi][fr.se - 1] != '@') {
      ok[fr.fi][fr.se - 1] = 1;
      q2.push({fr.fi, fr.se - 1});
    }
  }
  if (!ok[x2][y2]) {
    cout << -1 << endl;
    return 0;
  }

  int dist[h][w];
  rep(i, h) rep(j, w) dist[i][j] = INF;
  queue<P> q;
  q.push({x1, y1});
  dist[x1][y1] = 0;
  while (!q.empty()) {
    P fr = q.front();
    q.pop();
    int ds = dist[fr.fi][fr.se];
    if (ds >= dist[x2][y2])
      break;
    for (int i = 1; i <= min(k, h - fr.fi - 1); i++) {
      if (ds >= dist[fr.fi + i][fr.se] || c[fr.fi + i][fr.se] == '@')
        break;
      if (dist[fr.fi + i][fr.se] > ds + 1) {
        dist[fr.fi + i][fr.se] = ds + 1;
        q.push({fr.fi + i, fr.se});
      }
    }
    for (int i = 1; i <= min(k, w - fr.se - 1); i++) {
      if (ds >= dist[fr.fi][fr.se + i] || c[fr.fi][fr.se + i] == '@')
        break;
      if (dist[fr.fi][fr.se + i] > ds + 1) {
        dist[fr.fi][fr.se + i] = ds + 1;
        q.push({fr.fi, fr.se + i});
      }
    }
    for (int i = 1; i <= min(k, fr.fi); i++) {
      if (ds >= dist[fr.fi - i][fr.se] || c[fr.fi - i][fr.se] == '@')
        break;
      if (dist[fr.fi - i][fr.se] > ds + 1) {
        dist[fr.fi - i][fr.se] = ds + 1;
        q.push({fr.fi - i, fr.se});
      }
    }
    for (int i = 1; i <= min(k, fr.se); i++) {
      if (ds >= dist[fr.fi][fr.se - i] || c[fr.fi][fr.se - i] == '@')
        break;
      if (dist[fr.fi][fr.se - i] > ds + 1) {
        dist[fr.fi][fr.se - i] = ds + 1;
        q.push({fr.fi, fr.se - i});
      }
    }
  }
  cout << dist[x2][y2] << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstring> //memset(dp,0,sizeof(dp))
#include <functional>
#include <iomanip>
#include <iostream>
#include <locale>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define ll long long
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define fi first
#define se second
#define pb push_back
#define ALL(a) (a).begin(), (a).end()
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;

#define fout(num) cout << fixed << setprecision(20) << (num) << endl
// s[i]=tolower(s[i]); islower(s[i]); cout << tolower(s[i])はバグ
constexpr int INF = 1e7;
signed main() {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  int h, w, k;
  cin >> h >> w >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  x2--;
  y1--;
  y2--;
  char c[h][w];
  rep(i, h) {
    string s;
    cin >> s;
    rep(j, w) { c[i][j] = s[j]; }
  }
  int ok[h][w];
  rep(i, h) rep(j, w) ok[i][j] = 0;
  queue<P> q2;
  q2.push({x1, y1});
  ok[x1][y1] = 1;
  while (!q2.empty()) {
    P fr = q2.front();
    q2.pop();
    if (fr.fi + 1 < h && ok[fr.fi + 1][fr.se] == 0 &&
        c[fr.fi + 1][fr.se] != '@') {
      ok[fr.fi + 1][fr.se] = 1;
      q2.push({fr.fi + 1, fr.se});
    }
    if (0 <= fr.fi - 1 && ok[fr.fi - 1][fr.se] == 0 &&
        c[fr.fi - 1][fr.se] != '@') {
      ok[fr.fi - 1][fr.se] = 1;
      q2.push({fr.fi - 1, fr.se});
    }
    if (fr.se + 1 < w && ok[fr.fi][fr.se + 1] == 0 &&
        c[fr.fi][fr.se + 1] != '@') {
      ok[fr.fi][fr.se + 1] = 1;
      q2.push({fr.fi, fr.se + 1});
    }
    if (0 <= fr.se - 1 && ok[fr.fi][fr.se - 1] == 0 &&
        c[fr.fi][fr.se - 1] != '@') {
      ok[fr.fi][fr.se - 1] = 1;
      q2.push({fr.fi, fr.se - 1});
    }
  }
  if (!ok[x2][y2]) {
    cout << -1 << endl;
    return 0;
  }

  int dist[h][w];
  rep(i, h) rep(j, w) dist[i][j] = INF;
  queue<P> q;
  q.push({x1, y1});
  dist[x1][y1] = 0;
  while (!q.empty()) {
    P fr = q.front();
    q.pop();
    int ds = dist[fr.fi][fr.se];
    if (ds >= dist[x2][y2])
      break;
    for (int i = 1; i <= min(k, h - fr.fi - 1); i++) {
      if (ds >= dist[fr.fi + i][fr.se] || c[fr.fi + i][fr.se] == '@')
        break;
      dist[fr.fi + i][fr.se] = ds + 1;
      q.push({fr.fi + i, fr.se});
    }
    for (int i = 1; i <= min(k, w - fr.se - 1); i++) {
      if (ds >= dist[fr.fi][fr.se + i] || c[fr.fi][fr.se + i] == '@')
        break;
      dist[fr.fi][fr.se + i] = ds + 1;
      q.push({fr.fi, fr.se + i});
    }
    for (int i = 1; i <= min(k, fr.fi); i++) {
      if (ds >= dist[fr.fi - i][fr.se] || c[fr.fi - i][fr.se] == '@')
        break;
      dist[fr.fi - i][fr.se] = ds + 1;
      q.push({fr.fi - i, fr.se});
    }
    for (int i = 1; i <= min(k, fr.se); i++) {
      if (ds >= dist[fr.fi][fr.se - i] || c[fr.fi][fr.se - i] == '@')
        break;
      dist[fr.fi][fr.se - i] = ds + 1;
      q.push({fr.fi, fr.se - i});
    }
  }
  cout << dist[x2][y2] << endl;
}