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

#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)

const int INF = 1e8;
int vx[] = {1, 0, -1, 0};
int vy[] = {0, 1, 0, -1};

int main() {
  int h, w, ch, cw, dh, dw;
  cin >> h >> w;
  cin >> ch >> cw >> 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<pair<int, int>> q;
  q.push_back({ch, cw});
  dist[ch][cw] = 0;
  while (!q.empty()) {
    int x = q.front().first;
    int y = q.front().second;
    q.pop_front();
    auto is_out = [h, w](int a, int b) {
      if (a < 0 || h <= a)
        return true;
      if (b < 0 || w <= b)
        return true;
      return false;
    };
    for (int i = 0; i < 4; ++i) {
      int nx = x + vx[i];
      int ny = y + vy[i];
      if (is_out(nx, ny))
        continue;
      if (s[nx][ny] == '#')
        continue;
      if (dist[nx][ny] <= dist[x][y])
        continue;
      dist[nx][ny] = dist[x][y];
      q.push_front({nx, ny});
    }
    for (int i = -2; i <= 2; ++i) {
      for (int j = -2; j <= 2; ++j) {
        if (i == 0 && j == 0)
          continue;
        int nx = x + i;
        int ny = y + j;
        if (is_out(nx, ny))
          continue;
        if (s[nx][ny] == '#')
          continue;
        if (dist[nx][ny] <= dist[x][y] + 1)
          continue;
        dist[nx][ny] = dist[x][y] + 1;
        q.push_back({nx, ny});
      }
    }
  }
  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 y_n(ans) cout << (ans ? "Yes" : "No") << endl;

const int INF = 1e8;

int h, w;
vector<vector<int>> cnt, color;
vector<string> s;

int vx[] = {0, 1, 0, -1};
int vy[] = {1, 0, -1, 0};

void fill_color(int x, int y, int num) {
  queue<pair<int, int>> q;
  q.push(make_pair(x, y));
  while (!q.empty()) {
    int nowx = q.front().first;
    int nowy = q.front().second;
    q.pop();
    color[nowx][nowy] = num;
    for (int i = 0; i < 4; ++i) {
      int nextx = nowx + vx[i];
      int nexty = nowy + vy[i];
      if (nextx < 0 || h <= nextx)
        continue;
      if (nexty < 0 || w <= nexty)
        continue;
      if (color[nextx][nexty] != -1)
        continue;
      if (s[nextx][nexty] == '#')
        continue;
      q.push(make_pair(nextx, nexty));
    }
  }
}

void fill_cnt(int x, int y, int num) {
  queue<pair<int, int>> q;
  q.push(make_pair(x, y));
  while (!q.empty()) {
    int nowx = q.front().first;
    int nowy = q.front().second;
    q.pop();
    cnt[nowx][nowy] = num;
    for (int i = 0; i < 4; ++i) {
      int nextx = nowx + vx[i];
      int nexty = nowy + vy[i];
      if (nextx < 0 || h <= nextx)
        continue;
      if (nexty < 0 || w <= nexty)
        continue;
      if (s[nextx][nexty] == '#')
        continue;
      if (cnt[nowx][nowy] == cnt[nextx][nexty])
        continue;
      q.push(make_pair(nextx, nexty));
    }
  }
}

int main() {
  cin >> h >> w;
  int ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  s.resize(h);
  cnt.resize(h, vector<int>(w, -1));
  color.resize(h, vector<int>(w, -1));
  rep(i, h) cin >> s[i];
  int color_num = 0;
  rep(i, h) {
    rep(j, w) {
      if (s[i][j] == '.' && color[i][j] == -1) {
        fill_color(i, j, color_num);
        fill_cnt(i, j, INF);
        color_num++;
      }
    }
  }
  bool changed = true;
  fill_cnt(ch, cw, 0);
  while (changed) {
    changed = false;
    rep(i, h) {
      rep(j, w) {
        if (s[i][j] == '#')
          continue;
        if (cnt[i][j] == INF)
          continue;
        bool is_next = false;
        for (int a = 0; a < 4; ++a) {
          int ni = i + vx[a];
          int nj = j + vy[a];
          if (ni < 0 || h <= ni)
            continue;
          if (nj < 0 || w <= nj)
            continue;
          if (s[ni][nj] == '#')
            is_next = true;
        }
        if (!is_next)
          continue;
        for (int a = -2; a <= 2; ++a) {
          for (int b = -2; b <= 2; ++b) {
            if (a == 0 && b == 0)
              continue;
            int ni = i + a;
            int nj = j + b;
            if (ni < 0 || h <= ni)
              continue;
            if (nj < 0 || w <= nj)
              continue;
            if (s[ni][nj] == '#')
              continue;
            if (cnt[i][j] >= cnt[ni][nj] - 1)
              continue;
            fill_cnt(ni, nj, cnt[i][j] + 1);
            changed = true;
          }
        }
      }
    }
  }
  if (cnt[dh][dw] == INF)
    cout << -1 << endl;
  else
    cout << cnt[dh][dw] << endl;
}