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

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define rep(i, s, f) for (i = s; i < f; i++)
#define print(v)                                                               \\
  for (auto &z : v)                                                            \\
    cout << z << ' ';                                                          \\
  cout << '\\n'
#define db cout << "db: "
#define pb push_back
#define pii pair<int, int>
#define F first
#define S second
#define B begin()
#define E end()
#define all(v) v.B, v.E
#define sz(v) (int)((v).size())
#define vi vector<int>
#define vii vector<pair<int, int>>
#define boost                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define clk1 clock_t start_time = clock()
#define clk2 cout << (clock() - start_time) / (double)CLOCKS_PER_SEC
#define clean(arr) memset(arr, 0, sizeof(arr))
#define mod 1000000007
#define mod2 998244353
#define space 100005
//<<fixed << setprecision(9)
pii mov[4] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int main() {
  int n, i, j, t;
  boost;

  int r, c, k;
  cin >> r >> c >> k;

  pii s, f;
  cin >> s.F >> s.S >> f.F >> f.S;
  s.F--;
  s.S--;
  f.F--;
  f.S--;

  bool arr[r][c];
  clean(arr);
  rep(i, 0, r) {
    string s;
    cin >> s;
    rep(j, 0, c) if (s[j] == '@') arr[i][j] = 1;
  }
  // bool vis[r][c];
  // clean(vis);
  int best[r][c];

  rep(i, 0, r) { rep(j, 0, c) best[i][j] = 1e9; }
  priority_queue<pair<int, pii>> q;
  pair<int, pii> u;
  int x, y;
  q.push({0, s});
  // vis[s.F][s.S]=1;
  best[s.F][s.S] = 0;

  while (!q.empty()) {
    u = q.top();
    // cout<<u.F<<' '<<u.S<<'\\n';
    q.pop();
    if (-u.F > best[u.S.F][u.S.S])
      continue;
    for (auto &z : mov) {
      j = k;
      x = u.S.F;
      y = u.S.S;
      while (j--) {
        x += z.F;
        y += z.S;
        if (!(x >= 0 and y >= 0 and x < r and y < c and !arr[x][y] and
              best[x][y] >= 1 - u.F))
          break;
        if (best[x][y] == 1 - u.F)
          continue;
        best[x][y] = 1 - u.F;
        q.push({-best[x][y], {x, y}});
      }
    }
  }
  // rep(i,0,r)
  // {
  //     rep(j,0,c)cout<<best[i][j]<<' ';
  //     cout<<'\\n';
  // }
  if (best[f.F][f.S] == 1e9)
    best[f.F][f.S] = -1;
  cout << best[f.F][f.S];

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define rep(i, s, f) for (i = s; i < f; i++)
#define print(v)                                                               \\
  for (auto &z : v)                                                            \\
    cout << z << ' ';                                                          \\
  cout << '\\n'
#define db cout << "db: "
#define pb push_back
#define pii pair<int, int>
#define F first
#define S second
#define B begin()
#define E end()
#define all(v) v.B, v.E
#define sz(v) (int)((v).size())
#define vi vector<int>
#define vii vector<pair<int, int>>
#define boost                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define clk1 clock_t start_time = clock()
#define clk2 cout << (clock() - start_time) / (double)CLOCKS_PER_SEC
#define clean(arr) memset(arr, 0, sizeof(arr))
#define mod 1000000007
#define mod2 998244353
#define space 100005
//<<fixed << setprecision(9)
pii mov[4] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int main() {
  int n, i, j, t;
  boost;

  int r, c, k;
  cin >> r >> c >> k;

  pii s, f;
  cin >> s.F >> s.S >> f.F >> f.S;
  s.F--;
  s.S--;
  f.F--;
  f.S--;

  bool arr[r][c];
  clean(arr);
  rep(i, 0, r) {
    string s;
    cin >> s;
    rep(j, 0, c) if (s[j] == '@') arr[i][j] = 1;
  }
  // bool vis[r][c];
  // clean(vis);
  int best[r][c];

  rep(i, 0, r) { rep(j, 0, c) best[i][j] = 1e9; }
  priority_queue<pair<int, pii>> q;
  pair<int, pii> u;
  int x, y;
  q.push({0, s});
  // vis[s.F][s.S]=1;
  best[s.F][s.S] = 0;

  while (!q.empty()) {
    u = q.top();
    // cout<<u.F<<' '<<u.S<<'\\n';
    q.pop();
    if (-u.F > best[u.S.F][u.S.S])
      continue;
    for (auto &z : mov) {
      j = k;
      x = u.S.F;
      y = u.S.S;
      while (j--) {
        x += z.F;
        y += z.S;
        if (!(x >= 0 and y >= 0 and x < r and y < c and !arr[x][y] and
              best[x][y] >= 1 - u.F))
          break;
        best[x][y] = 1 - u.F;
        q.push({-best[x][y], {x, y}});
      }
    }
  }
  // rep(i,0,r)
  // {
  //     rep(j,0,c)cout<<best[i][j]<<' ';
  //     cout<<'\\n';
  // }
  if (best[f.F][f.S] == 1e9)
    best[f.F][f.S] = -1;
  cout << best[f.F][f.S];

  return 0;
}