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

#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops,no-stack-protector")
#pragma GCC target("sse,sse2,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define watch(x) cout << (#x) << " is " << (x) << endl
#define debug cout << "hi" << endl

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;

ll gcd(ll a, ll b) { return (!b ? a : gcd(b, a % b)); }
ll lcm(ll a, ll b) { return ((a * b) / gcd(a, b)); }
bool cmp(int a, int b) { return a > b; }

const ll mod = 1e9 + 7;
const int INF32 = 1 << 30;
const ll INF64 = 1LL << 60;
const ld pi = 3.141592653589793;
const int N = 1e6 + 10;

long long modpow(long long n, long long k, long long mod) {
  if (k == 0)
    return 1;
  long long r = modpow(n * n % mod, k >> 1, mod);
  if (k & 1)
    r = r * n % mod;
  return r;
}
const int dx[4] = {0, 0, 1, -1};
const int dy[4] = {1, -1, 0, 0};
void solve() {
  int h, w, x1, y1, x2, y2;
  cin >> h >> w >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;
  vector<string> s(h);
  for (int i = 0; i < h; i++)
    cin >> s[i];
  deque<pii> q;
  vector<vector<int>> dis(h, vector<int>(w, -1));
  dis[x1][y1] = 0;
  q.push_back({x1, y1});
  while (!q.empty()) {
    auto pos = q.front();
    q.pop_front();
    int x = pos.first;
    int y = pos.second;
    for (int i = 0; i < 4; i++) {
      int nx = x + dx[i];
      int ny = y + dy[i];
      if (nx < 0 || ny < 0 || nx >= h || ny >= w)
        continue;
      if (s[nx][ny] == '#')
        continue;
      if (dis[nx][ny] <= dis[x][y] && dis[nx][ny] != -1)
        continue;
      dis[nx][ny] = dis[x][y];
      q.push_front({nx, ny});
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int nx = x + i;
        int ny = y + j;
        if (nx < 0 || ny < 0 || nx >= h || ny >= w)
          continue;
        if (s[nx][ny] == '#')
          continue;
        if (dis[nx][ny] <= dis[x][y] + 1 && dis[nx][ny] != -1)
          continue;
        dis[nx][ny] = dis[x][y] + 1;
        q.push_back({nx, ny});
      }
    }
  }
  cout << dis[x2][y2];
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  solve();
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops,no-stack-protector")
#pragma GCC target("sse,sse2,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define watch(x) cout << (#x) << " is " << (x) << endl
#define debug cout << "hi" << endl

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;

ll gcd(ll a, ll b) { return (!b ? a : gcd(b, a % b)); }
ll lcm(ll a, ll b) { return ((a * b) / gcd(a, b)); }
bool cmp(int a, int b) { return a > b; }

const ll mod = 1e9 + 7;
const int INF32 = 1 << 30;
const ll INF64 = 1LL << 60;
const ld pi = 3.141592653589793;
const int N = 1e6 + 10;

long long modpow(long long n, long long k, long long mod) {
  if (k == 0)
    return 1;
  long long r = modpow(n * n % mod, k >> 1, mod);
  if (k & 1)
    r = r * n % mod;
  return r;
}
const int dx[4] = {0, 0, 1, -1};
const int dy[4] = {1, -1, 0, 0};
void solve() {
  int h, w, x1, y1, x2, y2;
  cin >> h >> w >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;
  vector<string> s(h);
  for (int i = 0; i < h; i++)
    cin >> s[i];
  queue<pii> q;
  vector<vector<int>> dis(h, vector<int>(w, -1));
  dis[x1][y1] = 0;
  q.push({x1, y1});
  while (!q.empty()) {
    auto pos = q.front();
    q.pop();
    int x = pos.first;
    int y = pos.second;
    /*if(x==x2&&y==y2){
        cout << dis[x][y];
        return;
    }*/
    for (int i = 0; i < 4; i++) {
      int nx = x + dx[i];
      int ny = y + dy[i];
      if (nx < 0 || ny < 0 || nx >= h || ny >= w)
        continue;
      if (s[nx][ny] == '#')
        continue;
      if (dis[nx][ny] <= dis[x][y] && dis[nx][ny] != -1)
        continue;
      dis[nx][ny] = dis[x][y];
      q.push({nx, ny});
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int nx = x + i;
        int ny = y + j;
        if (nx < 0 || ny < 0 || nx >= h || ny >= w)
          continue;
        if (s[nx][ny] == '#')
          continue;
        if (dis[nx][ny] <= dis[x][y] + 1 && dis[nx][ny] != -1)
          continue;
        dis[nx][ny] = dis[x][y] + 1;
        q.push({nx, ny});
      }
    }
  }
  cout << dis[x2][y2];
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  solve();
  return 0;
}