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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FAST                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);
const double EPS = 1e-9;
const double PI = acos(-1);
const int knightDir[8][2] = {{-2, -1}, {-2, 1}, {-1, 2},  {1, 2},
                             {2, -1},  {2, 1},  {-1, -2}, {1, -2}};
// const int dx[]={0,0,0,1,1,1,-1,-1,-1};
// const int dy[]={-1,0,1,-1,0,1,-1,0,1};
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
// #define MOD         1000000007LL
#define MAX 1005
const ll MOD = 1000000000 + 7;

bool vis[MAX][MAX];
int cost[MAX][MAX];
string maze[MAX];
int w, h, cx, cy, fx, fy;
bool isSafe(int x, int y) {
  if (x < 0 || x >= w || y < 0 || y >= h)
    return false;
  else
    return true;
}
int main() {
  FAST int t;
  t = 1;

  while (t--) {

    cin >> w >> h;
    cin >> cx >> cy;
    cx--, cy--;
    cin >> fx >> fy;
    fx--, fy--;

    for (int i = 0; i < w; i++) {
      cin >> maze[i];
    }

    memset(cost, -1, sizeof cost);

    deque<pair<int, int>> q;
    q.push_front({cx, cy});
    cost[cx][cy] = 0;

    while (!q.empty()) {

      pair<int, int> t = q.front();
      q.pop_front();

      if (vis[t.first][t.second]) {
        continue;
      }
      vis[t.first][t.second] = 1;

      for (int i = 0; i < 4; i++) {
        int nx = t.first + dx[i];
        int ny = t.second + dy[i];

        if (!isSafe(nx, ny))
          continue;
        if (maze[nx][ny] == '#')
          continue;

        if (cost[nx][ny] == -1 || cost[t.first][t.second] < cost[nx][ny]) {
          cost[nx][ny] = cost[t.first][t.second];
          q.push_front({nx, ny});
        }
      }

      for (int i = -2; i < 3; i++) {
        for (int j = -2; j < 3; j++) {
          int nx = t.first + i;
          int ny = t.second + j;

          if (!isSafe(nx, ny))
            continue;
          if (maze[nx][ny] == '#')
            continue;

          if (cost[nx][ny] == -1 ||
              cost[t.first][t.second] + 1 < cost[nx][ny]) {
            cost[nx][ny] = cost[t.first][t.second] + 1;
            q.push_back({nx, ny});
          }
        }
      }
    }

    cout << cost[fx][fy] << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vii;
typedef vector<pll> vll;

#define endl '\\n'
#define PB push_back
#define F first
#define S second
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define sz(x) (int)x.size()

const double PI = acos(-1);
const double eps = 1e-9;
const int inf = 2000000000;
const ll infLL = 9000000000000000000;
#define MOD 1000000007

#define mem(a, b) memset(a, b, sizeof(a))
#define sqr(a) ((a) * (a))

#define optimize()                                                             \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define fraction()                                                             \\
  cout.unsetf(ios::floatfield);                                                \\
  cout.precision(10);                                                          \\
  cout.setf(ios::fixed, ios::floatfield);
#define file()                                                                 \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout);

const int mxN = 1010;

int n, m;
char ch[mxN][mxN];
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};
ll cnt[mxN][mxN];

void bfs(int sx, int sy) {
  queue<pii> q;
  q.push({sx, sy});
  cnt[sx][sy] = 0;
  while (!q.empty()) {
    int ux = q.front().F;
    int uy = q.front().S;
    q.pop();
    for (int i = 0; i < 4; ++i) {
      int nx = ux + dx[i];
      int ny = uy + dy[i];
      if (nx <= 0 || ny <= 0 || nx > m || ny > n)
        continue;
      if (ch[ny][nx] == '.') {
        if (cnt[nx][ny] > cnt[ux][uy]) {
          cnt[nx][ny] = cnt[ux][uy];
          q.push({nx, ny});
        }
      }
    }
    for (int nx = ux - 2; nx <= ux + 2; ++nx) {
      for (int ny = uy - 2; ny <= uy + 2; ++ny) {
        if (nx <= 0 || ny <= 0 || nx > m || ny > n)
          continue;
        if (ch[ny][nx] == '.') {
          if (cnt[nx][ny] > cnt[ux][uy] + 1) {
            cnt[nx][ny] = cnt[ux][uy] + 1;
            q.push({nx, ny});
          }
        }
      }
    }
  }
}

int main() {
  optimize();
  cin >> n >> m;
  int sx, sy, dx, dy;
  cin >> sy >> sx >> dy >> dx;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      cin >> ch[i][j];
    }
  }
  for (int i = 0; i <= n; ++i) {
    for (int j = 0; j <= m; ++j) {
      cnt[j][i] = inf;
    }
  }
  bfs(sx, sy);
  cout << (cnt[dx][dy] == inf ? -1 : cnt[dx][dy]) << endl;
  return 0;
}