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

#include <algorithm>
#include <bits/stdc++.h>
#include <bitset>
#include <cassert>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <tuple>
#include <vector>
using namespace std;

#define rep(i, n) for (long long i = 0; i < (long long)(n); ++i)
using P = pair<long long, pair<long long, long long>>;

long long H, W;
long long ch, cw;
long long dh, dw;
long long dx[4] = {0, -1, 0, 1};
long long dy[4] = {1, 0, -1, 0};
string s[1000];
long long dist[1000][1000] = {0};
const long long INF = 1000000000;
const long long INF2 = 1000000000000000000;

int main() {
  cin >> H >> W;
  cin >> ch >> cw;
  cin >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  rep(i, H) {
    string temp;
    cin >> temp;
    s[i] = temp;
  }
  priority_queue<P, vector<P>, greater<P>> que;

  rep(i, H) {
    rep(j, W) { dist[i][j] = INF2; }
  }

  que.push({0, {ch, cw}});
  dist[ch][cw] = 0;

  while (que.size()) {
    bool f = true;
    P p = que.top();
    que.pop();
    rep(i, 4) {
      long long nx = p.second.first + dx[i];
      long long ny = p.second.second + dy[i];
      if (0 <= nx && nx < H && 0 <= ny && ny < W && s[nx][ny] == '.' &&
          dist[nx][ny] > dist[p.second.first][p.second.second] + 1) {
        dist[nx][ny] = dist[p.second.first][p.second.second] + 1;
        que.push({dist[nx][ny], {nx, ny}});
      }
    }
    for (long long i = -2; i <= 2; i++) {
      for (long long j = -2; j <= 2; j++) {
        long long nx = p.second.first + i;
        long long ny = p.second.second + j;
        if (0 <= nx && nx < H && 0 <= ny && ny < W && s[nx][ny] == '.' &&
            dist[nx][ny] > dist[p.second.first][p.second.second] + INF) {
          dist[nx][ny] = dist[p.second.first][p.second.second] + INF;
          que.push({dist[nx][ny], {nx, ny}});
        }
      }
    }
  }
  if (dist[dh][dw] == INF2) {
    cout << -1 << endl;
    return 0;
  }
  long long ans = dist[dh][dw] / INF;
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <bitset>
#include <cassert>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <tuple>
#include <vector>
using namespace std;

#define rep(i, n) for (long long i = 0; i < (long long)(n); ++i)
using P = pair<long long, long long>;

long long H, W;
long long ch, cw;
long long dh, dw;
long long dx[4] = {0, -1, 0, 1};
long long dy[4] = {1, 0, -1, 0};
string s[1000];
long long dist[1000][1000] = {0};
const long long INF = 1000000000;
const long long INF2 = 1000000000000000000;

int main() {
  cin >> H >> W;
  cin >> ch >> cw;
  cin >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  rep(i, H) {
    string temp;
    cin >> temp;
    s[i] = temp;
  }
  queue<P> que;

  rep(i, H) {
    rep(j, W) { dist[i][j] = INF2; }
  }

  que.push({ch, cw});
  dist[ch][cw] = 0;

  while (que.size()) {
    bool f = true;
    P p = que.front();
    que.pop();
    rep(i, 4) {
      long long nx = p.first + dx[i];
      long long ny = p.second + dy[i];
      if (0 <= nx && nx < H && 0 <= ny && ny < W && s[nx][ny] == '.' &&
          dist[nx][ny] > dist[p.first][p.second] + 1) {
        que.push({nx, ny});
        dist[nx][ny] = dist[p.first][p.second] + 1;
      }
    }
    for (long long i = -2; i <= 2; i++) {
      for (long long j = -2; j <= 2; j++) {
        long long nx = p.first + i;
        long long ny = p.second + j;
        if (0 <= nx && nx < H && 0 <= ny && ny < W && s[nx][ny] == '.' &&
            dist[nx][ny] > dist[p.first][p.second] + INF) {
          que.push({nx, ny});
          dist[nx][ny] = dist[p.first][p.second] + INF;
        }
      }
    }
  }
  if (dist[dh][dw] == INF2) {
    cout << -1 << endl;
    return 0;
  }
  long long ans = dist[dh][dw] / INF;
  cout << ans << endl;
  return 0;
}