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

#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define sz(x) ((int)(x).size())
#define endl "\\n"
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(nullptr);
#define all(x) x.begin(), x.end()
#define y1 omg
#define INF 1000000010
#define FOR(x, n) for (int x = 0; x < n; x++)
#define ini(x, n) memset(x, n, sizeof(x));
#define MOD ((int)1e9 + 7)
#define atoi(x) ((x) - '0')
// #define int ll
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;

int dx[] = {1, -1, 0, 0, 1, 1, -1, -1};
int dy[] = {0, 0, 1, -1, 1, -1, 1, -1};

const int MAX = 1e3;
int n, m, x, y, x1, y1, x2, y2;
vvi dp(MAX, vi(MAX, INF));
string v[MAX];
priority_queue<iii, vector<iii>, greater<iii>> Q;

int main() {
  fastio;
  cin >> n >> m >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;
  FOR(i, n) cin >> v[i];
  dp[x1][y1] = 0;
  Q.push(mp(0, mp(x1, y1)));
  while (!Q.empty()) {
    auto t = Q.top();
    Q.pop();
    x = t.se.fi, y = t.se.se;
    FOR(i, 4) {
      int nx = x + dx[i];
      int ny = y + dy[i];
      if (0 <= nx && nx < n && 0 <= ny && ny < m && v[nx][ny] == '.' &&
          dp[nx][ny] > dp[x][y]) {
        dp[nx][ny] = dp[x][y];
        Q.push(mp(dp[x][y], mp(nx, ny)));
      }
    }
    for (int i = -2; i <= 2; i++)
      for (int j = -2; j <= 2; j++) {
        int nx = x + i;
        int ny = y + j;
        if (0 <= nx && nx < n && 0 <= ny && ny < m && v[nx][ny] == '.' &&
            dp[nx][ny] > dp[x][y] + 1) {
          dp[nx][ny] = dp[x][y] + 1;
          Q.push(mp(dp[nx][ny], mp(nx, ny)));
        }
      }
  }
  cout << (dp[x2][y2] == INF ? -1 : dp[x2][y2]);
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define sz(x) ((int)(x).size())
#define endl "\\n"
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(nullptr);
#define all(x) x.begin(), x.end()
#define y1 omg
#define INF 1000000010
#define FOR(x, n) for (int x = 0; x < n; x++)
#define ini(x, n) memset(x, n, sizeof(x));
#define MOD ((int)1e9 + 7)
#define atoi(x) ((x) - '0')
// #define int ll
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;

int dx[] = {1, -1, 0, 0, 1, 1, -1, -1};
int dy[] = {0, 0, 1, -1, 1, -1, 1, -1};

const int MAX = 1e3;
int n, m, x, y, x1, y1, x2, y2;
vvi dp(MAX, vi(MAX, INF));
string v[MAX];
queue<ii> Q;
ii t;

int main() {
  fastio;
  cin >> n >> m >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;
  FOR(i, n) cin >> v[i];
  dp[x1][y1] = 0;
  Q.push(mp(x1, y1));
  while (!Q.empty()) {
    t = Q.front();
    Q.pop();
    x = t.fi, y = t.se;
    FOR(i, 4) {
      int nx = x + dx[i];
      int ny = y + dy[i];
      if (0 <= nx && nx < n && 0 <= ny && ny < m && v[nx][ny] == '.' &&
          dp[nx][ny] > dp[x][y]) {
        dp[nx][ny] = dp[x][y];
        Q.push(mp(nx, ny));
      }
    }
    for (int i = -2; i <= 2; i++)
      for (int j = -2; j <= 2; j++) {
        int nx = x + i;
        int ny = y + j;
        if (0 <= nx && nx < n && 0 <= ny && ny < m && v[nx][ny] == '.' &&
            dp[nx][ny] > dp[x][y] + 1) {
          dp[nx][ny] = dp[x][y] + 1;
          Q.push(mp(nx, ny));
        }
      }
  }
  cout << (dp[x2][y2] == INF ? -1 : dp[x2][y2]);
}