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

#include <bits/stdc++.h>
using namespace std;
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int h, w, a, b, x, y;
  cin >> h >> w >> a >> b >> x >> y;
  char grid[h + 3][w + 3];
  int vis[h + 3][w + 3];
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      cin >> grid[i][j];
      vis[i][j] = 1e8;
    }
  }
  priority_queue<pair<int, pair<int, int>>> q;
  vis[a][b] = 0;
  q.push({0, {a, b}});
  while (!q.empty()) {
    int c = q.top().second.first, d = q.top().second.second;
    int cost = -q.top().first;
    q.pop();
    if (cost > vis[c][d]) {
      continue;
    }
    if (c > 1 && grid[c - 1][d] == '.') {
      if (vis[c - 1][d] > cost) {
        vis[c - 1][d] = cost;
        q.push({-cost, {c - 1, d}});
      }
    }
    if (c < h && grid[c + 1][d] == '.') {
      if (vis[c + 1][d] > cost) {
        vis[c + 1][d] = cost;
        q.push({-cost, {c + 1, d}});
      }
    }
    if (d > 1 && grid[c][d - 1] == '.') {
      if (vis[c][d - 1] > cost) {
        vis[c][d - 1] = cost;
        q.push({-cost, {c, d - 1}});
      }
    }
    if (d < w && grid[c][d + 1] == '.') {
      if (vis[c][d + 1] > cost) {
        vis[c][d + 1] = cost;
        q.push({-cost, {c, d + 1}});
      }
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        if (c + i >= 1 && c + i <= h && d + j >= 1 && d + j <= w &&
            grid[c + i][d + j] == '.') {
          if (vis[c + i][d + j] > cost + 1) {
            vis[c + i][d + j] = cost + 1;
            q.push({-(cost + 1), {c + i, d + j}});
          }
        }
      }
    }
  }
  if (vis[x][y] == 1e8) {
    cout << "-1";
  } else {
    cout << vis[x][y];
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int h, w, a, b, x, y;
  cin >> h >> w >> a >> b >> x >> y;
  char grid[h + 3][w + 3];
  int vis[h + 3][w + 3];
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      cin >> grid[i][j];
      vis[i][j] = 1e8;
    }
  }
  queue<pair<int, int>> q;
  vis[a][b] = 0;
  q.push({a, b});
  while (!q.empty()) {
    int c = q.front().first, d = q.front().second;
    q.pop();
    if (c > 1 && grid[c - 1][d] == '.') {
      if (vis[c - 1][d] > vis[c][d]) {
        vis[c - 1][d] = vis[c][d];
        q.push({c - 1, d});
      }
    }
    if (c < h && grid[c + 1][d] == '.') {
      if (vis[c + 1][d] > vis[c][d]) {
        vis[c + 1][d] = vis[c][d];
        q.push({c + 1, d});
      }
    }
    if (d > 1 && grid[c][d - 1] == '.') {
      if (vis[c][d - 1] > vis[c][d]) {
        vis[c][d - 1] = vis[c][d];
        q.push({c, d - 1});
      }
    }
    if (d < w && grid[c][d + 1] == '.') {
      if (vis[c][d + 1] > vis[c][d]) {
        vis[c][d + 1] = vis[c][d];
        q.push({c, d + 1});
      }
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        if (c + i >= 1 && c + i <= h && d + j >= 1 && d + j <= w &&
            grid[c + i][d + j] == '.') {
          if (vis[c + i][d + j] > vis[c][d] + 1) {
            vis[c + i][d + j] = vis[c][d] + 1;
            q.push({c + i, d + j});
          }
        }
      }
    }
  }
  if (vis[x][y] == 1e8) {
    cout << "-1";
  } else {
    cout << vis[x][y];
  }
  return 0;
}