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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
#define IOS                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0)

using namespace std;
using namespace __gnu_pbds;
typedef long long LL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    order_set;
const int MAXN = 1234;
char s[MAXN];
int maze[MAXN][MAXN];
int lvl[MAXN][MAXN];
int sx[4] = {-1, 1, 0, 0}, sy[4] = {0, 0, 1, -1};

struct node {
  int x, y, val;
  node(int _x, int _y, int _val) : x(_x), y(_y), val(_val) {}
  bool operator<(const node &a) const { return val < a.val; }
};

bool check(int x, int y, int H, int W) {
  if (x < 0 || y < 0 || x > H || y > W)
    return 0;
  return maze[x][y];
}

int main() {
  int H, W;
  scanf("%d %d", &H, &W);
  int ch, cw, dh, dw;
  scanf("%d %d", &ch, &cw);
  scanf("%d %d", &dh, &dw);
  for (int i = 1; i <= H; i++) {
    scanf("%s", s + 1);
    for (int j = 1; j <= W; j++) {
      lvl[i][j] = -1;
      if (s[j] == '.')
        maze[i][j] = 1;
    }
  }
  deque<node> pq;
  pq.push_front(node(ch, cw, 0));
  while (!pq.empty()) {
    auto cur = pq.front();
    pq.pop_front();
    if (lvl[cur.x][cur.y] != -1 && lvl[cur.x][cur.y] <= cur.val)
      continue;
    lvl[cur.x][cur.y] = cur.val;
    for (int i = 0; i < 4; i++) {
      int xx = cur.x + sx[i], yy = cur.y + sy[i];
      if (check(xx, yy, H, W))
        pq.push_front(node(xx, yy, cur.val));
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int xx = cur.x + i, yy = cur.y + j;
        if (check(xx, yy, H, W))
          pq.push_back(node(xx, yy, cur.val + 1));
      }
    }
  }
  printf("%d\\n", lvl[dh][dw]);
  return 0;
}
/* stuff you should look for
        * int overflow, array bounds
        * special cases (n=1?)
        * do smth instead of nothing and stay organized
        * WRITE STUFF DOWN
by Benq;
*/


Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
#define IOS                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0)

using namespace std;
using namespace __gnu_pbds;
typedef long long LL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    order_set;
const int MAXN = 1234;
char s[MAXN];
int maze[MAXN][MAXN];
int lvl[MAXN][MAXN];
int sx[4] = {-1, 1, 0, 0}, sy[4] = {0, 0, 1, -1};

struct node {
  int x, y, val;
  node(int _x, int _y, int _val) : x(_x), y(_y), val(_val) {}
  bool operator<(const node &a) const { return val < a.val; }
};

bool check(int x, int y, int H, int W) {
  if (x < 0 || y < 0 || x > H || y > W)
    return 0;
  return maze[x][y];
}

int main() {
  int H, W;
  scanf("%d %d", &H, &W);
  int ch, cw, dh, dw;
  scanf("%d %d", &ch, &cw);
  scanf("%d %d", &dh, &dw);
  for (int i = 1; i <= H; i++) {
    scanf("%s", s + 1);
    for (int j = 1; j <= W; j++) {
      lvl[i][j] = -1;
      if (s[j] == '.')
        maze[i][j] = 1;
    }
  }
  priority_queue<node> pq;
  pq.push(node(ch, cw, 0));
  while (!pq.empty()) {
    auto cur = pq.top();
    pq.pop();
    if (lvl[cur.x][cur.y] != -1 && lvl[cur.x][cur.y] <= cur.val)
      continue;
    lvl[cur.x][cur.y] = cur.val;
    for (int i = 0; i < 4; i++) {
      int xx = cur.x + sx[i], yy = cur.y + sy[i];
      if (check(xx, yy, H, W))
        pq.push(node(xx, yy, cur.val));
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int xx = cur.x + i, yy = cur.y + j;
        if (check(xx, yy, H, W))
          pq.push(node(xx, yy, cur.val + 1));
      }
    }
  }
  printf("%d\\n", lvl[dh][dw]);
  return 0;
}
/* stuff you should look for
        * int overflow, array bounds
        * special cases (n=1?)
        * do smth instead of nothing and stay organized
        * WRITE STUFF DOWN
by Benq;
*/