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

#include <bits/stdc++.h>
#define FOR0(i, n) for (int i = 0; i < n; i++)
#define FOR1(i, n) for (ll i = 1; i <= n; i++)
#define FORl(i, l, n) for (ll i = l; i < n; i++)
using namespace std;
#define pi pair<int, int>
#define pb push_back
#define ll long long
#define ld long double
#define ff first
#define ss second
#define mp make_pair
#define mst0(x) memset(x, 0, sizeof(x));
#define vi vector<ll>
#define sync                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl '\\n'
#define sp << " " <<
#define intmax 1e18
ll power(ll num, ll g, ll mod) {
  if (g == 0)
    return 1;
  if (g % 2 == 1)
    return (num * power((num * num) % mod, g / 2, mod)) % mod;
  return power((num * num) % mod, g / 2, mod);
}

int main() {
  sync int h, w, k, x1, y1, x2, y2;
  cin >> h >> w >> k;
  cin >> x1 >> y1 >> x2 >> y2;
  int visited[h + 1][w + 1];
  memset(visited, -1, sizeof(visited));
  string x[h];
  FOR0(i, h) cin >> x[i];
  char pond[h + 1][w + 1];
  FOR1(i, h) {
    FOR1(j, w) { pond[i][j] = x[i - 1][j - 1]; }
  }
  queue<pair<pi, int>> qu;
  qu.push({{x1, y1}, 0});
  visited[x1][y1] = 0;
  while (!qu.empty()) {
    auto pt = qu.front();
    qu.pop();
    if (visited[x2][y2] != -1)
      break;
    int x = pt.ff.ff;
    int y = pt.ff.ss;
    FOR1(i, k) {
      if (x + i > h || pond[x + i][y] == '@')
        break;

      if (visited[x + i][y] == -1) {
        visited[x + i][y] = pt.ss + 1;
        qu.push({{x + i, y}, pt.ss + 1});
      } else {
        if (visited[x + i][y] < 1 + visited[x][y])
          break;
      }
    }
    FOR1(i, k) {
      if (x - i <= 0 || pond[x - i][y] == '@')
        break;

      if (visited[x - i][y] == -1) {
        visited[x - i][y] = pt.ss + 1;
        qu.push({{x - i, y}, pt.ss + 1});
      } else {
        if (visited[x - i][y] < 1 + visited[x][y])
          break;
      }
    }
    FOR1(i, k) {
      if (y + i > w || pond[x][y + i] == '@')
        break;

      if (visited[x][y + i] == -1) {
        visited[x][y + i] = pt.ss + 1;
        qu.push({{x, y + i}, pt.ss + 1});
      } else {
        if (visited[x][y + i] < 1 + visited[x][y])
          break;
      }
    }
    FOR1(i, k) {
      if (y - i <= 0 || pond[x][y - i] == '@')
        break;
      if (visited[x][y - i] == -1) {
        visited[x][y - i] = pt.ss + 1;
        qu.push({{x, y - i}, pt.ss + 1});
      } else {
        if (visited[x][y - i] < 1 + visited[x][y])
          break;
      }
    }
  }

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define FOR0(i, n) for (int i = 0; i < n; i++)
#define FOR1(i, n) for (ll i = 1; i <= n; i++)
#define FORl(i, l, n) for (ll i = l; i < n; i++)
using namespace std;
#define pi pair<int, int>
#define pb push_back
#define ll long long
#define ld long double
#define ff first
#define ss second
#define mp make_pair
#define mst0(x) memset(x, 0, sizeof(x));
#define vi vector<ll>
#define sync                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl '\\n'
#define sp << " " <<
#define intmax 1e18
ll power(ll num, ll g, ll mod) {
  if (g == 0)
    return 1;
  if (g % 2 == 1)
    return (num * power((num * num) % mod, g / 2, mod)) % mod;
  return power((num * num) % mod, g / 2, mod);
}

int main() {
  sync int h, w, k, x1, y1, x2, y2;
  cin >> h >> w >> k;
  cin >> x1 >> y1 >> x2 >> y2;
  int visited[h + 1][w + 1];
  memset(visited, -1, sizeof(visited));
  string x[h];
  FOR0(i, h) cin >> x[i];
  char pond[h + 1][w + 1];
  FOR1(i, h) {
    FOR1(j, w) { pond[i][j] = x[i - 1][j - 1]; }
  }
  queue<pair<pi, int>> qu;
  qu.push({{x1, y1}, 0});
  visited[x1][y1] = 0;
  while (!qu.empty()) {
    auto pt = qu.front();
    qu.pop();
    if (visited[x2][y2] != -1)
      break;
    int x = pt.ff.ff;
    int y = pt.ff.ss;
    FOR1(i, k) {
      if (x + i > h || pond[x + i][y] == '@')
        break;
      if (visited[x + i][y] == -1) {
        visited[x + i][y] = pt.ss + 1;
        qu.push({{x + i, y}, pt.ss + 1});
      }
    }
    FOR1(i, k) {
      if (x - i <= 0 || pond[x - i][y] == '@')
        break;
      if (visited[x - i][y] == -1) {
        visited[x - i][y] = pt.ss + 1;
        qu.push({{x - i, y}, pt.ss + 1});
      }
    }
    FOR1(i, k) {
      if (y + i > w || pond[x][y + i] == '@')
        break;
      if (visited[x][y + i] == -1) {
        visited[x][y + i] = pt.ss + 1;
        qu.push({{x, y + i}, pt.ss + 1});
      }
    }
    FOR1(i, k) {
      if (y - i <= 0 || pond[x][y - i] == '@')
        break;
      if (visited[x][y - i] == -1) {
        visited[x][y - i] = pt.ss + 1;
        qu.push({{x, y - i}, pt.ss + 1});
      }
    }
  }

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