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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define pb push_back
using ll = long long;
using P = pair<int, int>;

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

const int INF = 1001001001;

int main() {
  int H, W;
  cin >> H >> W;
  int ch, cw;
  cin >> ch >> cw;
  int dh, dw;
  cin >> dh >> dw;
  ch--, cw--, dh--, dw--;

  vector<string> s(H);
  vector<vector<int>> dist(H, vector<int>(W, INF));
  rep(i, H) cin >> s[i];
  deque<P> q;

  q.push_back(P(ch, cw));
  dist[ch][cw] = 0;

  while (!q.empty()) {
    P p = q.front();
    q.pop_front();
    int x = p.first;
    int y = p.second;
    int tmp = dist[x][y];

    for (int i = 0; i < 4; i++) {
      int nx = x + dx[i];
      int ny = y + dy[i];
      if (nx < 0 || nx >= H || ny < 0 || ny >= W)
        continue;
      if (s[nx][ny] == '#')
        continue;
      if (dist[nx][ny] <= tmp)
        continue;

      q.push_front(P(nx, ny));
      dist[nx][ny] = min(dist[nx][ny], tmp);
    }
    for (int j = -2; j <= 2; j++) {
      for (int k = -2; k <= 2; k++) {
        int nx = x + j;
        int ny = y + k;

        if (nx < 0 || nx >= H || ny < 0 || ny >= W)
          continue;
        if (s[nx][ny] == '#')
          continue;
        if (dist[nx][ny] <= tmp + 1)
          continue;

        q.push_back(P(nx, ny));
        dist[nx][ny] = min(dist[nx][ny], tmp + 1);
      }
    }
  }
  /*
  rep(i,H){
      rep(j,H) {
          cout << cnt[i][j];
      }
      cout << endl;
  }
  */
  if (dist[dh][dw] == INF)
    cout << -1 << endl;
  else
    cout << dist[dh][dw] << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define pb push_back
using ll = long long;
using P = pair<int, int>;

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

const int INF = 1001001001;

int main() {
  int H, W;
  cin >> H >> W;
  int ch, cw;
  cin >> ch >> cw;
  int dh, dw;
  cin >> dh >> dw;
  ch--, cw--, dh--, dw--;

  vector<string> s(H);
  vector<vector<int>> cnt(H, vector<int>(W, INF));
  vector<vector<bool>> visited(H, vector<bool>(W, 0));
  rep(i, H) cin >> s[i];
  queue<P> q;

  q.push(P(ch, cw));
  visited[ch][cw] = 1;
  cnt[ch][cw] = 0;

  while (!q.empty()) {
    P p = q.front();
    q.pop();
    int x = p.first;
    int y = p.second;
    int tmp = cnt[x][y];

    for (int i = 0; i < 4; i++) {
      int nx = x + dx[i];
      int ny = y + dy[i];
      if (nx < 0 || nx >= H || ny < 0 || ny >= W)
        continue;
      if (s[nx][ny] == '#')
        continue;
      if (cnt[nx][ny] <= tmp)
        continue;

      q.push(P(nx, ny));
      cnt[nx][ny] = tmp;
    }
    for (int j = -2; j <= 2; j++) {
      for (int k = -2; k <= 2; k++) {
        int nx = x + j;
        int ny = y + k;

        if (nx < 0 || nx >= H || ny < 0 || ny >= W)
          continue;
        if (s[nx][ny] == '#')
          continue;

        if (cnt[nx][ny] <= tmp + 1)
          continue;

        q.push(P(nx, ny));
        cnt[nx][ny] = tmp + 1;
      }
    }
  }
  /*
  rep(i,H){
      rep(j,H) {
          cout << cnt[i][j];
      }
      cout << endl;
  }
  */
  if (cnt[dh][dw] == INF)
    cout << -1 << endl;
  else
    cout << cnt[dh][dw] << endl;
}