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

/***************************************** START OF TEMPLATE
 * *********************************************/

#include <bits/stdc++.h>

using namespace std;

#define SPEED                                                                  \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);                                                                 \\
  cout << fixed;                                                               \\
  cout << setprecision(10);
#define randomINIT                                                             \\
  mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
#define foo(i, a, n) for (ll i = (a); i <= n; i++)
#define frr(i, a, n) for (ll i = (a); i >= n; i--)
#define fo(i, n) for (ll i = 0; i < n; i++)
#define newl cout << "\\n";
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define sym(s) s = "@" + s + "@";
#define all(x) (x).begin(), (x).end()
#define alll(x, n) x + 1, x + n + 1
#define mset(x, val) memset(x, val, sizeof(x))
#define mset1d(a, n, v) foo(i, 0, n + 5) a[i] = v;
#define mset2d(a, n, m, v) foo(i, 0, n + 5) foo(j, 0, m + 5) a[i][j] = v;
#define INP(a, n) foo(i, 1, n) cin >> a[i];
#define deb1d(a, n)                                                            \\
  foo(i, 1, n) cout << a[i] << " ";                                            \\
  newl;
#define deb2d(a, n, m)                                                         \\
  foo(i, 1, n) {                                                               \\
    foo(j, 1, m) { cout << a[i][j] << " "; }                                   \\
    newl;                                                                      \\
  }
#define debm(a)                                                                \\
  for (auto it : a)                                                            \\
    cout << "{" << it.f << "," << it.s << "}, ";                               \\
  newl;
#define debp(a, n)                                                             \\
  foo(i, 1, n) cout << "{" << a[i].f << "," << a[i].s << "}, ";                \\
  newl;
#define debv(a)                                                                \\
  for (auto it : a)                                                            \\
    cout << it << " ";                                                         \\
  newl;
#define deb1(a)                                                                \\
  cout << a << " ";                                                            \\
  newl;
#define deb2(a, b)                                                             \\
  cout << a << " " << b << " ";                                                \\
  newl;
#define deb3(a, b, c)                                                          \\
  cout << a << " " << b << " " << c << " ";                                    \\
  newl;
#define deb4(a, b, c, d)                                                       \\
  cout << a << " " << b << " " << c << " " << d;                               \\
  newl;
#define ll long long
#define ld long double
#define pll pair<ll, ll>
#define vll vector<ll>
#define vpll vector<pll>

const ld PI = 3.14159265358979323846;
const ll MOD = 1e+9 + 7;
const ll INF = LLONG_MAX;
const int INFi = INT_MAX;
const ll N = 1e+6 + 8;

vll adj[N];
ll dx4[] = {0, 1, 0, -1}, dy4[] = {1, 0, -1, 0};

ll test_cases = 1;
randomINIT;

/*********************************************************************************************************/
/***************************************** VARIABLE DECLARATIONS
 * *****************************************/
ll n, m, k;
ll ax, ay, bx, by;
ll global_ans = INF;
string s[N];
struct node {
  ll x, y, res;
};

ll vis[N], vis_actual[N];
// dir - 1 means x axis, 2, means y axis
/***************************************** FUNCTION IMPLEMENTATIONS
 * **************************************/
void bfs() {
  queue<node> q;
  q.push({ax, ay, 0});
  vis[(ax - 1) * m + ay] = 1;
  while (q.size() > 0) {
    node p = q.front();
    q.pop();
    ll x = p.x, y = p.y, res = p.res;
    if (x == bx && y == by) {
      global_ans = min(global_ans, res);
      break;
    }
    vis_actual[(x - 1) * m + y] = 1;

    fo(r, 4) {
      foo(step, 1, k) {
        ll dx = x + step * dx4[r];
        ll dy = y + step * dy4[r];
        if (s[dx][dy] == '@' || vis_actual[(dx - 1) * m + dy] == 1)
          break;
        if (vis[(dx - 1) * m + dy] == 0) {
          vis[(dx - 1) * m + dy] = 1;
          q.push({dx, dy, res + 1});
        }
      }
    }
  }
}

/***************************************** START OF MAIN FUNCTION
 * ****************************************/
void MAIN(ll tc) {
  cin >> n >> m >> k;
  cin >> ax >> ay >> bx >> by;
  foo(i, 1, n) {
    cin >> s[i];
    sym(s[i]);
  }
  s[0] = s[n + 1] = string(m + 2, '@');
  bfs();
  if (global_ans == INF)
    global_ans = -1;
  cout << global_ans;
}

int main() {
  SPEED;

  // cin>>test_cases;
  foo(i, 1, test_cases) { MAIN(i); }
}


Error: TLE

Fixed submission:

/***************************************** START OF TEMPLATE
 * *********************************************/

#include <bits/stdc++.h>

using namespace std;

#define SPEED                                                                  \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);                                                                 \\
  cout << fixed;                                                               \\
  cout << setprecision(10);
#define randomINIT                                                             \\
  mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
#define foo(i, a, n) for (ll i = (a); i <= n; i++)
#define frr(i, a, n) for (ll i = (a); i >= n; i--)
#define fo(i, n) for (ll i = 0; i < n; i++)
#define newl cout << "\\n";
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define sym(s) s = "@" + s + "@";
#define all(x) (x).begin(), (x).end()
#define alll(x, n) x + 1, x + n + 1
#define mset(x, val) memset(x, val, sizeof(x))
#define mset1d(a, n, v) foo(i, 0, n + 5) a[i] = v;
#define mset2d(a, n, m, v) foo(i, 0, n + 5) foo(j, 0, m + 5) a[i][j] = v;
#define INP(a, n) foo(i, 1, n) cin >> a[i];
#define deb1d(a, n)                                                            \\
  foo(i, 1, n) cout << a[i] << " ";                                            \\
  newl;
#define deb2d(a, n, m)                                                         \\
  foo(i, 1, n) {                                                               \\
    foo(j, 1, m) { cout << a[i][j] << " "; }                                   \\
    newl;                                                                      \\
  }
#define debm(a)                                                                \\
  for (auto it : a)                                                            \\
    cout << "{" << it.f << "," << it.s << "}, ";                               \\
  newl;
#define debp(a, n)                                                             \\
  foo(i, 1, n) cout << "{" << a[i].f << "," << a[i].s << "}, ";                \\
  newl;
#define debv(a)                                                                \\
  for (auto it : a)                                                            \\
    cout << it << " ";                                                         \\
  newl;
#define deb1(a)                                                                \\
  cout << a << " ";                                                            \\
  newl;
#define deb2(a, b)                                                             \\
  cout << a << " " << b << " ";                                                \\
  newl;
#define deb3(a, b, c)                                                          \\
  cout << a << " " << b << " " << c << " ";                                    \\
  newl;
#define deb4(a, b, c, d)                                                       \\
  cout << a << " " << b << " " << c << " " << d;                               \\
  newl;
#define ll long long
#define ld long double
#define pll pair<ll, ll>
#define vll vector<ll>
#define vpll vector<pll>

const ld PI = 3.14159265358979323846;
const ll MOD = 1e+9 + 7;
const ll INF = LLONG_MAX;
const int INFi = INT_MAX;
const ll N = 1e+6 + 8;

vll adj[N];
ll dx4[] = {0, 1, 0, -1}, dy4[] = {1, 0, -1, 0};

ll test_cases = 1;
randomINIT;

/*********************************************************************************************************/
/***************************************** VARIABLE DECLARATIONS
 * *****************************************/
ll n, m, k;
ll ax, ay, bx, by;
ll global_ans = INF;
string s[N];
struct node {
  ll x, y, res;
};

map<pll, ll> vis, vis_actual;
// dir - 1 means x axis, 2, means y axis
/***************************************** FUNCTION IMPLEMENTATIONS
 * **************************************/
void bfs() {
  queue<node> q;
  q.push({ax, ay, 0});
  vis[{ax, ay}] = 1;
  while (q.size() > 0) {
    node p = q.front();
    q.pop();
    ll x = p.x, y = p.y, res = p.res;
    if (x == bx && y == by) {
      global_ans = min(global_ans, res);
      break;
    }
    vis_actual[{x, y}] = 1;

    fo(r, 4) {
      foo(step, 1, k) {
        ll dx = x + step * dx4[r];
        ll dy = y + step * dy4[r];
        if (s[dx][dy] == '@' || vis_actual[{dx, dy}] == 1)
          break;
        if (vis[{dx, dy}] == 0) {
          vis[{dx, dy}] = 1;
          q.push({dx, dy, res + 1});
        }
      }
    }
  }
}

/***************************************** START OF MAIN FUNCTION
 * ****************************************/
void MAIN(ll tc) {
  cin >> n >> m >> k;
  cin >> ax >> ay >> bx >> by;
  foo(i, 1, n) {
    cin >> s[i];
    sym(s[i]);
  }
  s[0] = s[n + 1] = string(m + 2, '@');
  bfs();
  if (global_ans == INF)
    global_ans = -1;
  cout << global_ans;
}

int main() {
  SPEED;

  // cin>>test_cases;
  foo(i, 1, test_cases) { MAIN(i); }
}