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

// revised submission of https://atcoder.jp/contests/abc176/submissions/16230212
// @sabersann

#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:

// revised submission of https://atcoder.jp/contests/abc176/submissions/16230212
// @sabersann

#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>;

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

const int INF = 1001001001;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  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];
  queue<P> q;

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

  while (!q.empty()) {
    P p = q.front();
    q.pop();
    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.emplace(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.emplace(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;
}