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

#include <bits/stdc++.h>
using namespace std;

#define Professor

using ll = long long;
using i64 = unsigned long long;

template <typename T> inline T Min(T a, T b, T c) { return min(a, min(b, c)); }
template <typename T> inline T Min(T a, T b, T c, T d) {
  return min(a, min(b, min(c, d)));
}
template <typename T> inline T Max(T a, T b, T c) { return max(a, max(b, c)); }
template <typename T> inline T Max(T a, T b, T c, T d) {
  return max(a, max(b, max(c, d)));
}

template <typename T> inline T Ceil(T a, T b) {
  return ((a % b == 0) ? (a / b) : (a / b + 1));
}
template <typename T> inline T Floor(T a, T b) { return a / b; }

template <typename T> inline T Power(T a, T p) {
  T res = 1, x = a;
  while (p) {
    if (p & 1)
      res = (res * x);
    x = (x * x);
    p >>= 1;
  }
  return res;
}
template <typename T> inline T BigMod(T a, T p, T M) {
  a %= M;
  T ret = (T)1;
  while (p) {
    if (p & 1)
      ret = (ret * a) % M;
    a = (a * a) % M;
    p = p >> 1;
  }
  return ret;
}
template <typename T> inline T InverseMod(T a, T M) {
  return BigMod(a, M - 2, M);
}

template <typename T> inline T gcd(T a, T b) {
  a = abs(a);
  b = abs(b);
  while (b) {
    a = a % b;
    swap(a, b);
  }
  return a;
}
template <typename T> inline T lcm(T x, T y) {
  return (((x) / gcd((x), (y))) * (y));
}
template <typename T> inline T Reverse(T x) {
  reverse(x.begin(), x.end());
  return x;
}

template <typename T1, typename T2, typename T3> struct Pair {
  T1 first;
  T2 second;
  T3 third;
};
template <typename T1, typename T2, typename T3, typename T4> struct Pair4 {
  T1 first;
  T2 second;
  T3 third;
  T4 fourth;
};

using ii = pair<int, int>;
using iii = Pair<int, int, int>;
using iiii = Pair4<int, int, int, int>;

#define endl '\\n'
#define all(x) x.begin(), x.end()
#define all_c(x, c) x.begin(), x.end(), c

const int N = (int)4e5 + 10;
const double EPS = 1e-7;
const double PI = acos(-1.0);
const ll LLINF = (ll)1e18 + 1;
const int INF = (int)1e9 + 1;
const ll MOD = (ll)1e9 + 7;

template <typename T> bool cmp(const pair<T, T> &a, const pair<T, T> &b) {
  return a.first < b.first;
}

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  auto Solve = [&]() {
#ifdef __APPLE__
    cout << "Started Running.....\\n";
#endif
    int h, w, k_max, sx, sy, ex, ey;
    cin >> h >> w >> k_max >> sx >> sy >> ex >> ey;
    --sx, --sy, --ex, --ey;
    vector<vector<char>> grid(h, vector<char>(w));
    for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
        cin >> grid[i][j];
      }
    }
    vector<vector<int>> dist(h, vector<int>(w, -1));
    function<bool(int, int)> isValid = [&](int x, int y) {
      return (x >= 0 && x < h && y >= 0 && y < w);
    };
    function<void(void)> bfs = [&](void) {
      dist[sx][sy] = 0;
      queue<ii> q;
      q.push({sx, sy});
      while (!q.empty()) {
        auto current = q.front();
        q.pop();
        int cx = current.first, cy = current.second;
        for (int i = 0; i < 4; ++i) {
          for (int k = 1; k <= k_max; ++k) {
            int nx = cx + (dx[i] * k);
            int ny = cy + (dy[i] * k);
            if (!isValid(nx, ny) || grid[nx][ny] == '@')
              break;
            if (isValid(nx, ny) && grid[nx][ny] != '@') {
              if (dist[nx][ny] == -1) {
                dist[nx][ny] = dist[cx][cy] + 1;
                q.push({nx, ny});
              } else {
                if (dist[nx][ny] >= dist[cx][cy] + 1) {
                  dist[nx][ny] = dist[cx][cy] + 1;
                } else {
                  break;
                }
              }
            }
          }
        }
      }
    };
    bfs();
    if (dist[ex][ey] != -1) {
      cout << dist[ex][ey] << endl;
    } else {
      cout << "-1\\n";
    }
  };
  Solve();
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <math.h>

using namespace std;
typedef long long ll;
#define M_PI 3.14159265358979323846
const int MOD = 1e9 + 7;

int h, w, k;
int sx, sy, ex, ey;
vector<vector<int>> dist;
vector<vector<char>> grid;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
bool isValid(int x, int y) { return (x >= 0 && x < h && y >= 0 && y < w); }

void bfs() {
  dist[sx][sy] = 0;
  queue<pair<int, int>> q;
  q.push({sx, sy});
  while (!q.empty()) {
    auto curr = q.front();
    q.pop();
    int cx = curr.first;
    int cy = curr.second;
    for (int i = 0; i < 4; i++) {
      for (int z = 1; z <= k; z++) {
        int xx = cx + (dx[i] * z);
        int xy = cy + (dy[i] * z);
        if (!isValid(xx, xy) || grid[xx][xy] == '@')
          break;
        if (isValid(xx, xy) && grid[xx][xy] != '@') {
          if (dist[xx][xy] == -1) {
            dist[xx][xy] = dist[cx][cy] + 1;
            q.push({xx, xy});
          }
        } else {
          if (dist[xx][xy] >= dist[cx][cy] + 1)
            dist[xx][xy] = dist[cx][cy] + 1;
          else
            break;
        }
      }
    }
  }
  return;
}

void solve() {
  cin >> h >> w >> k;
  cin >> sx >> sy >> ex >> ey;
  --sx;
  --sy;
  --ex;
  --ey;
  grid.resize(h, vector<char>(w, 0));
  dist.resize(h, vector<int>(w, -1));
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++)
      cin >> grid[i][j];
  }
  bfs();
  //         for(int i=0;i<h;i++){
  //            for(int j=0;j<w;j++)
  //                cout<<dist[i][j]<<" ";
  //            cout<<"\\n";
  //         }

  if (dist[ex][ey] != -1)
    cout << dist[ex][ey] << "\\n";
  else
    cout << -1 << "\\n";
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << fixed;
  cout << setprecision(10);
  int t = 1;
  //        cin>>t;
  for (int i = 1; i <= t; i++) {
    solve();
  }
  return 0;
}