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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
constexpr int Inf = 2000000010;
constexpr ll INF = 2000000000000000000;
constexpr ll MOD = 1000000007;
const double PI = 3.1415926535897;
typedef pair<int, int> P;
typedef pair<int, P> PP;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

ll mod(ll val, ll M) {
  ll res = val % M;
  if (res < 0) {
    res += M;
  }
  return res;
}

// N^P mod M（ただしM == -1の時はmodを取らない）
template <typename T> T RS(T N, T P, T M) {
  if (P == 0) {
    return 1;
  }
  if (P < 0) {
    return 0;
  }
  if (P % 2 == 0) {
    ll t = RS(N, P / 2, M);
    if (M == -1)
      return t * t;
    return t * t % M;
  }
  if (M == -1) {
    return N * RS(N, P - 1, M);
  }
  return N * RS(N, P - 1, M) % M;
}

int H, W;
int sy, sx, gy, gx;
char maze[1010][1010];
int dist[1010][1010];
vector<int> dy = {-1, 1, 0, 0};
vector<int> dx = {0, 0, -1, 1};

void solve() {
  for (int i = 0; i < 1010; i++) {
    for (int j = 0; j < 1010; j++) {
      dist[i][j] = Inf;
    }
  }
  dist[sy][sx] = 0;
  deque<P> que;
  que.push_back(P(sy, sx));
  while (!que.empty()) {
    P p = que.front();
    que.pop_front();
    for (int i = 0; i < 4; i++) {
      int ny = p.first + dy.at(i);
      int nx = p.second + dx.at(i);
      if (0 <= ny && ny < H && 0 <= nx && nx < W && maze[ny][nx] == '.') {
        if (chmin(dist[ny][nx], dist[p.first][p.second])) {
          que.push_front(P(ny, nx));
        }
      }
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int ny = p.first + i;
        int nx = p.second + j;
        if (0 <= ny && ny < H && 0 <= nx && nx < W && maze[ny][nx] == '.') {
          if (chmin(dist[ny][nx], dist[p.first][p.second] + 1)) {
            que.push_back(P(ny, nx));
          }
        }
      }
    }
  }
}

int main() {
  cin >> H >> W;
  cin >> sy >> sx >> gy >> gx;
  sy--;
  sx--;
  gy--;
  gx--;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> maze[i][j];
    }
  }
  solve();
  if (dist[gy][gx] == Inf) {
    cout << -1 << endl;
  } else {
    cout << dist[gy][gx] << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
constexpr int Inf = 2000000010;
constexpr ll INF = 2000000000000000000;
constexpr ll MOD = 1000000007;
const double PI = 3.1415926535897;
typedef pair<ll, ll> P;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

ll mod(ll val, ll M) {
  ll res = val % M;
  if (res < 0) {
    res += M;
  }
  return res;
}

// N^P mod M（ただしM == -1の時はmodを取らない）
template <typename T> T RS(T N, T P, T M) {
  if (P == 0) {
    return 1;
  }
  if (P < 0) {
    return 0;
  }
  if (P % 2 == 0) {
    ll t = RS(N, P / 2, M);
    if (M == -1)
      return t * t;
    return t * t % M;
  }
  if (M == -1) {
    return N * RS(N, P - 1, M);
  }
  return N * RS(N, P - 1, M) % M;
}

int H, W;
int sy, sx;
int gy, gx;
char maze[1010][1010];
int ret[1010][1010];
vector<int> dy = {-1, 1, 0, 0};
vector<int> dx = {0, 0, -1, 1};

void solve() {
  for (int i = 0; i < 1010; i++) {
    for (int j = 0; j < 1010; j++) {
      ret[i][j] = Inf;
    }
  }
  ret[sy][sx] = 0;
  queue<pair<int, P>> que;
  que.push(make_pair(0, P(sy, sx)));
  while (!que.empty()) {
    pair<int, P> p = que.front();
    que.pop();
    if (ret[p.second.first][p.second.second] < p.first) {
      continue;
    }
    for (int i = 0; i < 4; i++) {
      int ny = p.second.first + dy.at(i);
      int nx = p.second.second + dx.at(i);
      if (0 <= ny && ny < H && 0 <= nx && nx < W && maze[ny][nx] != '#') {
        if (chmin(ret[ny][nx], p.first)) {
          que.push(make_pair(ret[ny][nx], P(ny, nx)));
        }
      }
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int ny = p.second.first + i;
        int nx = p.second.second + j;
        if (0 <= ny && ny < H && 0 <= nx && nx < W && maze[ny][nx] != '#') {
          if (chmin(ret[ny][nx], p.first + 1)) {
            que.push(make_pair(ret[ny][nx], P(ny, nx)));
          }
        }
      }
    }
  }
}

int main() {
  cin >> H >> W;
  cin >> sy >> sx;
  cin >> gy >> gx;
  sy--;
  sx--;
  gy--;
  gx--;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> maze[i][j];
    }
  }
  solve();
  if (ret[gy][gx] == Inf) {
    cout << -1 << endl;
  } else {
    cout << ret[gy][gx] << endl;
  }
}