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

#pragma comment(linker, "/stack:16777216")
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <exception>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <streambuf>
#include <string>
#include <utility>
#include <vector>
// #include"functions.h"
// #include"highprecision.h"
#define pb push_back
#define pf push_front
#define ppb pop_back
#define mp make_pair
#define np next_permutation
#define fi first
#define sc second
#define vi vector<int>
#define vll vector<long long>
#define vvi vector<vector<int>>
#define vpi vector<pair<int, int>>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define pff pair<float, float>
#define pdd pair<double, double>
#define psi pair<string, int>
#define pis pair<int, string>
#define ppi pair<pii, int>
#define pdi pair<pii, pii>
#define LN 84757
#define INF 1e9
#define LLINF 1e18
#define MAXINT 0x7fffffff
#define MAXLL 0x7fffffffffffffff
#define EPS 1e-5
#define DEPS 1e-9
#define LDEPS 1e-13
#define MOD 1000000007
#define E 2.7182818284
#define PI 3.1415926535
#define rep(a, b) for (int a = 0; a < b; a++)
#define rep(a, b) for (int a = 0; a < b; a++)
#define forn(a, n) for (int a = 1; a <= n; a++)
#define forr(a, n) for (int a = n; a >= 1; a--)
#define repn(a, n, p) for (int a = 0; a < n; a += p)
#define repr(a, n) for (int a = n - 1; a >= 0; a--)
#define foreach(a, b) for (__typeof(b.begin()) a = b.begin(); a != b.end(); a++)
#define fillemp(a) memset(a, 0, sizeof(a))
#define fillinf(a) memset(a, 0x3f, sizeof(a))
#define frestream                                                              \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout);
#define stdf(a, b)                                                             \\
  freopen(a, "r", stdin);                                                      \\
  freopen(b, "w", stdout);
#define inputf(a) freopen(a, "r", stdin);
#define outputf(a) freopen(a, "w", stdout);
#define sz(a) a.size()
#define all(a) a.begin(), a.end()
#define MOD 1000000007
typedef long long ll;
typedef long double ld;
using namespace std;

int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, 1, -1};
int h, w;
int dist[1010][1010];
string s[1010];
int main() {
  cin >> h >> w;
  int ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  rep(i, h) { cin >> s[i]; }
  rep(i, h) rep(j, w) dist[i][j] = INF;
  deque<pii> q;
  dist[ch][cw] = 0;
  q.push_back(mp(ch, cw));
  while (!q.empty()) {
    pii p = q.front();
    q.pop_front();
    int x = p.fi, y = p.sc;
    rep(i, 4) {
      int xx = x + dx[i], yy = y + dy[i];
      if (xx < 0 || xx >= h || yy < 0 || yy >= w) {
        continue;
      }
      if (s[xx][yy] == '#') {
        continue;
      }
      if (dist[xx][yy] <= dist[x][y]) {
        continue;
      }
      dist[xx][yy] = dist[x][y];
      q.push_front(mp(xx, yy));
    }
    for (int i = x - 2; i <= x + 2; i++) {
      for (int j = y - 2; j <= y + 2; j++) {
        if (i < 0 || i >= h || j < 0 || j >= w) {
          continue;
        }
        if (s[i][j] == '#') {
          continue;
        }
        if (dist[i][j] <= dist[x][y] + 1) {
          continue;
        }
        dist[i][j] = dist[x][y] + 1;
        q.push_back(mp(i, j));
      }
    }
  }
  if (dist[dh][dw] == INF) {
    cout << -1;
    return 0;
  }
  cout << dist[dh][dw];
  return 0;
}

Error: TLE

Fixed submission:

#pragma comment(linker, "/stack:16777216")
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <exception>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <streambuf>
#include <string>
#include <utility>
#include <vector>
// #include"functions.h"
// #include"highprecision.h"
#define pb push_back
#define pf push_front
#define ppb pop_back
#define mp make_pair
#define np next_permutation
#define fi first
#define sc second
#define vi vector<int>
#define vll vector<long long>
#define vvi vector<vector<int>>
#define vpi vector<pair<int, int>>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define pff pair<float, float>
#define pdd pair<double, double>
#define psi pair<string, int>
#define pis pair<int, string>
#define ppi pair<pii, int>
#define pdi pair<pii, pii>
#define LN 84757
#define INF 1e9
#define LLINF 1e18
#define MAXINT 0x7fffffff
#define MAXLL 0x7fffffffffffffff
#define EPS 1e-5
#define DEPS 1e-9
#define LDEPS 1e-13
#define MOD 1000000007
#define E 2.7182818284
#define PI 3.1415926535
#define rep(a, b) for (int a = 0; a < b; a++)
#define rep(a, b) for (int a = 0; a < b; a++)
#define forn(a, n) for (int a = 1; a <= n; a++)
#define forr(a, n) for (int a = n; a >= 1; a--)
#define repn(a, n, p) for (int a = 0; a < n; a += p)
#define repr(a, n) for (int a = n - 1; a >= 0; a--)
#define foreach(a, b) for (__typeof(b.begin()) a = b.begin(); a != b.end(); a++)
#define fillemp(a) memset(a, 0, sizeof(a))
#define fillinf(a) memset(a, 0x3f, sizeof(a))
#define frestream                                                              \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout);
#define stdf(a, b)                                                             \\
  freopen(a, "r", stdin);                                                      \\
  freopen(b, "w", stdout);
#define inputf(a) freopen(a, "r", stdin);
#define outputf(a) freopen(a, "w", stdout);
#define sz(a) a.size()
#define all(a) a.begin(), a.end()
#define MOD 1000000007
typedef long long ll;
typedef long double ld;
using namespace std;

int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, 1, -1};
int h, w;
int dist[1010][1010];
string s[1010];
int main() {
  cin >> h >> w;
  int ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  rep(i, h) { cin >> s[i]; }
  rep(i, h) rep(j, w) dist[i][j] = INF;
  queue<pii> q;
  dist[ch][cw] = 0;
  q.push(mp(ch, cw));
  while (!q.empty()) {
    pii p = q.front();
    q.pop();
    int x = p.fi, y = p.sc;
    rep(i, 4) {
      int xx = x + dx[i], yy = y + dy[i];
      if (xx < 0 || xx >= h || yy < 0 || yy >= w) {
        continue;
      }
      if (s[xx][yy] == '#') {
        continue;
      }
      if (dist[xx][yy] <= dist[x][y]) {
        continue;
      }
      dist[xx][yy] = dist[x][y];
      q.push(mp(xx, yy));
    }
    for (int i = x - 2; i <= x + 2; i++) {
      for (int j = y - 2; j <= y + 2; j++) {
        if (i < 0 || i >= h || j < 0 || j >= w) {
          continue;
        }
        if (s[i][j] == '#') {
          continue;
        }
        if (dist[i][j] <= dist[x][y] + 1) {
          continue;
        }
        dist[i][j] = dist[x][y] + 1;
        q.push(mp(i, j));
      }
    }
  }
  if (dist[dh][dw] == INF) {
    cout << -1;
    return 0;
  }
  cout << dist[dh][dw];
  return 0;
}