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

#pragma optimization_level 3
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
#define F first
#define S second
#define vec vector
#define pb push_back
#define pll pair<ll, ll>
#define pdd pair<ld, ld>
#define pq priority_queue
#define umap unordered_map
#define uset unordered_set
#define pii pair<int, int>
#define pnn pair<Node *, Node *>
#define uid uniform_int_distribution
#define FILE                                                                   \\
  ifstream in("input.txt");                                                    \\
  ofstream out("output.txt");
#define fast                                                                   \\
  cin.tie(0);                                                                  \\
  cout.tie(0);                                                                 \\
  cin.sync_with_stdio(0);                                                      \\
  cout.sync_with_stdio(0);
using namespace std;
typedef string str;
typedef long long ll;
typedef long double ld;
typedef unsigned int uint;
typedef unsigned long long ull;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

int a, b, k, sx, sy, fx, fy;
vec<str> m;
vec<vec<int>> us;

int main() {
  fast;
  cin >> a >> b >> k;
  cin >> sx >> sy >> fx >> fy;
  sx--, sy--, fx--, fy--;
  m = vec<str>(a);
  us = vec<vec<int>>(a, vec<int>(b));
  for (int q = 0; q < a; q++) {
    cin >> m[q];
    for (int w = 0; w < b; w++)
      if (m[q][w] == '@')
        us[q][w] = -1;
  }

  vec<pii> n1 = {{sx, sy}}, n2;
  us[sx][sy] = 1;
  for (; n1.size() + n2.size();) {
    if (n1.empty())
      swap(n1, n2);
    pii p = n1.back();
    n1.pop_back();
    int x = p.F, y = p.S;
    for (int nx = x + 1, i = 1; nx < a && i <= k; i++, nx++) {
      if (us[nx][y] && us[nx][y] <= us[x][y])
        break;
      if (us[nx][y])
        continue;
      us[nx][y] = us[x][y] + 1;
      n2.pb({nx, y});
    }
    for (int nx = x - 1, i = 1; nx >= 0 && i <= k; i++, nx--) {
      if (us[nx][y] && us[nx][y] <= us[x][y])
        break;
      if (us[nx][y])
        continue;
      us[nx][y] = us[x][y] + 1;
      n2.pb({nx, y});
    }
    for (int ny = y + 1, i = 1; ny < b && i <= k; i++, ny++) {
      if (us[x][ny] && us[x][ny] <= us[x][y])
        break;
      if (us[x][ny])
        continue;
      us[x][ny] = us[x][y] + 1;
      n2.pb({x, ny});
    }
    for (int ny = y - 1, i = 1; ny >= 0 && i <= k; i++, ny--) {
      if (us[x][ny] && us[x][ny] <= us[x][y])
        break;
      if (us[x][ny])
        continue;
      us[x][ny] = us[x][y] + 1;
      n2.pb({x, ny});
    }
  }
  cout << us[fx][fy] - 1;
}

Error: TLE

Fixed submission:

#pragma optimization_level 3
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
#define F first
#define S second
#define vec vector
#define pb push_back
#define pll pair<ll, ll>
#define pdd pair<ld, ld>
#define pq priority_queue
#define umap unordered_map
#define uset unordered_set
#define pii pair<int, int>
#define pnn pair<Node *, Node *>
#define uid uniform_int_distribution
#define FILE                                                                   \\
  ifstream in("input.txt");                                                    \\
  ofstream out("output.txt");
#define fast                                                                   \\
  cin.tie(0);                                                                  \\
  cout.tie(0);                                                                 \\
  cin.sync_with_stdio(0);                                                      \\
  cout.sync_with_stdio(0);
using namespace std;
typedef string str;
typedef long long ll;
typedef long double ld;
typedef unsigned int uint;
typedef unsigned long long ull;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

int a, b, k, sx, sy, fx, fy;
vec<str> m;
vec<vec<int>> us;

int main() {
  fast;
  cin >> a >> b >> k;
  cin >> sx >> sy >> fx >> fy;
  sx--, sy--, fx--, fy--;
  m = vec<str>(a);
  us = vec<vec<int>>(a, vec<int>(b));
  for (int q = 0; q < a; q++) {
    cin >> m[q];
    for (int w = 0; w < b; w++)
      if (m[q][w] == '@')
        us[q][w] = -1;
  }

  vec<pii> n1 = {{sx, sy}}, n2;
  us[sx][sy] = 1;
  for (; n1.size() + n2.size();) {
    if (n1.empty())
      swap(n1, n2);
    pii p = n1.back();
    n1.pop_back();
    int x = p.F, y = p.S;
    for (int nx = x + 1, i = 1; nx < a && i <= k; i++, nx++) {
      if (us[nx][y] && us[nx][y] <= us[x][y])
        break;
      us[nx][y] = us[x][y] + 1;
      n2.pb({nx, y});
    }
    for (int nx = x - 1, i = 1; nx >= 0 && i <= k; i++, nx--) {
      if (us[nx][y] && us[nx][y] <= us[x][y])
        break;
      us[nx][y] = us[x][y] + 1;
      n2.pb({nx, y});
    }
    for (int ny = y + 1, i = 1; ny < b && i <= k; i++, ny++) {
      if (us[x][ny] && us[x][ny] <= us[x][y])
        break;
      us[x][ny] = us[x][y] + 1;
      n2.pb({x, ny});
    }
    for (int ny = y - 1, i = 1; ny >= 0 && i <= k; i++, ny--) {
      if (us[x][ny] && us[x][ny] <= us[x][y])
        break;
      us[x][ny] = us[x][y] + 1;
      n2.pb({x, ny});
    }
  }
  cout << us[fx][fy] - 1;
}