Problem p02579 (C++)

Buggy submission (status: Time Limit Exceeded):
//
//  main.cpp
//  ervb
//
//  Created by Kanak Gautam on 21/04/20.
//  Copyright © 2020 Kanak Gautam. All rights reserved.
//

#include <algorithm>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define pb push_back
#define mk make_pair
#define endl "\n"
#define mod 1000000007
using namespace std;
typedef long long int lli;
typedef long double ld;
priority_queue<lli, vector<lli>, greater<lli>> ti;
vector<lli> p[200005], h, b(200005, 0), f(200005, 0);
lli vis[200005];
map<pair<lli, lli>, lli> mp;
set<pair<lli, lli>> s;
set<lli> st;
map<lli, lli> np, v;
queue<lli> qy;
lli gcd(lli a, lli b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
lli bpow(lli a, lli b) {
  lli res = 1;
  while (b > 0) {
    if (b & 1)
      res = (res * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return res % mod;
}
lli fact(lli i) {
  f[0] = 1;
  for (lli k = 1; k <= i; k++) {
    (f[k] = f[k - 1] * k) %= mod;
  }
  return f[i];
}
lli isprime(lli n) {
  if (n == 1)
    return 0;
  for (lli i = 2; i <= sqrt(n); i++)
    if (n % i == 0)
      return 0;
  return 1;
}
bool cmp(lli i, lli j) { return p[i].size() < p[j].size(); }
void sieve() {
  for (lli i = 2; i <= 5000000; i++) {
    if (b[i] == 0) {
      for (lli j = 2; i * j <= 5000000; j++) {
        b[i * j] = 1;
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  lli h, w;
  cin >> h >> w;
  lli cx, cy, dx, dy;
  cin >> cx >> cy >> dx >> dy;
  lli a1[] = {0, 0, -1, 1};
  lli a2[] = {1, -1, 0, 0};
  char s[h + 1][w + 1];
  map<pair<lli, lli>, lli> mp;
  queue<pair<lli, lli>> q1, q2;
  vector<vector<lli>> dp(h + 1, vector<lli>(w + 1, 1e8));
  dp[cx][cy] = 0;
  for (lli i = 1; i <= h; i++) {
    for (lli j = 1; j <= w; j++) {
      cin >> s[i][j];
    }
  }
  q1.push(make_pair(cx, cy));
  while (!q1.empty()) {
    lli x = q1.front().first;
    lli y = q1.front().second;
    q1.pop();
    if (mp[{x, y}] == 0) {
      // cout<<x<<" "<<y<<endl;
      q2.push(make_pair(x, y));
      while (!q2.empty()) {
        x = q2.front().first;
        y = q2.front().second;
        // cout<<x<<" "<<y<<endl;
        mp[{x, y}] = 1;
        lli x1, y1;
        for (lli i = 0; i < 4; i++) {
          x1 = x + a1[i];
          y1 = y + a2[i];
          if (x1 >= 1 && x1 <= h && y1 >= 1 && y1 <= w && s[x1][y1] == '.' &&
              !mp[{x1, y1}]) {
            // cout<<1<<" "<<x1<<" "<<y1<<endl;
            dp[x1][y1] = min(dp[x][y], dp[x1][y1]);
            q2.push(make_pair(x1, y1));
          }
        }
        q2.pop();
        for (lli i = max(x - 2, 1ll); i <= min(h, x + 2); i++) {
          for (lli j = max(y - 2, 1ll); j <= min(w, y + 2); j++) {
            if (s[i][j] == '.' && !mp[{i, j}]) {
              // cout<<0<<" "<<i<<" "<<j<<endl;
              dp[i][j] = min(dp[x][y] + 1, dp[i][j]);
              q1.push(make_pair(i, j));
            }
          }
        }
      }
    }
  }
  /* for(lli i=1;i<=h;i++)
   {
       for(lli j=1;j<=w;j++)
       {
           cout<<dp[i][j]<<" ";
       }
       cout<<endl;
   }*/
  if (dp[dx][dy] == 1e8)
    dp[dx][dy] = -1;
  cout << dp[dx][dy] << endl;
}


Fixed submission:
//
//  main.cpp
//  ervb
//
//  Created by Kanak Gautam on 21/04/20.
//  Copyright © 2020 Kanak Gautam. All rights reserved.
//

#include <algorithm>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define pb push_back
#define mk make_pair
#define endl "\n"
#define mod 1000000007
using namespace std;
typedef long long int lli;
typedef long double ld;
priority_queue<lli, vector<lli>, greater<lli>> ti;
vector<lli> p[200005], h, b(200005, 0), f(200005, 0);
lli vis[200005];
map<pair<lli, lli>, lli> mp;
set<pair<lli, lli>> s;
set<lli> st;
map<lli, lli> np, v;
queue<lli> qy;
lli gcd(lli a, lli b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
lli bpow(lli a, lli b) {
  lli res = 1;
  while (b > 0) {
    if (b & 1)
      res = (res * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return res % mod;
}
lli fact(lli i) {
  f[0] = 1;
  for (lli k = 1; k <= i; k++) {
    (f[k] = f[k - 1] * k) %= mod;
  }
  return f[i];
}
lli isprime(lli n) {
  if (n == 1)
    return 0;
  for (lli i = 2; i <= sqrt(n); i++)
    if (n % i == 0)
      return 0;
  return 1;
}
bool cmp(lli i, lli j) { return p[i].size() < p[j].size(); }
void sieve() {
  for (lli i = 2; i <= 5000000; i++) {
    if (b[i] == 0) {
      for (lli j = 2; i * j <= 5000000; j++) {
        b[i * j] = 1;
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  lli h, w;
  cin >> h >> w;
  lli cx, cy, dx, dy;
  cin >> cx >> cy >> dx >> dy;
  lli a1[] = {0, 0, -1, 1};
  lli a2[] = {1, -1, 0, 0};
  char s[h + 1][w + 1];
  map<pair<lli, lli>, lli> mp;
  queue<pair<lli, lli>> q1, q2;
  vector<vector<lli>> dp(h + 1, vector<lli>(w + 1, 1e8));
  dp[cx][cy] = 0;
  for (lli i = 1; i <= h; i++) {
    for (lli j = 1; j <= w; j++) {
      cin >> s[i][j];
    }
  }
  q1.push(make_pair(cx, cy));
  while (!q1.empty()) {
    lli x = q1.front().first;
    lli y = q1.front().second;
    q1.pop();
    if (mp[{x, y}] == 0) {
      // cout<<x<<" "<<y<<endl;
      q2.push(make_pair(x, y));
      while (!q2.empty()) {
        x = q2.front().first;
        y = q2.front().second;
        // cout<<x<<" "<<y<<endl;
        mp[{x, y}] = 1;
        lli x1, y1;
        for (lli i = 0; i < 4; i++) {
          x1 = x + a1[i];
          y1 = y + a2[i];
          if (x1 >= 1 && x1 <= h && y1 >= 1 && y1 <= w && s[x1][y1] == '.' &&
              !mp[{x1, y1}]) {
            // cout<<1<<" "<<x1<<" "<<y1<<endl;
            dp[x1][y1] = min(dp[x][y], dp[x1][y1]);
            q2.push(make_pair(x1, y1));
            mp[{x1, y1}] = 1;
          }
        }
        q2.pop();
        for (lli i = max(x - 2, 1ll); i <= min(h, x + 2); i++) {
          for (lli j = max(y - 2, 1ll); j <= min(w, y + 2); j++) {
            if (s[i][j] == '.' && !mp[{i, j}]) {
              // cout<<0<<" "<<i<<" "<<j<<endl;
              dp[i][j] = min(dp[x][y] + 1, dp[i][j]);
              q1.push(make_pair(i, j));
            }
          }
        }
      }
    }
  }
  /* for(lli i=1;i<=h;i++)
   {
       for(lli j=1;j<=w;j++)
       {
           cout<<dp[i][j]<<" ";
       }
       cout<<endl;
   }*/
  if (dp[dx][dy] == 1e8)
    dp[dx][dy] = -1;
  cout << dp[dx][dy] << endl;
}