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

// clear adj and visited vector declared globally after each test case
// check for long long overflow
// while adding and subs check if mod becomes -ve
// while using an integer directly in a builtin function add ll
// Mod wale question mein last mein if dalo ie. Ans<0 then ans+=mod;
// Dont keep array name as size or any other key word

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
// #define int long long
#define IOS                                                                    \\
  std::ios::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);                                                              \\
  cout.precision(dbl::max_digits10);
#define pb push_back
#define mod 1000000007 // 998244353
#define lld long double
#define mii map<int, int>
#define mci map<char, int>
#define msi map<string, int>
#define pii pair<int, int>
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define rep(i, x, y) for (int i = x; i < y; i++)
#define fill(a, b) memset(a, b, sizeof(a))
#define vi vector<int>
#define setbits(x) __builtin_popcountll(x)
#define print2d(dp, n, m)                                                      \\
  for (int i = 0; i <= n; i++) {                                               \\
    for (int j = 0; j <= m; j++)                                               \\
      cout << dp[i][j] << " ";                                                 \\
    cout << "\\n";                                                              \\
  }
typedef std::numeric_limits<double> dbl;
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;
const long long N = 100005, INF = (int)1e9;
lld pi = 3.1415926535897932;
int lcm(int a, int b) {
  int g = __gcd(a, b);
  return a / g * b;
}
int power(int a, int b, int p) {
  if (a == 0)
    return 0;
  int res = 1;
  a %= p;
  while (b > 0) {
    if (b & 1)
      res = (res * a) % p;
    b >>= 1;
    a = (a * a) % p;
  }
  return res;
}
vector<int> v[N], temp;
int ch[N], n;
void dfs(int u, int p) {
  ch[u] = 1;
  for (auto to : v[u]) {
    if (to == p)
      continue;
    dfs(to, u);
    ch[u] += ch[to];
    // cout<<u<<" "<<to<<" "<<ch[to]<<" "<<n-ch[to]<<"\\n";
    temp.pb((ch[to] * (n - ch[to])));
  }
  return;
}
int h, w;
string s[1001];
int dp[1002][1002];
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
bool valid(int x, int y) {
  if (x >= 0 && x < h && y >= 0 && y < w && s[x][y] == '.')
    return true;
  return false;
}
int32_t main() {
  IOS;
  cin >> h >> w;
  int x1, x2, y1, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  x2--;
  y1--;
  y2--;
  for (int i = 0; i < h; i++)
    cin >> s[i];
  rep(i, 0, h) rep(j, 0, w) dp[i][j] = INF;
  deque<pair<pii, int>> q;
  vector<pii> temp;
  for (int i = -2; i < 3; i++) {
    for (int j = -2; j < 3; j++) {
      if (i != 0 || j != 0) {
        temp.pb({i, j});
        // cout<<i<<" "<<j<<"\\n";
      }
    }
  }
  q.push_front({{x1, y1}, 0});
  dp[x1][y1] = 0;
  while (!q.empty()) {
    pair<pii, int> p = q.front();
    int x = p.ff.ff, y = p.ff.ss;
    q.pop_front();
    for (int i = 0; i < 4; i++) {
      if (valid(x + dx[i], y + dy[i]) && dp[x + dx[i]][y + dy[i]] > p.ss) {
        dp[x + dx[i]][y + dy[i]] = p.ss;
        q.push_front({{x + dx[i], y + dy[i]}, p.ss});
      }
    }
    for (int i = 0; i < 24; i++) {
      if (valid(x + temp[i].ff, y + temp[i].ss) &&
          dp[x + temp[i].ff][y + temp[i].ss] > (p.ss + 1)) {
        dp[x + temp[i].ff][y + temp[i].ss] = p.ss + 1;
        q.push_back({{x + temp[i].ff, y + temp[i].ss}, p.ss + 1});
      }
    }
  }
  if (dp[x2][y2] == INF)
    cout << -1;
  else
    cout << dp[x2][y2];
}

Error: TLE

Fixed submission:

// clear adj and visited vector declared globally after each test case
// check for long long overflow
// while adding and subs check if mod becomes -ve
// while using an integer directly in a builtin function add ll
// Mod wale question mein last mein if dalo ie. Ans<0 then ans+=mod;
// Dont keep array name as size or any other key word

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define int long long
#define IOS                                                                    \\
  std::ios::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);                                                              \\
  cout.precision(dbl::max_digits10);
#define pb push_back
#define mod 1000000007 // 998244353
#define lld long double
#define mii map<int, int>
#define mci map<char, int>
#define msi map<string, int>
#define pii pair<int, int>
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define rep(i, x, y) for (int i = x; i < y; i++)
#define fill(a, b) memset(a, b, sizeof(a))
#define vi vector<int>
#define setbits(x) __builtin_popcountll(x)
#define print2d(dp, n, m)                                                      \\
  for (int i = 0; i <= n; i++) {                                               \\
    for (int j = 0; j <= m; j++)                                               \\
      cout << dp[i][j] << " ";                                                 \\
    cout << "\\n";                                                              \\
  }
typedef std::numeric_limits<double> dbl;
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;
const long long N = 100005, INF = 2000000000000000000;
lld pi = 3.1415926535897932;
int lcm(int a, int b) {
  int g = __gcd(a, b);
  return a / g * b;
}
int power(int a, int b, int p) {
  if (a == 0)
    return 0;
  int res = 1;
  a %= p;
  while (b > 0) {
    if (b & 1)
      res = (res * a) % p;
    b >>= 1;
    a = (a * a) % p;
  }
  return res;
}
vector<int> v[N], temp;
int ch[N], n;
void dfs(int u, int p) {
  ch[u] = 1;
  for (auto to : v[u]) {
    if (to == p)
      continue;
    dfs(to, u);
    ch[u] += ch[to];
    // cout<<u<<" "<<to<<" "<<ch[to]<<" "<<n-ch[to]<<"\\n";
    temp.pb((ch[to] * (n - ch[to])));
  }
  return;
}
int h, w;
string s[1001];
int dp[1002][1002];
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
bool valid(int x, int y) {
  if (x >= 0 && x < h && y >= 0 && y < w && s[x][y] == '.')
    return true;
  return false;
}
int32_t main() {
  IOS;
  cin >> h >> w;
  int x1, x2, y1, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  x2--;
  y1--;
  y2--;
  for (int i = 0; i < h; i++)
    cin >> s[i];
  rep(i, 0, h) rep(j, 0, w) dp[i][j] = INF;
  queue<pair<pii, int>> q;
  vector<pii> temp;
  for (int i = -2; i < 3; i++) {
    for (int j = -2; j < 3; j++) {
      if (i != 0 || j != 0) {
        temp.pb({i, j});
        // cout<<i<<" "<<j<<"\\n";
      }
    }
  }
  q.push({{x1, y1}, 0});
  dp[x1][y1] = 0;
  while (!q.empty()) {
    pair<pii, int> p = q.front();
    int x = p.ff.ff, y = p.ff.ss;
    q.pop();
    for (int i = 0; i < 4; i++) {
      if (valid(x + dx[i], y + dy[i]) && dp[x + dx[i]][y + dy[i]] > p.ss) {
        dp[x + dx[i]][y + dy[i]] = p.ss;
        q.push({{x + dx[i], y + dy[i]}, p.ss});
      }
    }
    for (int i = 0; i < 24; i++) {
      if (valid(x + temp[i].ff, y + temp[i].ss) &&
          dp[x + temp[i].ff][y + temp[i].ss] > (p.ss + 1)) {
        dp[x + temp[i].ff][y + temp[i].ss] = p.ss + 1;
        q.push({{x + temp[i].ff, y + temp[i].ss}, p.ss + 1});
      }
    }
  }
  if (dp[x2][y2] == INF)
    cout << -1;
  else
    cout << dp[x2][y2];
}