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 = 1e9;

int main() {
  int H, W;
  cin >> H >> W;
  int ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  string s[H];
  rep(i, H) cin >> s[i];

  deque<P> q;
  vector<vector<int>> dist(H, vector<int>(W, INF));
  int search[H][W];
  rep(i, H) rep(j, W) search[i][j] = 0;
  dist[ch][cw] = 0;
  search[ch][cw] = 1;
  q.emplace_back(ch, cw);

  while (q.empty() == false) {
    int x = q.front().first;
    int y = q.front().second;
    q.pop_front();
    // cost 0
    for (int i = -1; i <= 1; i++) {
      for (int j = -1; j <= 1; j++) {
        if (abs(i + j) != 1)
          continue;
        if (x + i < 0)
          continue;
        if (y + j < 0)
          continue;
        if (x + i >= H)
          continue;
        if (y + j >= W)
          continue;
        if (dist[x + i][y + j] <= dist[x][y])
          continue;
        if (search[x + i][y + j] == 0 && s[x + i][y + j] == '.') {
          dist[x + i][y + j] = dist[x][y];
          q.emplace_front(x + i, y + j);
          search[x + i][y + j] = 1;
        }
      }
    }

    // cost 1
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        if (x + i < 0)
          continue;
        if (y + j < 0)
          continue;
        if (x + i >= H)
          continue;
        if (y + j >= W)
          continue;
        if (dist[x + i][y + j] <= dist[x][y])
          continue;
        if (search[x + i][y + j] == 0 && s[x + i][y + j] == '.') {
          dist[x + i][y + j] = dist[x][y] + 1;
          q.emplace_back(x + i, y + j);
        }
      }
    }
  }
  int ans = dist[dh][dw];
  // rep(i, H){
  //     rep(j, W) cout<<dist[i][j]<<" ";
  //     cout<<endl;
  // }
  if (ans == INF)
    ans = -1;
  cout << ans << 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 = 1e9;

int main() {
  int H, W;
  cin >> H >> W;
  int ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  string s[H];
  rep(i, H) cin >> s[i];

  deque<P> q;
  vector<vector<int>> dist(H, vector<int>(W, INF));
  int search[H][W];
  rep(i, H) rep(j, W) search[i][j] = 0;
  dist[ch][cw] = 0;
  search[ch][cw] = 1;
  q.emplace_back(ch, cw);

  while (q.empty() == false) {
    int x = q.front().first;
    int y = q.front().second;
    q.pop_front();
    // cost 0
    for (int i = -1; i <= 1; i++) {
      for (int j = -1; j <= 1; j++) {
        if (abs(i + j) != 1)
          continue;
        if (x + i < 0)
          continue;
        if (y + j < 0)
          continue;
        if (x + i >= H)
          continue;
        if (y + j >= W)
          continue;
        if (dist[x + i][y + j] < dist[x][y])
          continue;
        if (search[x + i][y + j] == 0 && s[x + i][y + j] == '.') {
          dist[x + i][y + j] = dist[x][y];
          q.emplace_front(x + i, y + j);
          search[x + i][y + j] = 1;
        }
      }
    }

    // cost 1
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        if (x + i < 0)
          continue;
        if (y + j < 0)
          continue;
        if (x + i >= H)
          continue;
        if (y + j >= W)
          continue;
        if (dist[x + i][y + j] < dist[x][y])
          continue;
        if (search[x + i][y + j] == 0 && s[x + i][y + j] == '.') {
          dist[x + i][y + j] = min(dist[x + i][y + j], dist[x][y] + 1);
          q.emplace_back(x + i, y + j);
        }
      }
    }
  }
  int ans = dist[dh][dw];
  // rep(i, H){
  //     rep(j, W) cout<<dist[i][j]<<" ";
  //     cout<<endl;
  // }
  if (ans == INF)
    ans = -1;
  cout << ans << endl;
  return 0;
}