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

#include <bits/stdc++.h>
using namespace std;
#define AC 0
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n, m, cx, cy, dx, dy,
      go1[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}},
      go2[20][2] = {{-2, -2}, {-2, -1}, {-2, 0}, {-2, 1}, {-2, 2},
                    {-1, -2}, {-1, -1}, {-1, 1}, {-1, 2}, {0, -2},
                    {0, 2},   {2, -2},  {2, -1}, {2, 0},  {2, 1},
                    {2, 2},   {1, -2},  {1, -1}, {1, 1},  {1, 2}};
  cin >> n >> m >> cx >> cy >> dx >> dy;
  cx--, cy--, dx--, dy--;
  vector<vector<bool>> g(n, vector<bool>(m));
  vector<vector<int>> dis(n, vector<int>(m, 1e9));
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      char c;
      cin >> c;
      g[i][j] = c == '#';
    }
  priority_queue<pair<int, pair<int, int>>> q;
  q.push(pair<int, pair<int, int>>(0, pair<int, int>(cx, cy)));
  dis[cx][cy] = 0;
  while (q.size()) {
    pair<int, pair<int, int>> h = q.top();
    q.pop();
    int hx = h.second.first, hy = h.second.second;
    for (int j = 0; j < 4; j++) {
      int xx = hx + go1[j][0], yy = hy + go1[j][1];
      if (xx >= 0 && xx < n && yy >= 0 && yy < m && !g[xx][yy] &&
          dis[xx][yy] > dis[hx][hy]) {
        dis[xx][yy] = dis[hx][hy];
        q.push(pair<int, pair<int, int>>(-dis[xx][yy], pair<int, int>(xx, yy)));
      }
    }
    for (int j = 0; j < 20; j++) {
      int xx = hx + go2[j][0], yy = hy + go2[j][1];
      if (xx >= 0 && xx < n && yy >= 0 && yy < m && !g[xx][yy] &&
          dis[xx][yy] > dis[hx][hy] + 1) {
        dis[xx][yy] = dis[hx][hy] + 1;
        q.push(pair<int, pair<int, int>>(-dis[xx][yy], pair<int, int>(xx, yy)));
      }
    }
  }
  if (dis[dx][dy] == 1e9)
    cout << "-1";
  else
    cout << dis[dx][dy];
  return AC;
}

Error: TLE

Fixed submission:

#pragma GCC optimize("-Ofast")
#include <bits/stdc++.h>
using namespace std;
#define AC 0
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n, m, cx, cy, dx, dy,
      go1[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}},
      go2[20][2] = {{-2, -2}, {-2, -1}, {-2, 0}, {-2, 1}, {-2, 2},
                    {-1, -2}, {-1, -1}, {-1, 1}, {-1, 2}, {0, -2},
                    {0, 2},   {2, -2},  {2, -1}, {2, 0},  {2, 1},
                    {2, 2},   {1, -2},  {1, -1}, {1, 1},  {1, 2}};
  cin >> n >> m >> cx >> cy >> dx >> dy;
  cx--, cy--, dx--, dy--;
  vector<vector<bool>> g(n, vector<bool>(m)), inq(n, vector<bool>(m));
  vector<vector<int>> dis(n, vector<int>(m, 1e9));
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      char c;
      cin >> c;
      g[i][j] = c == '#';
    }
  queue<pair<int, int>> q;
  q.push(pair<int, int>(cx, cy));
  inq[cx][cy] = 1;
  dis[cx][cy] = 0;
  while (q.size()) {
    int hx = q.front().first, hy = q.front().second;
    for (int j = 0; j < 4; j++) {
      int xx = hx + go1[j][0], yy = hy + go1[j][1];
      if (xx >= 0 && xx < n && yy >= 0 && yy < m && !g[xx][yy] &&
          dis[xx][yy] > dis[hx][hy]) {
        dis[xx][yy] = dis[hx][hy];
        if (!inq[xx][yy]) {
          q.push(pair<int, int>(xx, yy));
          inq[xx][yy] = 1;
        }
      }
    }
    for (int j = 0; j < 20; j++) {
      int xx = hx + go2[j][0], yy = hy + go2[j][1];
      if (xx >= 0 && xx < n && yy >= 0 && yy < m && !g[xx][yy] &&
          dis[xx][yy] > dis[hx][hy] + 1) {
        dis[xx][yy] = dis[hx][hy] + 1;
        if (!inq[xx][yy]) {
          q.push(pair<int, int>(xx, yy));
          inq[xx][yy] = 1;
        }
      }
    }
    q.pop();
    inq[hx][hy] = 0;
  }
  if (dis[dx][dy] == 1e9)
    cout << "-1";
  else
    cout << dis[dx][dy];
  return AC;
}