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

#include "bits/stdc++.h"
#define ll long long
#define lld long double
#define MOD 1000000007LL
#define inf 1000000000000000000ll
#define pii pair<int, int>
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define all(v) v.begin(), v.end()
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
ll power(ll x, ll y, ll md = MOD) {
  ll res = 1;
  x %= md;
  while (y) {
    if (y & 1)
      res = (res * x) % md;
    x *= x;
    if (x >= md)
      x %= md;
    y >>= 1;
  }
  return res;
}

using namespace std;

#define endl '\\n'
#define int ll

signed main() {
  fast;

  int h, w, k;
  cin >> h >> w >> k;

  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;

  vector<vector<char>> mat(h, vector<char>(w, '.'));
  vector<vector<int>> vis(h, vector<int>(w, -1));
  vector<vector<bool>> touch(h, vector<bool>(w, false));

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++)
      cin >> mat[i][j];
  }

  queue<pair<pii, int>> q;
  q.push({{x1, y1}, 0});

  while (!q.empty()) {
    pii cell;
    int d;
    tie(cell, d) = q.front();
    q.pop();

    if (vis[cell.f][cell.s] != -1)
      continue;
    else
      vis[cell.f][cell.s] = d;

    if (x2 == cell.f && y2 == cell.s) {
      cout << d;
      return 0;
    }

    for (int i = 1; i <= k; i++) {
      if (cell.f - i < 0)
        break;
      if (mat[cell.f - i][cell.s] == '@')
        break;
      if (vis[cell.f - i][cell.s] != -1)
        break;
      if (!touch[cell.f - i][cell.s])
        q.push({{cell.f - i, cell.s}, d + 1});
      touch[cell.f - i][cell.s] = 1;
    }

    for (int i = 1; i <= k; i++) {
      if (cell.f + i >= h)
        break;
      if (mat[cell.f + i][cell.s] == '@')
        break;
      if (vis[cell.f + i][cell.s] != -1)
        break;
      if (!touch[cell.f + i][cell.s])
        q.push({{cell.f + i, cell.s}, d + 1});
      touch[cell.f + i][cell.s] = 1;
    }

    for (int i = 1; i <= k; i++) {
      if (cell.s - i < 0)
        break;
      if (mat[cell.f][cell.s - i] == '@')
        break;
      if (vis[cell.f][cell.s - i] != -1)
        break;
      if (!touch[cell.f][cell.s - i])
        q.push({{cell.f, cell.s - i}, d + 1});
      touch[cell.f][cell.s - i] = 1;
    }

    for (int i = 1; i <= k; i++) {
      if (cell.s + i >= w)
        break;
      if (mat[cell.f][cell.s + i] == '@')
        break;
      if (vis[cell.f][cell.s + i] != -1)
        break;
      if (!touch[cell.f][cell.s + i])
        q.push({{cell.f, cell.s + i}, d + 1});
      touch[cell.f][cell.s + i] = 1;
    }
  }

  cout << vis[x2][y2];
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#define ll long long
#define lld long double
#define MOD 1000000007LL
#define inf 1000000000000000000ll
#define pii pair<int, int>
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define all(v) v.begin(), v.end()
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
ll power(ll x, ll y, ll md = MOD) {
  ll res = 1;
  x %= md;
  while (y) {
    if (y & 1)
      res = (res * x) % md;
    x *= x;
    if (x >= md)
      x %= md;
    y >>= 1;
  }
  return res;
}

using namespace std;

#define endl '\\n'
// #define int ll

signed main() {
  fast;

  int h, w, k;
  cin >> h >> w >> k;

  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;

  vector<vector<char>> mat(h, vector<char>(w, '.'));
  vector<vector<int>> vis(h, vector<int>(w, -1));
  vector<vector<bool>> touch(h, vector<bool>(w, false));

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++)
      cin >> mat[i][j];
  }

  queue<pair<pii, int>> q;
  q.push({{x1, y1}, 0});

  while (!q.empty()) {
    pii cell;
    int d;
    tie(cell, d) = q.front();
    q.pop();

    if (x2 == cell.f && y2 == cell.s) {
      cout << d;
      return 0;
    }

    for (int i = 1; i <= k; i++) {
      if (cell.f - i < 0)
        break;
      if (mat[cell.f - i][cell.s] == '@')
        break;
      if (vis[cell.f - i][cell.s] != -1)
        break;
      if (!touch[cell.f - i][cell.s])
        q.push({{cell.f - i, cell.s}, d + 1});
      touch[cell.f - i][cell.s] = 1;
    }

    for (int i = 1; i <= k; i++) {
      if (cell.f + i >= h)
        break;
      if (mat[cell.f + i][cell.s] == '@')
        break;
      if (vis[cell.f + i][cell.s] != -1)
        break;
      if (!touch[cell.f + i][cell.s])
        q.push({{cell.f + i, cell.s}, d + 1});
      touch[cell.f + i][cell.s] = 1;
    }

    for (int i = 1; i <= k; i++) {
      if (cell.s - i < 0)
        break;
      if (mat[cell.f][cell.s - i] == '@')
        break;
      if (vis[cell.f][cell.s - i] != -1)
        break;
      if (!touch[cell.f][cell.s - i])
        q.push({{cell.f, cell.s - i}, d + 1});
      touch[cell.f][cell.s - i] = 1;
    }

    for (int i = 1; i <= k; i++) {
      if (cell.s + i >= w)
        break;
      if (mat[cell.f][cell.s + i] == '@')
        break;
      if (vis[cell.f][cell.s + i] != -1)
        break;
      if (!touch[cell.f][cell.s + i])
        q.push({{cell.f, cell.s + i}, d + 1});
      touch[cell.f][cell.s + i] = 1;
    }
  }

  cout << -1;
}