Problem p02579 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<ll> vl;
typedef vector<int> vi;
typedef vector<vl> vvl;
typedef vector<vi> vvi;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define MOD 1000000007
#define F first
#define S second
#define INF int(1e9)
#ifdef SHUBHAM107
#include "../trace.h"
#else
#define trace(args...)
#endif

// vector<int> dx = {-1, 0, 0, 1};
// vector<int> dy = {0, 1, -1, 0};

void solve() {
  int h, w;
  cin >> h >> w;

  int ch, cw;
  cin >> ch >> cw;
  ch--, cw--;

  int dh, dw;
  cin >> dh >> dw;
  dh--, dw--;

  vector<string> mat(h);
  for (string &s : mat)
    cin >> s;
  // trace(mat);
  deque<pair<int, int>> q;
  vector<vector<int>> d(h, vector<int>(w, INF));

  d[ch][cw] = 0;
  q.push_back({ch, cw});

  while (!q.empty()) {
    int x = q.front().first, y = q.front().second;
    // trace(x, y);
    q.pop_front();

    for (int dx = -2; dx <= 2; dx++) {
      for (int dy = -2; dy <= 2; dy++) {
        int w1 = 1;
        if (dx * dx + dy * dy == 1)
          w1 = 0;

        int nx = x + dx;
        int ny = y + dy;

        if (nx < h && ny < w && nx >= 0 && ny >= 0) {
          // trace(nx, ny, 'a');
          if (mat[nx][ny] == '.') {
            // trace(nx, ny, 'b');
            if (d[nx][ny] > w1 + d[x][y]) {
              // trace(nx, ny);
              d[nx][ny] = w1 + d[x][y];
              if (w == 0) {
                q.push_front({nx, ny});
              } else {
                q.push_back({nx, ny});
              }
            }
          }
        }
      }
    }
  }

  if (d[dh][dw] >= INF) {
    cout << -1;
  } else {
    cout << d[dh][dw];
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifdef SHUBHAM107
  freopen("inp.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
  freopen("err.txt", "w", stderr);
#endif
  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
}


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

typedef long long ll;
typedef vector<ll> vl;
typedef vector<int> vi;
typedef vector<vl> vvl;
typedef vector<vi> vvi;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define MOD 1000000007
#define F first
#define S second
#define INF int(1e9)
#ifdef SHUBHAM107
#include "../trace.h"
#else
#define trace(args...)
#endif

// vector<int> dx = {-1, 0, 0, 1};
// vector<int> dy = {0, 1, -1, 0};

void solve() {
  int h, w;
  cin >> h >> w;

  int ch, cw;
  cin >> ch >> cw;
  ch--, cw--;

  int dh, dw;
  cin >> dh >> dw;
  dh--, dw--;

  vector<string> mat(h);
  for (string &s : mat)
    cin >> s;
  // trace(mat);
  deque<pair<int, int>> q;
  vector<vector<int>> d(h, vector<int>(w, INF));

  d[ch][cw] = 0;
  q.push_back({ch, cw});

  while (!q.empty()) {
    int x = q.front().first, y = q.front().second;
    // trace(x, y);
    q.pop_front();

    for (int dx = -2; dx <= 2; dx++) {
      for (int dy = -2; dy <= 2; dy++) {
        int w1 = 1;
        if (dx * dx + dy * dy == 1)
          w1 = 0;

        int nx = x + dx;
        int ny = y + dy;

        if (nx < h && ny < w && nx >= 0 && ny >= 0 && mat[nx][ny] == '.') {
          if (d[nx][ny] > w1 + d[x][y]) {
            d[nx][ny] = w1 + d[x][y];
            if (w1 == 0) {
              q.push_front({nx, ny});
            } else {
              q.push_back({nx, ny});
            }
          }
        }
      }
    }
  }

  if (d[dh][dw] >= INF) {
    cout << -1;
  } else {
    cout << d[dh][dw];
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifdef SHUBHAM107
  freopen("inp.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
  freopen("err.txt", "w", stderr);
#endif
  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
}