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

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define fi first
#define se second
// #pragma GCC optimize "trapv"
char a[1000 + 10][1000 + 10];
int vi[1000 + 10][1000 + 10];
int ch, cw, dh, dw;
int h, w;
int mrk[1004][1004];
void bfs(int x, int y) {
  priority_queue<pair<ll, pair<ll, ll>>, vector<pair<ll, pair<ll, ll>>>,
                 greater<pair<ll, pair<ll, ll>>>>
      q;
  q.push({0, {x, y}});
  vi[x][y] = 0;
  while (!q.empty()) {
    int x1 = q.top().se.fi;
    int y1 = q.top().se.se;

    q.pop();
    if (mrk[x1][y1] == 1)
      continue;
    mrk[x1][y1];

    for (int i = -1; i <= 1; i++) {
      for (int j = -1; j <= 1; j++) {
        if (abs(i) == abs(j))
          continue;
        int x2 = x1 + i;
        int y2 = y1 + j;
        if (x2 < 0 || x2 >= h || y2 < 0 || y2 >= w)
          continue;

        if (a[x2][y2] == '#')
          continue;

        if ((vi[x2][y2] > vi[x1][y1]) || vi[x2][y2] == -1) {
          vi[x2][y2] = vi[x1][y1];
          q.push({vi[x2][y2], {x2, y2}});
        }
      }
    }

    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int x2 = x1 + i;
        int y2 = y1 + j;
        if (x2 < 0 || x2 >= h || y2 < 0 || y2 >= w)
          continue;

        if (a[x2][y2] == '#')
          continue;

        if ((vi[x2][y2] > vi[x1][y1] + 1) || vi[x2][y2] == -1) {
          vi[x2][y2] = vi[x1][y1] + 1;
          q.push({vi[x2][y2], {x2, y2}});
        }
      }
    }
  }
}

int main() {
  // ios_base::sync_with_stdio(false);
  // cin.tie(NULL);
  cin >> h >> w;
  cin >> ch >> cw;
  cin >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  // memset(vi,-1,sizeof(vi));
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      vi[i][j] = -1;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++) {
      cin >> a[i][j];
    }

  bfs(ch, cw);
  if (vi[dh][dw] != -1)
    cout << vi[dh][dw] << "\\n";
  else
    cout << "-1\\n";
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define fi first
#define se second
// #pragma GCC optimize "trapv"
char a[1000 + 10][1000 + 10];
int vi[1000 + 10][1000 + 10];
int ch, cw, dh, dw;
int h, w;
bool mrk[1004][1004];
void bfs(int x, int y) {
  queue<pair<int, int>> q;
  q.push({x, y});
  vi[x][y] = 0;
  while (!q.empty()) {
    int x1 = q.front().fi;
    int y1 = q.front().se;

    q.pop();

    for (int i = -1; i <= 1; i++) {
      for (int j = -1; j <= 1; j++) {
        if (abs(i) == abs(j))
          continue;
        int x2 = x1 + i;
        int y2 = y1 + j;
        if (x2 < 0 || x2 >= h || y2 < 0 || y2 >= w)
          continue;

        if (a[x2][y2] == '#')
          continue;

        if ((vi[x2][y2] > vi[x1][y1]) || vi[x2][y2] == -1) {
          q.push({x2, y2});
          vi[x2][y2] = vi[x1][y1];
        }
      }
    }

    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int x2 = x1 + i;
        int y2 = y1 + j;
        if (x2 < 0 || x2 >= h || y2 < 0 || y2 >= w)
          continue;

        if (a[x2][y2] == '#')
          continue;

        if ((vi[x2][y2] > vi[x1][y1] + 1) || vi[x2][y2] == -1) {
          q.push({x2, y2});
          vi[x2][y2] = vi[x1][y1] + 1;
        }
      }
    }
  }
}

int main() {
  // ios_base::sync_with_stdio(false);
  // cin.tie(NULL);
  cin >> h >> w;
  cin >> ch >> cw;
  cin >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  // memset(vi,-1,sizeof(vi));
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      vi[i][j] = -1;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++) {
      cin >> a[i][j];
    }

  bfs(ch, cw);
  if (vi[dh][dw] != -1)
    cout << vi[dh][dw] << "\\n";
  else
    cout << "-1\\n";
}