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

// #include <fsociety>
#include <algorithm>
#include <cmath>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
// end of libraries ;
//  freopen("input.txt","r",stdin);
//  freopen("output.txt","w",stdout);
//  ll lmax(ll o , ll w) {if(o>w)return o; return w;}
//  ll lmin(ll o , ll w) {if(o<w)return o; return w;}
#define LNF 3999999999999999999
#define INF 999999999
#define N 2000
#define PI 3.14159265359
#define endl "\\n"
#define F first
#define S second
#define pb push_back
#define ll long long
#define all(c) (c).begin(), (c).end()
#define sz(c) (int)(c).size()
#define mkp(a, b) make_pair(a, b)
#define fcin ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
using namespace std;
int h, w, ch, cw, dh, dw, dp[N][N], ans = INF;
char a[N][N];
int movex[] = {1, 0, -1, 0};
int movey[] = {0, 1, 0, -1};
set<pair<int, int>> q;
bool check(int x, int y) {
  if (x < 0 || y < 0)
    return 0;
  if (x >= h || y >= w)
    return 0;
  return 1;
}
int main() {
  fcin;
  cin >> h >> w;
  cin >> ch >> cw;
  cin >> dh >> dw;
  dh--;
  dw--;
  ch--;
  cw--;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++)
      cin >> a[i][j];
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      dp[i][j] = INF;
    }
  }
  dp[ch][cw] = 0;
  q.insert(mkp(ch, cw));
  while (sz(q) > 0) {
    auto it = q.begin();
    int x = it->F, y = it->S;
    q.erase(it);
    for (int i = max(0, x - 2); i <= min(h - 1, x + 2); i++) {
      for (int j = max(0, y - 2); j <= min(w - 1, y + 2); j++) {
        if (!check(i, j))
          continue;
        int d = abs(i - x) + abs(j - y) <= 1 ? 0 : 1;
        if (a[i][j] == '.' && dp[i][j] > dp[x][y] + d) {
          dp[i][j] = dp[x][y] + d;
          q.insert(mkp(i, j));
        }
      }
    }
  }
  if (dp[dh][dw] == INF)
    cout << "-1";
  else
    cout << dp[dh][dw] << '\\n';
}

Error: TLE

Fixed submission:

// #include <fsociety>
#include <algorithm>
#include <cmath>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
// end of libraries ;
//  freopen("input.txt","r",stdin);
//  freopen("output.txt","w",stdout);
//  ll lmax(ll o , ll w) {if(o>w)return o; return w;}
//  ll lmin(ll o , ll w) {if(o<w)return o; return w;}
#define LNF 3999999999999999999
#define INF 999999999
#define N 2000
#define PI 3.14159265359
#define endl "\\n"
#define F first
#define S second
#define pb push_back
#define ll long long
#define all(c) (c).begin(), (c).end()
#define sz(c) (int)(c).size()
#define mkp(a, b) make_pair(a, b)
#define fcin ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
using namespace std;
int h, w, ch, cw, dh, dw, dp[N][N], ans = INF;
char a[N][N];
int movex[] = {1, 0, -1, 0};
int movey[] = {0, 1, 0, -1};
deque<pair<int, int>> q;
bool check(int x, int y) {
  if (x < 0 || y < 0)
    return 0;
  if (x >= h || y >= w)
    return 0;
  return 1;
}
int main() {
  fcin;
  cin >> h >> w;
  cin >> ch >> cw;
  cin >> dh >> dw;
  dh--;
  dw--;
  ch--;
  cw--;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++)
      cin >> a[i][j];
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      dp[i][j] = INF;
    }
  }
  dp[ch][cw] = 0;
  q.push_back(mkp(ch, cw));
  while (sz(q) > 0) {
    int x = q.front().F, y = q.front().S;
    q.pop_front();
    for (int i = max(0, x - 2); i <= min(h - 1, x + 2); i++) {
      for (int j = max(0, y - 2); j <= min(w - 1, y + 2); j++) {
        if (!check(i, j))
          continue;
        int d = abs(i - x) + abs(j - y) <= 1 ? 0 : 1;
        if (a[i][j] == '.' && dp[i][j] > dp[x][y] + d) {
          dp[i][j] = dp[x][y] + d;
          q.push_back(mkp(i, j));
        }
      }
    }
  }
  if (dp[dh][dw] == INF)
    cout << "-1";
  else
    cout << dp[dh][dw] << '\\n';
}