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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int INF = 1000000000;

int main() {
  int h, w, ch, cw, dh, dw;
  cin >> h >> w >> ch >> cw >> dh >> dw;
  --ch;
  --cw;
  --dh;
  --dw;
  vector<string> s(h);
  rep(i, h) cin >> s[i];

  vector<vector<int>> dist(h, vector<int>(w, INF));
  deque<P> deq;
  deq.push_back(P(ch, cw));
  dist[ch][cw] = 0;

  while (!deq.empty()) {
    P p = deq.front();
    deq.pop_front();
    if (p.first == dh && p.second == dw)
      break;

    int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
    for (int i = 0; i < 4; i++) {
      int nx = p.first + dx[i], ny = p.second + dy[i];
      if (0 <= nx && nx < h && 0 <= ny && ny < w && s[nx][ny] != '#' &&
          (dist[nx][ny] == INF || dist[nx][ny] > dist[p.first][p.second])) {
        dist[nx][ny] = dist[p.first][p.second];
        deq.push_front(P(nx, ny));
      }
    }

    for (int i = -2; i < 3; i++) {
      for (int j = -2; j < 3; j++) {
        if (i == 0 && j == 0)
          continue;
        int nx = p.first + i, ny = p.second + j;
        if (0 <= nx && nx < h && 0 <= ny && ny < w && s[nx][ny] != '#' &&
            (dist[nx][ny] == INF ||
             dist[nx][ny] > dist[p.first][p.second] + 1)) {
          dist[nx][ny] = dist[p.first][p.second] + 1;
          deq.push_back(P(nx, ny));
        }
      }
    }
  }
  if (dist[dh][dw] == INF)
    cout << -1 << endl;
  else
    cout << dist[dh][dw] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int INF = 1000000000;

int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int d[1005][1005];

int main() {
  int H, W;
  cin >> H >> W;
  int ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  vector<string> s(H);
  for (int i = 0; i < H; i++)
    cin >> s[i];

  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++)
      d[i][j] = INF;

  queue<P> que;
  que.push(P(ch, cw));
  d[ch][cw] = 0;

  while (que.size()) {
    P p = que.front();
    que.pop();
    if (d[p.first][p.second] == d[dh][dw])
      continue;
    if (p.first == dh && p.second == dw)
      continue;

    for (int i = 0; i < 4; i++) {
      int nx = p.first + dx[i], ny = p.second + dy[i];
      if (0 <= nx && nx < H && 0 <= ny && ny < W && s[nx][ny] != '#' &&
          (d[nx][ny] == INF || d[nx][ny] > d[p.first][p.second])) {
        que.push(P(nx, ny));
        d[nx][ny] = d[p.first][p.second];
      }
    }
    for (int i = -2; i < 3; i++) {
      for (int j = -2; j < 3; j++) {
        if ((i == 0 && j == 0) || (i == 1 && j == 0) || (i == 0 && j == 1) ||
            (i == -1 && j == 0) || (i == 0 && j == -1))
          continue;
        int nx = p.first + i, ny = p.second + j;
        if (0 <= nx && nx < H && 0 <= ny && ny < W && s[nx][ny] != '#' &&
            (d[nx][ny] == INF || d[nx][ny] > (d[p.first][p.second] + 1))) {
          que.push(P(nx, ny));
          d[nx][ny] = d[p.first][p.second] + 1;
        }
      }
    }
  }
  if (d[dh][dw] == INF)
    cout << -1 << endl;
  else
    cout << d[dh][dw] << endl;
  return 0;
}