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

#include <bits/stdc++.h>
#include <cctype>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, n) for (int i = 1; i <= (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rreps(i, n) for (int i = (n); i > 0; i--)
#define all(v) (v).begin(), (v).end()
#define pi 3.1415926535897932384
#define E9 1000000000
#define eps 1e-4
#define pii pair<int, int>
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
};
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
};
typedef long long int ll;
const long long INF = 1LL << 60;

int H, W;
void paint(string s[], vector<vector<int>> &num_w, set<pii> &C, int color) {
  set<pii> passed;
  queue<pii> q;
  for (auto p : C) {
    int Ch = p.first, Cw = p.second;
    passed.insert({Ch, Cw});
    q.push({Ch, Cw});
    num_w[Ch][Cw] = color;
  }
  C.clear();

  int to_r[4] = {0, 0, 1, -1}, to_c[4] = {1, -1, 0, 0};
  while (!q.empty()) {
    auto now = q.front();
    q.pop();
    int r = now.first, c = now.second;

    rep(i, 4) {
      int row = r + to_r[i], col = c + to_c[i];
      if (row < 0 || row >= H || col < 0 || col >= W)
        continue;

      if (s[row][col] == '.' && num_w[row][col] == -1) {
        num_w[row][col] = color;
        passed.insert({row, col});
        q.push({row, col});
      }
    }
  }

  for (auto now : passed) {
    int r = now.first, c = now.second;
    for (int dr = -2; dr <= 2; dr++) {
      for (int dc = -2; dc <= 2; dc++) {
        if (dr == 0 && dc == 0)
          continue;
        if (r + dr < 0 || r + dr >= H || c + dc < 0 || c + dc >= W)
          continue;

        if (s[r + dr][c + dc] == '.' && num_w[r + dr][c + dc] == -1) {
          C.insert({r + dr, c + dc});
        }
      }
    }
  }

  return;
}

int main() {
  cin >> H >> W;
  int Ch, Cw;
  cin >> Ch >> Cw;
  Ch--, Cw--;
  int Dh, Dw;
  cin >> Dh >> Dw;
  Dh--, Dw--;
  string s[H];
  rep(row, H) cin >> s[row];

  vector<vector<int>> num_w(H, vector<int>(W, -1));
  int color = 0;
  set<pii> C;
  C.insert({Ch, Cw});

  while (1) {
    paint(s, num_w, C, color);

    if (C.empty())
      break;
    color++;
  }
  cout << num_w[Dh][Dw] << endl;
  /*
  rep(r,H) rep(c,W){
    if (s[r][c]=='.') cout << num_w[r][c];
    else cout << 0;

    if (c==W-1) cout << endl;
  }*/

  // cout << fixed << setprecision(10);

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <cctype>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, n) for (int i = 1; i <= (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rreps(i, n) for (int i = (n); i > 0; i--)
#define all(v) (v).begin(), (v).end()
#define pi 3.1415926535897932384
#define E9 1000000000
#define eps 1e-4
#define pii pair<int, int>
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
};
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
};
typedef long long int ll;
const long long INF = 1LL << 60;

int main() {
  int H, W;
  cin >> H >> W;
  int Ch, Cw;
  cin >> Ch >> Cw;
  Ch--, Cw--;
  int Dh, Dw;
  cin >> Dh >> Dw;
  Dh--, Dw--;
  string s[H];
  rep(row, H) cin >> s[row];

  queue<pii> q;
  q.push({Ch, Cw});
  vector<vector<int>> num_w(H, vector<int>(W, 1000000));
  num_w[Ch][Cw] = 0;

  int to_r[4] = {0, 0, 1, -1}, to_c[4] = {1, -1, 0, 0};
  while (!q.empty()) {
    auto now = q.front();
    q.pop();
    int r = now.first, c = now.second;
    // cout << r << " " << c << endl;
    for (int dr = -2; dr <= 2; dr++) {
      for (int dc = -2; dc <= 2; dc++) {
        if (dr == 0 && dc == 0)
          continue;
        if (r + dr < 0 || r + dr >= H || c + dc < 0 || c + dc >= W)
          continue;

        if (s[r + dr][c + dc] == '.' && num_w[r + dr][c + dc] > num_w[r][c]) {
          if ((dr == 0 && abs(dc) == 1) || (dc == 0 && abs(dr) == 1)) {
            num_w[r + dr][c + dc] = num_w[r][c];
            q.push({r + dr, c + dc});
          } else if (num_w[r + dr][c + dc] > num_w[r][c] + 1) {
            num_w[r + dr][c + dc] = num_w[r][c] + 1;
            q.push({r + dr, c + dc});
          }
        }
      }
    }
  }
  if (num_w[Dh][Dw] == 1000000)
    cout << -1 << endl;
  else
    cout << num_w[Dh][Dw] << endl;

  // cout << fixed << setprecision(10);

  return 0;
}