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

// g++ -std=c++17 -Wshadow -Wall -fsanitize=address -fsanitize=undefined
// -D_GLIBCXX_DEBUG -g PROGRAM -o OUTPUT -DLOCAL -lasan -lubsan
#include <bits/stdc++.h>
#include <ext/pb_ds/detail/standard_policies.hpp>

#define ll long long
#define ull unsigned long long
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define alli(a, n, k) (a + k), (a + n + k)
#define FP(i, a, b, k) for (__typeof(a) i = a; i < b; i += k)
#define FS(i, a, b, k) for (__typeof(a) i = a; i > b; i -= k)
#define IT(it, a) for (__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)
#define eps 1e-6
#define pi 3.141592653589793
using namespace std;
using namespace __gnu_pbds;

template <class T> inline T gcd(T x, T y) {
  if (!y)
    return x;
  return gcd(y, x % y);
}
typedef vector<int> VII;
typedef vector<ll> VLL;
typedef pair<int, int> PII;
typedef vector<pair<int, int>> VPII;
typedef vector<pair<int, PII>> VPPI;
const int MOD = 1e9 + 7;
const int INF = 1e9;

inline ll modulo(ll a, ll m) { return (a % m + m) % m; }

inline ll modInverse(ll a, ll m) {
  assert(__gcd(a, m) == 1);
  ll m0 = m;
  ll y = 0, x = 1;

  if (m == 1)
    return 0;

  while (a > 1) {
    ll q = a / m;
    ll t = m;

    m = a % m, a = t;
    t = y;

    y = x - q * y;
    x = t;
  }

  if (x < 0)
    x += m0;

  return x;
}

inline ll modPow(ll x, ll y, ll m) { // x^y % m
  if (y == 0)
    return 1LL;
  else if (y == 1)
    return x;
  else {
    ll ans = modPow(x, y / 2, m) % m;
    if (y & 1) {
      return (((ans * ans) % m) * x) % m;
    } else {
      return (ans * ans) % m;
    }
  }
}

vector<string> split(char d, string s) {
  stringstream ss(s);
  string t;
  vector<string> words;
  while (getline(ss, t, d)) {
    words.pb(t);
  }

  return words;
}
VPII dir = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
int main(int argc, char *argv[]) {
#ifdef LOCAL
  auto start = chrono::high_resolution_clock::now();
#endif

  ios::sync_with_stdio(false);
  cin.tie(NULL);

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

  int sx, sy, dx, dy;
  cin >> sx >> sy >> dx >> dy;
  sx--, sy--, dx--, dy--;
  vector<vector<char>> adj(h, vector<char>(w));

  for (auto &x : adj)
    for (auto &y : x)
      cin >> y;

  priority_queue<VII, vector<VII>, greater<VII>> q;
  vector<vector<int>> vis(h, vector<int>(w, INT_MAX));

  int ans = INT_MAX;
  q.push({0, sx, sy});
  while (!q.empty()) {
    auto v = q.top();
    q.pop();

    if (v[1] == dx && v[2] == dy) {
      // ans = min(ans, v[0]);
      cout << v[0];
      exit(0);
    }

    for (auto it : dir) {
      int x = v[1] + it.first;
      int y = v[2] + it.second;
      if (x >= 0 && x < h && y >= 0 && y < w && adj[x][y] == '.' &&
          vis[x][y] > v[0]) {
        q.push({v[0], x, y});
        vis[x][y] = v[0];
      }
    }

    for (int i = -2; i <= 2; ++i) {
      for (int j = -2; j <= 2; ++j) {
        int x = v[1] + i;
        int y = v[2] + j;
        if (x >= 0 && x < h && y >= 0 && y < w && adj[x][y] == '.' &&
            vis[x][y] > v[0] + 1) {
          q.push({v[0] + 1, x, y});
          vis[x][y] = v[0] + 1;
        }
      }
    }
  }
  if (ans == INT_MAX)
    cout << -1;
  else
    cout << ans;

#ifdef LOCAL
  auto end = chrono::high_resolution_clock::now();
  double time_taken =
      chrono::duration_cast<chrono::nanoseconds>(end - start).count();

  time_taken *= 1e-9;

  cerr << "\\nTime taken by program is : " << fixed << time_taken
       << setprecision(9);
  cerr << " sec" << endl;
#endif
  return 0;
}


Error: TLE

Fixed submission:

// g++ -std=c++17 -Wshadow -Wall -fsanitize=address -fsanitize=undefined
// -D_GLIBCXX_DEBUG -g PROGRAM -o OUTPUT -DLOCAL -lasan -lubsan
#include <bits/stdc++.h>
#include <ext/pb_ds/detail/standard_policies.hpp>

#define ll long long
#define ull unsigned long long
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define alli(a, n, k) (a + k), (a + n + k)
#define FP(i, a, b, k) for (__typeof(a) i = a; i < b; i += k)
#define FS(i, a, b, k) for (__typeof(a) i = a; i > b; i -= k)
#define IT(it, a) for (__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)
#define eps 1e-6
#define pi 3.141592653589793
using namespace std;
using namespace __gnu_pbds;

template <class T> inline T gcd(T x, T y) {
  if (!y)
    return x;
  return gcd(y, x % y);
}
typedef vector<int> VII;
typedef vector<ll> VLL;
typedef pair<int, int> PII;
typedef vector<pair<int, int>> VPII;
typedef vector<pair<int, PII>> VPPI;
const int MOD = 1e9 + 7;
const int INF = 1e9;

inline ll modulo(ll a, ll m) { return (a % m + m) % m; }

inline ll modInverse(ll a, ll m) {
  assert(__gcd(a, m) == 1);
  ll m0 = m;
  ll y = 0, x = 1;

  if (m == 1)
    return 0;

  while (a > 1) {
    ll q = a / m;
    ll t = m;

    m = a % m, a = t;
    t = y;

    y = x - q * y;
    x = t;
  }

  if (x < 0)
    x += m0;

  return x;
}

inline ll modPow(ll x, ll y, ll m) { // x^y % m
  if (y == 0)
    return 1LL;
  else if (y == 1)
    return x;
  else {
    ll ans = modPow(x, y / 2, m) % m;
    if (y & 1) {
      return (((ans * ans) % m) * x) % m;
    } else {
      return (ans * ans) % m;
    }
  }
}

vector<string> split(char d, string s) {
  stringstream ss(s);
  string t;
  vector<string> words;
  while (getline(ss, t, d)) {
    words.pb(t);
  }

  return words;
}
VPII dir = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
int main(int argc, char *argv[]) {
#ifdef LOCAL
  auto start = chrono::high_resolution_clock::now();
#endif

  ios::sync_with_stdio(false);
  cin.tie(NULL);

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

  int sx, sy, dx, dy;
  cin >> sx >> sy >> dx >> dy;
  sx--, sy--, dx--, dy--;
  vector<vector<char>> adj(h, vector<char>(w));

  for (auto &x : adj)
    for (auto &y : x)
      cin >> y;

  queue<VII> q;
  vector<vector<int>> vis(h, vector<int>(w, INT_MAX));

  int ans = INT_MAX;
  q.push({sx, sy, 0});
  while (!q.empty()) {
    auto v = q.front();
    q.pop();

    if (v[0] == dx && v[1] == dy) {
      ans = min(ans, v[2]);
    }

    for (auto it : dir) {
      int x = v[0] + it.first;
      int y = v[1] + it.second;
      if (x >= 0 && x < h && y >= 0 && y < w && adj[x][y] == '.' &&
          vis[x][y] > v[2]) {
        q.push({x, y, v[2]});
        vis[x][y] = v[2];
      }
    }

    for (int i = -2; i <= 2; ++i) {
      for (int j = -2; j <= 2; ++j) {
        int x = v[0] + i;
        int y = v[1] + j;
        if (x >= 0 && x < h && y >= 0 && y < w && adj[x][y] == '.' &&
            vis[x][y] > v[2] + 1) {
          q.push({x, y, v[2] + 1});
          vis[x][y] = v[2] + 1;
        }
      }
    }
  }
  if (ans == INT_MAX)
    cout << -1;
  else
    cout << ans;

#ifdef LOCAL
  auto end = chrono::high_resolution_clock::now();
  double time_taken =
      chrono::duration_cast<chrono::nanoseconds>(end - start).count();

  time_taken *= 1e-9;

  cerr << "\\nTime taken by program is : " << fixed << time_taken
       << setprecision(9);
  cerr << " sec" << endl;
#endif
  return 0;
}