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

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define maxn 100005
const ll inf = 1e9 + 7;
#define pb push_back
void solve();
int main() {
  ios_base::sync_with_stdio(false);
  cout << fixed;
  cout << setprecision(10);
  int t = 1;
  // cin>>t;
  while (t--)
    solve();
  return 0;
}
ll h, w, i, j, k;

void solve() {
  ll x1, y1, x2, y2;
  cin >> h >> w >> k;
  cin >> x1 >> y1 >> x2 >> y2;
  ll dp[h + 1][w + 1];
  char arr[h + 1][w + 1];
  for (i = 1; i <= h; i++) {
    for (j = 1; j <= w; j++) {
      cin >> arr[i][j];
      dp[i][j] = inf;
    }
  }
  ll cnt[h + 1][w + 1];
  for (i = 1; i <= h; i++) {
    for (j = 1; j <= k; j++)
      cnt[i][j] = 0;
  }

  dp[x1][y1] = 0;
  deque<pair<ll, ll>> s;
  s.push_back({x1, y1});
  while (!s.empty()) {
    pair<ll, ll> p = s.front();
    s.pop_front();
    ll currx = p.first;
    ll curry = p.second;

    for (i = currx + 1; i <= min(h, currx + k); i++) {
      if (dp[i][curry] == inf) {

        if (arr[i][curry] == '@')
          break;
        dp[i][curry] = min(dp[i][curry], dp[currx][curry] + 1);
        s.push_back({i, curry});
      } else {
        if (cnt[i][curry] == 4)
          break;
        cnt[i][curry]++;
      }
    }
    for (i = currx - 1; i >= max(1LL, currx - k); i--) {
      if (dp[i][curry] == inf) {

        if (arr[i][curry] == '@')
          break;
        dp[i][curry] = min(dp[i][curry], dp[currx][curry] + 1);
        s.push_back({i, curry});
      } else {
        if (cnt[i][curry] == 4)
          break;
        cnt[i][curry]++;
      }
    }
    for (i = curry + 1; i <= min(w, curry + k); i++) {
      if (dp[currx][i] == inf) {
        if (arr[currx][i] == '@')
          break;
        dp[currx][i] = min(dp[currx][i], dp[currx][curry] + 1);
        s.push_back({currx, i});
      } else {
        if (cnt[currx][i] == 4)
          break;
        cnt[currx][i]++;
      }
    }
    for (i = curry - 1; i >= max(1LL, curry - k); i--) {
      if (dp[currx][i] == inf) {
        if (arr[currx][i] == '@')
          break;

        dp[currx][i] = min(dp[currx][i], dp[currx][curry] + 1);
        s.push_back({currx, i});
      } else {
        if (cnt[currx][i] == 4)
          break;
        cnt[currx][i]++;
      }
    }
  }
  /*
  for(i=1;i<=h;i++)
  {
          for(j=1;j<=w;j++)
          cout<<dp[i][j]<<" ";
          cout<<endl;
  }
  */

  if (dp[x2][y2] == inf)
    cout << -1;
  else
    cout << dp[x2][y2];
}

// BORA BITCH IM COMING FOR YOU!!!


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define maxn 100005
const ll inf = 1e9 + 7;
#define pb push_back
void solve();
int main() {
  ios_base::sync_with_stdio(false);
  cout << fixed;
  cout << setprecision(10);
  int t = 1;
  // cin>>t;
  while (t--)
    solve();
  return 0;
}
ll h, w, i, j, k;

void solve() {
  ll x1, y1, x2, y2;
  cin >> h >> w >> k;
  cin >> x1 >> y1 >> x2 >> y2;
  ll dp[h + 1][w + 1];
  char arr[h + 1][w + 1];
  for (i = 1; i <= h; i++) {
    for (j = 1; j <= w; j++) {
      cin >> arr[i][j];
      dp[i][j] = inf;
    }
  }
  dp[x1][y1] = 0;
  deque<pair<ll, ll>> s;
  s.push_back({x1, y1});
  while (!s.empty()) {
    pair<ll, ll> p = s.front();
    s.pop_front();
    ll currx = p.first;
    ll curry = p.second;

    for (i = currx + 1; i <= min(h, currx + k); i++) {
      if (dp[i][curry] == inf) {

        if (arr[i][curry] == '@')
          break;
        dp[i][curry] = min(dp[i][curry], dp[currx][curry] + 1);
        s.push_back({i, curry});
      }
    }
    for (i = currx - 1; i >= max(1LL, currx - k); i--) {
      if (dp[i][curry] == inf) {

        if (arr[i][curry] == '@')
          break;
        dp[i][curry] = min(dp[i][curry], dp[currx][curry] + 1);
        s.push_back({i, curry});
      }
    }
    for (i = curry + 1; i <= min(w, curry + k); i++) {
      if (dp[currx][i] == inf) {
        if (arr[currx][i] == '@')
          break;
        dp[currx][i] = min(dp[currx][i], dp[currx][curry] + 1);
        s.push_back({currx, i});
      }
    }
    for (i = curry - 1; i >= max(1LL, curry - k); i--) {
      if (dp[currx][i] == inf) {
        if (arr[currx][i] == '@')
          break;

        dp[currx][i] = min(dp[currx][i], dp[currx][curry] + 1);
        s.push_back({currx, i});
      }
    }
  }
  /*
  for(i=1;i<=h;i++)
  {
          for(j=1;j<=w;j++)
          cout<<dp[i][j]<<" ";
          cout<<endl;
  }
  */

  if (dp[x2][y2] == inf)
    cout << -1;
  else
    cout << dp[x2][y2];
}

// BORA BITCH IM COMING FOR YOU!!!