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

#include <bits/stdc++.h>
#include <iomanip>
#define M 100000
#define MAX 1000000001
#define MIN -100000000
#define mod 998244353
#define ss second
#define ff first
#define ll long long int
#define pb push_back
#define ld double
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL)
using namespace std;

int main() {
  fast;
  int h, w, k;
  cin >> h >> w >> k;
  int x1, x2, y1, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  string s[h + 1];
  for (int i = 1; i <= h; i++)
    cin >> s[i];
  int dp[h + 1][w + 1];
  memset(dp, -1, sizeof(dp));
  queue<pair<int, int>> q;
  q.push({x1, y1});
  dp[x1][y1] = 0;
  while (q.empty() == false) {
    pair<int, int> current = q.front();
    // cout<<current.ff<<" sddj "<<current.ss<<endl;
    for (int i = 1; i <= k; i++) {
      if (current.ff + i <= h && dp[current.ff + i][current.ss] == -1) {
        if (s[current.ff + i][current.ss - 1] == '.') {
          q.push({current.ff + i, current.ss});
          dp[current.ff + i][current.ss] = dp[current.ff][current.ss] + 1;
        } else {
          break;
        }
      } else if (current.ff + i <= h &&
                 dp[current.ff + i][current.ss] <= dp[current.ff][current.ss])
        break;
    }
    for (int i = 1; i <= k; i++) {
      if (current.ff - i > 0 && dp[current.ff - i][current.ss] == -1) {
        if (s[current.ff - i][current.ss - 1] == '.') {
          q.push({current.ff - i, current.ss});
          dp[current.ff - i][current.ss] = dp[current.ff][current.ss] + 1;
        } else {
          break;
        }
      } else if (current.ff - i > 0 &&
                 dp[current.ff - i][current.ss] <= dp[current.ff][current.ss])
        break;
    }

    for (int i = 1; i <= k; i++) {
      if (current.ss + i <= w && dp[current.ff][current.ss + i] == -1) {
        if (s[current.ff][current.ss + i - 1] == '.') {
          q.push({current.ff, current.ss + i});
          // cout<<current.ff<<" "<<(current.ss+i)<<"\\n";
          dp[current.ff][current.ss + i] = dp[current.ff][current.ss] + 1;
        } else {
          break;
        }
      } else if (current.ss + i <= w &&
                 dp[current.ff][current.ss + i] <= dp[current.ff][current.ss])
        break;
    }
    for (int i = 1; i <= k; i++) {
      if (current.ss - i > 0 && dp[current.ff][current.ss - i] == -1) {
        if (s[current.ff][current.ss - i - 1] == '.') {
          q.push({current.ff, current.ss - i});
          dp[current.ff][current.ss - i] = dp[current.ff][current.ss] + 1;
        } else {
          break;
        }
      } else if (current.ss - i > 0 &&
                 dp[current.ff][current.ss - i] <= dp[current.ff][current.ss])
        break;
    }
    q.pop();
  }
  // for(int i=1;i<=h;i++){
  // 	for(int j=1;j<=w;j++)
  // 		cout<<dp[i][j]<<" ";
  // 	cout<<endl;
  // }
  cout << dp[x2][y2] << "\\n";

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <iomanip>
#define M 100000
#define MAX 1000000001
#define MIN -100000000
#define mod 998244353
#define ss second
#define ff first
#define ll long long int
#define pb push_back
#define ld double
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL)
using namespace std;

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  int x1, x2, y1, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  string s[h + 1];
  for (int i = 1; i <= h; i++)
    cin >> s[i];
  int dp[h + 1][w + 1];
  memset(dp, -1, sizeof(dp));
  queue<pair<int, int>> q;
  q.push({x1, y1});
  dp[x1][y1] = 0;
  while (q.empty() == false) {
    pair<int, int> current = q.front();
    // cout<<current.ff<<" sddj "<<current.ss<<endl;
    for (int i = 1; i <= k; i++) {
      if (current.ff + i <= h && dp[current.ff + i][current.ss] == -1) {
        if (s[current.ff + i][current.ss - 1] == '.') {
          q.push({current.ff + i, current.ss});
          dp[current.ff + i][current.ss] = dp[current.ff][current.ss] + 1;
        } else {
          break;
        }
      }
    }
    for (int i = 1; i <= k; i++) {
      if (current.ff - i > 0 && dp[current.ff - i][current.ss] == -1) {
        if (s[current.ff - i][current.ss - 1] == '.') {
          q.push({current.ff - i, current.ss});
          dp[current.ff - i][current.ss] = dp[current.ff][current.ss] + 1;
        } else {
          break;
        }
      }
    }

    for (int i = 1; i <= k; i++) {
      if (current.ss + i <= w && dp[current.ff][current.ss + i] == -1) {
        if (s[current.ff][current.ss + i - 1] == '.') {
          q.push({current.ff, current.ss + i});
          // cout<<current.ff<<" "<<(current.ss+i)<<"\\n";
          dp[current.ff][current.ss + i] = dp[current.ff][current.ss] + 1;
        } else {
          break;
        }
      }
    }
    for (int i = 1; i <= k; i++) {
      if (current.ss - i > 0 && dp[current.ff][current.ss - i] == -1) {
        if (s[current.ff][current.ss - i - 1] == '.') {
          q.push({current.ff, current.ss - i});
          dp[current.ff][current.ss - i] = dp[current.ff][current.ss] + 1;
        } else {
          break;
        }
      }
    }
    q.pop();
  }
  // for(int i=1;i<=h;i++){
  // 	for(int j=1;j<=w;j++)
  // 		cout<<dp[i][j]<<" ";
  // 	cout<<endl;
  // }
  cout << dp[x2][y2] << "\\n";

  return 0;
}