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

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

int main() {
  long h, w;
  cin >> h >> w;
  long ch, cw;
  cin >> ch >> cw;
  long dh, dw;
  cin >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  string s;
  vector<string> grid;
  for (long i = 0; i < h; i++) {
    cin >> s;
    grid.push_back(s);
  }
  vector<long> drow(w, 1000000);
  vector<vector<long>> dists(h, drow);
  priority_queue<pair<long, pair<long, long>>> moves;
  moves.push(make_pair(0, make_pair(ch, cw)));
  while (moves.size() > 0) {
    pair<long, pair<long, long>> curr = moves.top();
    moves.pop();
    long x = curr.second.first;
    long y = curr.second.second;
    long c = curr.first;
    if (x < 0 || x >= h || y < 0 || y >= w)
      continue;
    if (-c >= dists[x][y])
      continue;
    if (grid[x][y] == '#')
      continue;
    dists[x][y] = -c;
    if (x - 1 >= 0 && -c < dists[x - 1][y] && grid[x - 1][y] != '#')
      moves.push(make_pair(c, make_pair(x - 1, y)));
    if (y - 1 >= 0 && -c < dists[x][y - 1] && grid[x][y - 1] != '#')
      moves.push(make_pair(c, make_pair(x, y - 1)));
    if (x + 1 < h && -c < dists[x + 1][y] && grid[x + 1][y] != '#')
      moves.push(make_pair(c, make_pair(x + 1, y)));
    if (y + 1 < w && -c < dists[x][y + 1] && grid[x][y + 1] != '#')
      moves.push(make_pair(c, make_pair(x, y + 1)));
    for (long i = -2; i <= 2; i++)
      for (long j = -2; j <= 2; j++) {
        if (x + i >= 0 && x + i < h && y + j >= 0 && y + j < w &&
            -c < dists[x + i][y + j] && grid[x + i][y + j] != '#')
          moves.push(make_pair(c - 1, make_pair(x + i, y + j)));
      }
  }
  if (dists[dh][dw] == 1000000)
    cout << -1;
  else
    cout << dists[dh][dw];
  return 0;
}


Error: TLE

Fixed submission:

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

int main() {
  long h, w;
  cin >> h >> w;
  long ch, cw;
  cin >> ch >> cw;
  long dh, dw;
  cin >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  string s;
  vector<string> grid;
  for (long i = 0; i < h; i++) {
    cin >> s;
    grid.push_back(s);
  }
  vector<long> drow(w, 1000000);
  vector<vector<long>> dists(h, drow);
  queue<pair<long, pair<long, long>>> moves;
  moves.push(make_pair(0, make_pair(ch, cw)));
  while (moves.size() > 0) {
    pair<long, pair<long, long>> curr = moves.front();
    moves.pop();
    long x = curr.second.first;
    long y = curr.second.second;
    long c = curr.first;
    if (x < 0 || x >= h || y < 0 || y >= w)
      continue;
    if (c >= dists[x][y])
      continue;
    if (grid[x][y] == '#')
      continue;
    dists[x][y] = c;
    if (x - 1 >= 0 && c < dists[x - 1][y] && grid[x - 1][y] != '#')
      moves.push(make_pair(c, make_pair(x - 1, y)));
    if (y - 1 >= 0 && c < dists[x][y - 1] && grid[x][y - 1] != '#')
      moves.push(make_pair(c, make_pair(x, y - 1)));
    if (x + 1 < h && c < dists[x + 1][y] && grid[x + 1][y] != '#')
      moves.push(make_pair(c, make_pair(x + 1, y)));
    if (y + 1 < w && c < dists[x][y + 1] && grid[x][y + 1] != '#')
      moves.push(make_pair(c, make_pair(x, y + 1)));
    for (long i = -2; i <= 2; i++)
      for (long j = -2; j <= 2; j++) {
        if (x + i >= 0 && x + i < h && y + j >= 0 && y + j < w &&
            c < dists[x + i][y + j] && grid[x + i][y + j] != '#')
          moves.push(make_pair(c + 1, make_pair(x + i, y + j)));
      }
  }
  if (dists[dh][dw] == 1000000)
    cout << -1;
  else
    cout << dists[dh][dw];
  return 0;
}