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

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

#define ll long long
#define inf 0x3f3f3f3f
#define infll 0x3f3f3f3f3f3f3f3f
#define all(x) (x).begin(), (x).end()
#define forn(i, a, b) for (int i = a; i < (signed)(b); ++i)
#define pb push_back

// debug templates
#define debug(x) cerr << #x << " : " << x << endl;
#define debuga(A, N)                                                           \\
  cerr << #A << " : [";                                                        \\
  for (int i = 0; i < N; i++)                                                  \\
    cerr << A[i] << " ";                                                       \\
  cerr << "]\\n";
#define debuga2(A, N, M)                                                       \\
  cerr << #A << " : \\n";                                                       \\
  for (int i = 0; i < N; i++) {                                                \\
    cerr << "[";                                                               \\
    for (int j = 0; j < M; ++j)                                                \\
      cerr << A[i][j] << " ";                                                  \\
    cerr << "]\\n";                                                             \\
  }
#define debugp(p)                                                              \\
  cerr << #p << " : "                                                          \\
       << "(" << (p).first << "," << (p).second << ")\\n";
#define debugv(v)                                                              \\
  cerr << #v << " : "                                                          \\
       << "[";                                                                 \\
  for (int i = 0; i < (v).size(); i++)                                         \\
    cerr << v[i] << " ";                                                       \\
  cerr << "]\\n";
#define debugv2(v)                                                             \\
  cerr << #v << " : \\n";                                                       \\
  for (int i = 0; i < v.size(); i++) {                                         \\
    cerr << "[";                                                               \\
    for (int j = 0; j < (v[0].size()); ++j)                                    \\
      cerr << v[i][j] << " ";                                                  \\
    cerr << "]\\n";                                                             \\
  }
#define debugs(m)                                                              \\
  cerr << #m << " : [ ";                                                       \\
  for (auto itr = m.begin(); itr != m.end(); itr++)                            \\
    cerr << *itr << " ";                                                       \\
  cerr << "]\\n";
#define debugm(m)                                                              \\
  cerr << #m << " : [ ";                                                       \\
  for (auto itr = m.begin(); itr != m.end(); itr++)                            \\
    cerr << "(" << itr->first << "," << itr->second << ") ";                   \\
  cerr << "]\\n";

// dijstra's algo
void solve() {
  int dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, -1, 1};
  int h, w, k;
  cin >> h >> w >> k;
  int sx, sy, tx, ty;
  cin >> sx >> sy >> tx >> ty;
  --sx;
  --sy;
  --tx;
  --ty;
  vector<string> s(h);
  forn(i, 0, h) cin >> s[i];

  queue<pair<int, pair<int, int>>> q;
  q.push({0, {sx, sy}});
  int dist[h][w];
  memset(dist, inf, sizeof(dist));
  dist[sx][sy] = 0;
  while (q.size() > 0) {
    auto temp = q.front();
    q.pop();
    int cost = temp.first;
    int x = temp.second.first, y = temp.second.second;
    forn(i, 0, 4) {
      forn(j, 1, k + 1) {
        int nx = x + dx[i] * j, ny = y + dy[i] * j;
        if (nx < 0 || ny < 0 || nx >= h || ny >= w)
          break;
        int cur_cost = cost + 1;
        if (s[nx][ny] == '@')
          break;
        if (dist[nx][ny] > cur_cost) {
          dist[nx][ny] = cur_cost;
          q.push({cur_cost, {nx, ny}});
        }
        if (dist[nx][ny] < cur_cost)
          break;
      }
    }
  }
  cout << (dist[tx][ty] == inf ? -1 : dist[tx][ty]);
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  solve();
  return 0;
}


Error: TLE

Fixed submission:

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

#define ll long long
#define inf 0x3f3f3f3f
#define infll 0x3f3f3f3f3f3f3f3f
#define all(x) (x).begin(), (x).end()
#define forn(i, a, b) for (int i = a; i < (signed)(b); ++i)
#define pb push_back

// debug templates
#define debug(x) cerr << #x << " : " << x << endl;
#define debuga(A, N)                                                           \\
  cerr << #A << " : [";                                                        \\
  for (int i = 0; i < N; i++)                                                  \\
    cerr << A[i] << " ";                                                       \\
  cerr << "]\\n";
#define debuga2(A, N, M)                                                       \\
  cerr << #A << " : \\n";                                                       \\
  for (int i = 0; i < N; i++) {                                                \\
    cerr << "[";                                                               \\
    for (int j = 0; j < M; ++j)                                                \\
      cerr << A[i][j] << " ";                                                  \\
    cerr << "]\\n";                                                             \\
  }
#define debugp(p)                                                              \\
  cerr << #p << " : "                                                          \\
       << "(" << (p).first << "," << (p).second << ")\\n";
#define debugv(v)                                                              \\
  cerr << #v << " : "                                                          \\
       << "[";                                                                 \\
  for (int i = 0; i < (v).size(); i++)                                         \\
    cerr << v[i] << " ";                                                       \\
  cerr << "]\\n";
#define debugv2(v)                                                             \\
  cerr << #v << " : \\n";                                                       \\
  for (int i = 0; i < v.size(); i++) {                                         \\
    cerr << "[";                                                               \\
    for (int j = 0; j < (v[0].size()); ++j)                                    \\
      cerr << v[i][j] << " ";                                                  \\
    cerr << "]\\n";                                                             \\
  }
#define debugs(m)                                                              \\
  cerr << #m << " : [ ";                                                       \\
  for (auto itr = m.begin(); itr != m.end(); itr++)                            \\
    cerr << *itr << " ";                                                       \\
  cerr << "]\\n";
#define debugm(m)                                                              \\
  cerr << #m << " : [ ";                                                       \\
  for (auto itr = m.begin(); itr != m.end(); itr++)                            \\
    cerr << "(" << itr->first << "," << itr->second << ") ";                   \\
  cerr << "]\\n";

// dijstra's algo
void solve() {
  int dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, -1, 1};
  int h, w, k;
  cin >> h >> w >> k;
  int sx, sy, tx, ty;
  cin >> sx >> sy >> tx >> ty;
  --sx;
  --sy;
  --tx;
  --ty;
  vector<string> s(h);
  forn(i, 0, h) cin >> s[i];

  queue<pair<int, pair<int, int>>> q;
  q.push({0, {sx, sy}});
  int dist[h][w];
  memset(dist, inf, sizeof(dist));
  dist[sx][sy] = 0;
  while (q.size() > 0) {
    auto temp = q.front();
    q.pop();
    int cost = temp.first;
    int x = temp.second.first, y = temp.second.second;
    forn(i, 0, 4) {
      forn(j, 1, k + 1) {
        int nx = x + dx[i] * j, ny = y + dy[i] * j;
        if (nx < 0 || ny < 0 || nx >= h || ny >= w)
          break;
        int cur_cost = cost + 1;
        if (s[nx][ny] == '@')
          break;
        if (dist[nx][ny] > cur_cost) {
          dist[nx][ny] = cur_cost;
          q.push({cur_cost, {nx, ny}});
        }
      }
    }
  }
  cout << (dist[tx][ty] == inf ? -1 : dist[tx][ty]);
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  solve();
  return 0;
}