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

#include <algorithm>
#include <bits/stdc++.h>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <set>
#include <sstream>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
vector<vector<char>> grid;
vector<vector<long long int>> dp;
int main(void) {
  long long int h, w;
  cin >> h >> w;
  long long int ch, cw;
  cin >> ch >> cw;
  ch--;
  cw--;
  long long int dh, dw;
  cin >> dh >> dw;
  dh--;
  dw--;
  grid = vector<vector<char>>(h, vector<char>(w));
  dp = vector<vector<long long int>>(
      h, vector<long long int>(w, 99999999999999999));
  for (long long int i = 0; i < h; i++) {
    for (long long int j = 0; j < w; j++) {
      cin >> grid.at(i).at(j);
    }
  }

  dp.at(ch).at(cw) = 0;
  queue<pair<long long int, long long int>> que;
  que.push(make_pair(ch, cw));
  queue<pair<long long int, long long int>> jump;
  while (!que.empty() || !jump.empty()) {
    if (que.empty()) {
      que.push(jump.front());
      jump.pop();
    }
    pair<long long int, long long int> np = que.front();
    que.pop();
    long long int nh = np.first;
    long long int nw = np.second;
    for (long long int i = nh - 2; i <= nh + 2; i++) {
      for (long long int j = nw - 2; j <= nw + 2; j++) {
        if (i < 0 || j < 0) {
          continue;
        }
        if (i >= h || j >= w) {
          continue;
        }
        if (grid.at(i).at(j) == '#') {
          continue;
        }
        if ((i == nh && j == nw - 1) || (i == nh && j == nw + 1) ||
            (i == nh - 1 && j == nw) || (i == nh + 1 && j == nw)) {
          if (dp.at(nh).at(nw) < dp.at(i).at(j)) {
            dp.at(i).at(j) = dp.at(nh).at(nw);
            que.push(make_pair(i, j));
          }
        } else {
          if (dp.at(nh).at(nw) + 1 < dp.at(i).at(j)) {
            dp.at(i).at(j) = dp.at(nh).at(nw) + 1;
            jump.push(make_pair(i, j));
          }
        }
      }
    }
  }
  if (dp.at(dh).at(dw) == 99999999999999999) {
    cout << -1 << endl;
  } else {
    cout << dp.at(dh).at(dw);
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <set>
#include <sstream>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
vector<vector<char>> grid;
vector<vector<long long int>> dp;
int main(void) {
  long long int h, w;
  cin >> h >> w;
  long long int ch, cw;
  cin >> ch >> cw;
  ch--;
  cw--;
  long long int dh, dw;
  cin >> dh >> dw;
  dh--;
  dw--;
  grid = vector<vector<char>>(h, vector<char>(w));
  dp = vector<vector<long long int>>(
      h, vector<long long int>(w, 99999999999999999));
  for (long long int i = 0; i < h; i++) {
    for (long long int j = 0; j < w; j++) {
      cin >> grid.at(i).at(j);
    }
  }

  dp.at(ch).at(cw) = 0;
  queue<pair<long long int, long long int>> que;
  queue<pair<long long int, long long int>> que2;
  que.push(make_pair(ch, cw));
  while (!que.empty()) {
    pair<long long int, long long int> np = que.front();
    que2.push(np);
    que.pop();
    long long int nh = np.first;
    long long int nw = np.second;
    if (nw != 0 && dp.at(nh).at(nw - 1) == 99999999999999999 &&
        grid.at(nh).at(nw - 1) == '.') {
      que.push(make_pair(nh, nw - 1));
      dp.at(nh).at(nw - 1) = 0;
    }
    if (nw != w - 1 && dp.at(nh).at(nw + 1) == 99999999999999999 &&
        grid.at(nh).at(nw + 1) == '.') {
      que.push(make_pair(nh, nw + 1));
      dp.at(nh).at(nw + 1) = 0;
    }
    if (nh != 0 && dp.at(nh - 1).at(nw) == 99999999999999999 &&
        grid.at(nh - 1).at(nw) == '.') {
      que.push(make_pair(nh - 1, nw));
      dp.at(nh - 1).at(nw) = 0;
    }
    if (nh != h - 1 && dp.at(nh + 1).at(nw) == 99999999999999999 &&
        grid.at(nh + 1).at(nw) == '.') {
      que.push(make_pair(nh + 1, nw));
      dp.at(nh + 1).at(nw) = 0;
    }
  }
  if (dp.at(dh).at(dw) == 0) {
    cout << 0 << endl;
    return 0;
  }
  que = que2;
  while (!que.empty()) {
    pair<long long int, long long int> np = que.front();
    que.pop();
    long long int nh = np.first;
    long long int nw = np.second;
    for (long long int i = nh - 2; i <= nh + 2; i++) {
      for (long long int j = nw - 2; j <= nw + 2; j++) {
        if (i < 0 || j < 0) {
          continue;
        }
        if (i >= h || j >= w) {
          continue;
        }
        if (grid.at(i).at(j) == '#') {
          continue;
        }
        if ((i == nh && j == nw - 1) || (i == nh && j == nw + 1) ||
            (i == nh - 1 && j == nw) || (i == nh + 1 && j == nw)) {
          if (dp.at(nh).at(nw) < dp.at(i).at(j)) {
            dp.at(i).at(j) = dp.at(nh).at(nw);
            que.push(make_pair(i, j));
          }
        } else {
          if (dp.at(nh).at(nw) + 1 < dp.at(i).at(j)) {
            dp.at(i).at(j) = dp.at(nh).at(nw) + 1;
            que.push(make_pair(i, j));
          }
        }
      }
    }
  }
  if (dp.at(dh).at(dw) == 99999999999999999) {
    cout << -1 << endl;
  } else {
    cout << dp.at(dh).at(dw);
  }
}