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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <vector>
#pragma GCC optimize(3, "Ofast", "inline")
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
using namespace std;
#define ios ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define x first
#define y second
#define pb push_back
#define ls rt << 1
#define rs rt << 1 | 1
typedef long long ll;
const double esp = 1e-6, pi = acos(-1);
typedef pair<int, int> PII;
const int N = 1e3 + 10, INF = 0x3f3f3f3f, mod = 1e9 + 7;
int d[4][2] = {0, 1, 0, -1, 1, 0, -1, 0};
char s[N][N];
int dis[N][N];
int work() {
  int n, m, cx, cy, dx, dy;
  scanf("%d%d%d%d%d%d", &n, &m, &cx, &cy, &dx, &dy);
  for (int i = 1; i <= n; i++)
    scanf("%s", s[i] + 1);
  memset(dis, 0x3f, sizeof dis);
  dis[cx][cy] = 0;
  deque<PII> q;
  q.push_back({cx, cy});
  while (q.size()) {
    int x = q.front().x, y = q.front().y;
    q.pop_front();
    for (int i = 0; i < 4; i++) {
      int tx = x + d[i][0], ty = y + d[i][1];
      if (tx < 1 || ty < 1 || tx > n || ty > m || s[tx][ty] == '#')
        continue;
      if (dis[tx][ty] > dis[x][y]) {
        dis[tx][ty] = dis[x][y];
        q.push_front({tx, ty});
      }
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int tx = x + i, ty = y + j;
        if (tx < 1 || ty < 1 || tx > n || ty > m || s[tx][ty] == '#')
          continue;
        if (dis[tx][ty] > dis[x][y] + 1) {
          dis[tx][ty] = dis[x][y] + 1;
          q.push_back({tx, ty});
        }
      }
    }
  }
  if (dis[dx][dy] == INF)
    dis[dx][dy] = -1;
  cout << dis[dx][dy] << endl;
  return 0;
}
int main() {
  //   freopen("C:UsersspnooyseedDesktopin.txt" , "r" , stdin) ;
  //   freopen("C:UsersspnooyseedDesktopout.txt" , "w" , stdout) ;

  work();
  return 0;
}
/*
 */


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <vector>
#pragma GCC optimize(3, "Ofast", "inline")
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
using namespace std;
#define ios ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define x first
#define y second
#define pb push_back
#define ls rt << 1
#define rs rt << 1 | 1
typedef long long ll;
const double esp = 1e-6, pi = acos(-1);
typedef pair<int, int> PII;
const int N = 1e3 + 10, INF = 0x3f3f3f3f, mod = 1e9 + 7;
int d[4][2] = {0, 1, 0, -1, 1, 0, -1, 0};
char s[N][N];
int dis[N][N];
int work() {
  int n, m, cx, cy, dx, dy;
  scanf("%d%d%d%d%d%d", &n, &m, &cx, &cy, &dx, &dy);
  for (int i = 1; i <= n; i++)
    scanf("%s", s[i] + 1);
  memset(dis, 0x3f, sizeof dis);
  dis[cx][cy] = 0;
  queue<PII> q;
  q.push({cx, cy});
  while (q.size()) {
    int x = q.front().x, y = q.front().y;
    q.pop();
    for (int i = 0; i < 4; i++) {
      int tx = x + d[i][0], ty = y + d[i][1];
      if (tx < 1 || ty < 1 || tx > n || ty > m || s[tx][ty] == '#')
        continue;
      if (dis[tx][ty] > dis[x][y]) {
        dis[tx][ty] = dis[x][y];
        q.push({tx, ty});
      }
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int tx = x + i, ty = y + j;
        if (tx < 1 || ty < 1 || tx > n || ty > m || s[tx][ty] == '#')
          continue;
        if (dis[tx][ty] > dis[x][y] + 1) {
          dis[tx][ty] = dis[x][y] + 1;
          q.push({tx, ty});
        }
      }
    }
  }
  if (dis[dx][dy] == INF)
    dis[dx][dy] = -1;
  cout << dis[dx][dy] << endl;
  return 0;
}
int main() {
  //   freopen("C:UsersspnooyseedDesktopin.txt" , "r" , stdin) ;
  //   freopen("C:UsersspnooyseedDesktopout.txt" , "w" , stdout) ;

  work();
  return 0;
}
/*
 */