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

#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define fw(p) for (int w = 0; w < (p); w++)
#define fx(p) for (int x = 0; x < (p); x++)
#define fy(p) for (int y = 0; y < (p); y++)
#define fz(p) for (int z = 0; z < (p); z++)
#define fyg(p, g) for (int y = (g); y < (p); y++)
#define fzg(p, g) for (int z = (g); z < (p); z++)
#define ce(d) cout << d << endl;
#define vecp(p)                                                                \\
  int aa;                                                                      \\
  cin >> aa;                                                                   \\
  (p).push_back(aa);
#define vecpl(p)                                                               \\
  long long aa;                                                                \\
  cin >> aa;                                                                   \\
  (p).push_back(aa);
#define vecps(p)                                                               \\
  string aa;                                                                   \\
  cin >> aa;                                                                   \\
  (p).push_back(aa);
#define vecp2(p)                                                               \\
  cin >> aa;                                                                   \\
  (p).push_back(aa);
#define vecpl2(p)                                                              \\
  long long a b;                                                               \\
  cin >> ab;                                                                   \\
  (p).push_back(ab);
#define vecps2(p)                                                              \\
  string ab;                                                                   \\
  cin >> ab;                                                                   \\
  (p).push_back(ab);
#define sorts(c) sort((c).begin(), (c).end());
#define reverses(c) reverse((c).begin(), (c).end());
#define vec(b) vector<int>(b);
#define vecl(b) vector<long long>(b);
#define vecs(b) vector<string>(b);
#define pb(b, a) (b).push_back((a));
#define doublece(a, b) cout << (a) << ' ' << (b) << endl;
#define pairs(s) vector<pair<int, int>>(s);
#define pairsp(s)                                                              \\
  int aa, bb;                                                                  \\
  cin >> aa >> bb;                                                             \\
  (s).push_back(make_pair(aa, bb));
#define MOD 1000000007
#define cey ce("Yes")
#define cen ce("No")
#define ceY ce("YES")
#define ceN ce("NO")
int cost[1100][1100];
int main() {
  int H, W;
  cin >> H >> W;
  int sh, sw, gh, gw;
  cin >> sh >> sw >> gh >> gw;
  sh--;
  sw--;
  gh--;
  gw--;
  char field[1100][1100];
  fx(H) {
    string s;
    cin >> s;
    fy(W) { field[x][y] = s[y]; }
  }
  fx(H) {
    fy(W) { cost[x][y] = INT_MAX; }
  }
  cost[sh][sw] = 0;
  priority_queue<pair<int, pair<int, int>>> Q;
  Q.push(make_pair(0, make_pair(sh, sw)));
  while (Q.size()) {
    auto q = Q.top();
    Q.pop();
    int x = q.second.first, y = q.second.second;
    if (cost[x][y] < -q.first) {
      continue;
    }

    if (x + 1 < H && field[x + 1][y] == '.' && cost[x + 1][y] > cost[x][y]) {
      cost[x + 1][y] = cost[x][y];
      Q.push(make_pair(-cost[x + 1][y], make_pair(x + 1, y)));
    }
    if (x - 1 >= 0 && field[x - 1][y] == '.' && cost[x - 1][y] > cost[x][y]) {
      cost[x - 1][y] = cost[x][y];
      Q.push(make_pair(-cost[x - 1][y], make_pair(x - 1, y)));
    }
    if (y + 1 < W && field[x][y + 1] == '.' && cost[x][y + 1] > cost[x][y]) {
      cost[x][y + 1] = cost[x][y];
      Q.push(make_pair(-cost[x][y + 1], make_pair(x, y + 1)));
    }
    if (y - 1 >= 0 && field[x][y - 1] == '.' && cost[x][y - 1] > cost[x][y]) {
      cost[x][y - 1] = cost[x][y];
      Q.push(make_pair(-cost[x][y - 1], make_pair(x, y - 1)));
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        if (x + i >= 0 && x + i < H && y + j >= 0 && y + j < W &&
            field[x + i][y + j] == '.') {
          if (cost[x + i][y + j] > cost[x][y] + 1) {
            cost[x + i][y + j] = cost[x][y] + 1;
            Q.push(make_pair(-cost[x + i][y + j], make_pair(x + i, y + j)));
          }
        }
      }
    }
  }

  if (cost[gh][gw] == INT_MAX) {
    ce(-1)
  } else {
    ce(cost[gh][gw])
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define fw(p) for (int w = 0; w < (p); w++)
#define fx(p) for (int x = 0; x < (p); x++)
#define fy(p) for (int y = 0; y < (p); y++)
#define fz(p) for (int z = 0; z < (p); z++)
#define fyg(p, g) for (int y = (g); y < (p); y++)
#define fzg(p, g) for (int z = (g); z < (p); z++)
#define ce(d) cout << d << endl;
#define vecp(p)                                                                \\
  int aa;                                                                      \\
  cin >> aa;                                                                   \\
  (p).push_back(aa);
#define vecpl(p)                                                               \\
  long long aa;                                                                \\
  cin >> aa;                                                                   \\
  (p).push_back(aa);
#define vecps(p)                                                               \\
  string aa;                                                                   \\
  cin >> aa;                                                                   \\
  (p).push_back(aa);
#define vecp2(p)                                                               \\
  cin >> aa;                                                                   \\
  (p).push_back(aa);
#define vecpl2(p)                                                              \\
  long long a b;                                                               \\
  cin >> ab;                                                                   \\
  (p).push_back(ab);
#define vecps2(p)                                                              \\
  string ab;                                                                   \\
  cin >> ab;                                                                   \\
  (p).push_back(ab);
#define sorts(c) sort((c).begin(), (c).end());
#define reverses(c) reverse((c).begin(), (c).end());
#define vec(b) vector<int>(b);
#define vecl(b) vector<long long>(b);
#define vecs(b) vector<string>(b);
#define pb(b, a) (b).push_back((a));
#define doublece(a, b) cout << (a) << ' ' << (b) << endl;
#define pairs(s) vector<pair<int, int>>(s);
#define pairsp(s)                                                              \\
  int aa, bb;                                                                  \\
  cin >> aa >> bb;                                                             \\
  (s).push_back(make_pair(aa, bb));
#define MOD 1000000007
#define cey ce("Yes")
#define cen ce("No")
#define ceY ce("YES")
#define ceN ce("NO")
int cost[1100][1100];
int main() {
  int H, W;
  cin >> H >> W;
  int sh, sw, gh, gw;
  cin >> sh >> sw >> gh >> gw;
  sh--;
  sw--;
  gh--;
  gw--;
  char field[1100][1100];
  fx(H) {
    string s;
    cin >> s;
    fy(W) { field[x][y] = s[y]; }
  }
  fx(H) {
    fy(W) { cost[x][y] = INT_MAX; }
  }
  cost[sh][sw] = 0;
  queue<pair<int, pair<int, int>>> Q;
  Q.push(make_pair(0, make_pair(sh, sw)));
  while (Q.size()) {
    auto q = Q.front();
    Q.pop();
    int x = q.second.first, y = q.second.second;
    if (cost[x][y] < q.first) {
      continue;
    }

    if (x + 1 < H && field[x + 1][y] == '.' && cost[x + 1][y] > cost[x][y]) {
      cost[x + 1][y] = cost[x][y];
      Q.push(make_pair(cost[x + 1][y], make_pair(x + 1, y)));
    }
    if (x - 1 >= 0 && field[x - 1][y] == '.' && cost[x - 1][y] > cost[x][y]) {
      cost[x - 1][y] = cost[x][y];
      Q.push(make_pair(cost[x - 1][y], make_pair(x - 1, y)));
    }
    if (y + 1 < W && field[x][y + 1] == '.' && cost[x][y + 1] > cost[x][y]) {
      cost[x][y + 1] = cost[x][y];
      Q.push(make_pair(cost[x][y + 1], make_pair(x, y + 1)));
    }
    if (y - 1 >= 0 && field[x][y - 1] == '.' && cost[x][y - 1] > cost[x][y]) {
      cost[x][y - 1] = cost[x][y];
      Q.push(make_pair(cost[x][y - 1], make_pair(x, y - 1)));
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        if (x + i >= 0 && x + i < H && y + j >= 0 && y + j < W &&
            field[x + i][y + j] == '.') {
          if (cost[x + i][y + j] > cost[x][y] + 1) {
            cost[x + i][y + j] = cost[x][y] + 1;
            Q.push(make_pair(cost[x + i][y + j], make_pair(x + i, y + j)));
          }
        }
      }
    }
  }

  if (cost[gh][gw] == INT_MAX) {
    ce(-1)
  } else {
    ce(cost[gh][gw])
  }

  return 0;
}