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

#include <bits/stdc++.h>
using namespace std;

#define ALL(a) (a.begin(), a.end())
#define ll long long int
#define ff first
#define ss second
#define pb push_back
#define ar array
#define mp make_pair
#define f(i, a, b) for (ll i = a; i <= b; i++)
#define inf INT_MAX
#define pll pair<ll, ll>
#define mat vector<vector<ll>>

const ll INF = 1e18;
void init() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

bool checkp2(ll n) { return n && !(n & (n - 1)); }

ll numsetbit(ll n) {
  ll count = 0;
  for (; n; count++) {
    n = n & (n - 1);
  }
  return count;
}

ll power(ll x, ll n, ll mod) {
  x = x % mod;
  ll po = 1;
  while (n) {
    if (n & 1) {
      po = (po * x) % mod;
    }
    n = n >> 1;
    x = ((x % mod) * (x % mod)) % mod;
  }
  return po;
}

bool isPrime(ll x) {
  if (x < 2)
    return 0;
  for (ll y = 2; y * y <= x; y++) {
    if (x % y == 0) {
      return 0;
    }
  }
  return true;
}
const ll mxn = 1e3;
ll H, W, c1, c2, d1, d2;
string s[mxn];
vector<vector<ll>> d;
ll vis[mxn + 10][mxn + 10];

bool e(ll i, ll j) {
  return i >= 0 && i < H && j >= 0 && j < W && s[i][j] == '.';
}

void solve() {

  d = vector<vector<ll>>(mxn + 10, vector<ll>(mxn + 10, 3e9));
  cin >> H >> W;
  cin >> c1 >> c2 >> d1 >> d2;
  --c1, --c2, --d1, --d2;

  for (ll i = 0; i < H; i++) {
    cin >> s[i];
  }

  priority_queue<ar<ll, 3>, vector<ar<ll, 3>>, greater<ar<ll, 3>>> pq;

  pq.push({0, c1, c2}); // move , starting , ending
  d[c1][c2] = 0;

  while (!pq.empty()) {

    ar<ll, 3> cur = pq.top();
    pq.pop();

    ll i = cur[1], j = cur[2], d1 = cur[0];

    if (e(i - 1, j)) {

      if (d[i - 1][j] > d[i][j]) {
        d[i - 1][j] = d[i][j];
        pq.push({d[i - 1][j], i - 1, j});
      }
    }
    if (e(i + 1, j)) {

      if (d[i + 1][j] > d[i][j]) {
        d[i + 1][j] = d[i][j];
        pq.push({d[i + 1][j], i + 1, j});
      }
    }
    if (e(i, j - 1)) {
      if (d[i][j - 1] > d[i][j]) {
        d[i][j - 1] = d[i][j];
        pq.push({d[i][j - 1], i, j - 1});
      }
    }
    if (e(i, j + 1)) {
      if (d[i][j + 1] > d[i][j]) {
        d[i][j + 1] = d[i][j];
        pq.push({d[i][j + 1], i, j + 1});
      }
    }

    for (ll i1 = i - 2; i1 <= i + 2; i1++) {
      for (ll j1 = j - 2; j1 <= j + 2; j1++) {

        if (e(i1, j1)) {
          // cout<<"kdnv";
          if (d[i1][j1] > d[i][j] + 1) {
            d[i1][j1] = d[i][j] + 1;
            pq.push({d[i1][j1], i1, j1});
          }
        }
      }
    }
  }

  if (d[d1][d2] >= 2e9 + 100) {
    cout << "-1\\n";
  } else {
    cout << d[d1][d2] << endl;
    return;
  }
}

int main() {

  init();
  ll t = 1;
  // cin>>t;
  for (ll i = 1; i <= t; i++) {
    // cout<<"Case #"<<i<<": ";
    solve();
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define ALL(a) (a.begin(), a.end())
#define ll long long int
#define ff first
#define ss second
#define pb push_back
#define ar array
#define mp make_pair
#define f(i, a, b) for (ll i = a; i <= b; i++)
#define inf INT_MAX
#define pll pair<ll, ll>
#define mat vector<vector<ll>>

const ll INF = 1e18;
void init() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

bool checkp2(ll n) { return n && !(n & (n - 1)); }

ll numsetbit(ll n) {
  ll count = 0;
  for (; n; count++) {
    n = n & (n - 1);
  }
  return count;
}

ll power(ll x, ll n, ll mod) {
  x = x % mod;
  ll po = 1;
  while (n) {
    if (n & 1) {
      po = (po * x) % mod;
    }
    n = n >> 1;
    x = ((x % mod) * (x % mod)) % mod;
  }
  return po;
}

bool isPrime(ll x) {
  if (x < 2)
    return 0;
  for (ll y = 2; y * y <= x; y++) {
    if (x % y == 0) {
      return 0;
    }
  }
  return true;
}
const ll mxn = 1e3 + 10;
ll H, W, c1, c2, d1, d2;
string s[mxn];
ll d[mxn + 10][mxn + 10];
ll vis[mxn + 10][mxn + 10];

bool e(ll i, ll j) {
  return i >= 0 && i < H && j >= 0 && j < W && s[i][j] == '.';
}

void dfs(ll i = c1, ll j = c2) {
  vis[i][j] = 1;
  if (e(i - 1, j)) {
    bool flag = 0;
    if (d[i - 1][j] > d[i][j]) {
      flag = 1;
    }
    d[i - 1][j] = min(d[i][j], d[i - 1][j]);
    if (flag)
      dfs(i - 1, j);
  }
  if (e(i + 1, j)) {
    bool flag = 0;
    if (d[i + 1][j] > d[i][j])
      flag = 1;
    d[i + 1][j] = min(d[i][j], d[i + 1][j]);
    if (flag)
      dfs(i + 1, j);
  }
  if (e(i, j - 1)) {
    bool flag = 0;
    if (d[i][j - 1] > d[i][j]) {
      flag = 1;
    }
    d[i][j - 1] = min(d[i][j], d[i][j - 1]);
    if (flag)
      dfs(i, j - 1);
  }
  if (e(i, j + 1)) {
    bool flag = 0;
    if (d[i][j + 1] > d[i][j])
      flag = 1;

    d[i][j + 1] = min(d[i][j], d[i][j + 1]);
    if (flag)
      dfs(i, j + 1);
  }
  for (ll i1 = i - 2; i1 <= i + 2; i1++) {
    for (ll j1 = j - 2; j1 <= j + 2; j1++) {
      if (i1 == i && j1 == j)
        continue;
      if (e(i1, j1)) {
        bool flag = 0;
        if (d[i1][j1] > d[i][j] + 1)
          flag = 1;
        d[i1][j1] = min(d[i1][j1], d[i][j] + 1);

        if (flag)
          dfs(i1, j1);
      }
    }
  }
  return;
}

void solve() {
  for (ll i = 0; i < mxn + 10; i++) {
    for (ll j = 0; j < mxn + 10; j++) {
      d[i][j] = INF;
    }
  }
  memset(vis, 0, sizeof(vis));
  cin >> H >> W;
  cin >> c1 >> c2 >> d1 >> d2;
  --c1, --c2, --d1, --d2;
  for (ll i = 0; i < H; i++) {
    cin >> s[i];
  }
  d[c1][c2] = 0;
  dfs();

  /*for(ll i = 0;i<H;i++){
          for(ll j = 0;j<W;j++){
                  if(d[i][j] >= INF){
                          cout<<"-1 ";
                  }
                  else{
                          cout<<d[i][j]<<" ";
                  }
          }
          cout<<endl;
  }
  */
  if (d[d1][d2] >= INF) {
    cout << "-1" << endl;
  } else {
    cout << d[d1][d2] << endl;
    return;
  }
  /*queue<ar<ll,2>> q;
  q.push({d1,d2});
  while(!q.empty()){
          auto cur = q.front();
          q.pop();

          for(ll i = cur[0] - 4; i<= cur[0] + 4;i++){
                  for(ll j = cur[0]-4;j<=cur[0]+4;j++){
                          if(e(i ,j)){
                                  d[i][j] = min(d[cur[0]][cur[1]] + 1 ,
  d[i][j]);
                          }
                  }
          }
          ll i = cur[0];

          ll j = cur[1];
          if(e(i-1 , j)){
                  d[i-1][j] = min(d[cur[0]][cur[1]] , d[i-1][j]);
                  q.push({i-1 , j});
          }
          if(e(i+1 , j)){
                  d[i+1][j] = min(d[cur[0]][cur[1]] , d[i+1][j]);
                  q.push({i+1 , j});
          }
          if(e(i , j-1)){
                  d[i][j-1] = min(d[cur[0]][cur[1]] , d[i][j-1]);
                  q.push({i,j-1});
          }
          if(e(i, j+1)){
                  d[i][j+1] = min(d[cur[0]][cur[1]] , d[i][j+1]);
                  q.push({i, j+1});
          }


  }
  */
}

int main() {

  init();
  ll t = 1;
  // cin>>t;
  for (ll i = 1; i <= t; i++) {
    // cout<<"Case #"<<i<<": ";
    solve();
  }
}