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

#include <bits/stdc++.h>
using namespace std;

// macro
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(v) v.begin(), v.end()
using ll = long long;
using P = pair<int, int>;

// code starts
#define INF 1020304050

int main() {
  int h, w;
  cin >> h >> w;
  int ch, dh, cw, dw;
  cin >> ch >> cw >> dh >> dw;
  vector<vector<char>> s(h + 10, vector<char>(w + 10));
  int i, j, k;
  rep(i, h + 10) rep(j, w + 10) {
    if (i >= 5 && i < h + 5 && j >= 5 && j < w + 5) {
      cin >> s[i][j];
    } else
      s[i][j] = '#';
  }
  ch--;
  cw--;
  dh--;
  dw--;
  vector<vector<int>> mins(h + 10, vector<int>(w + 10, INF));
  mins[ch + 5][cw + 5] = 0;
  queue<P> q;
  queue<P> q2;
  vector<int> dx = {0, 1, 0, -1};
  vector<int> dy = {1, 0, -1, 0};
  q.push(make_pair(ch + 5, cw + 5));
  int cnt = 0;
  while (!q.empty()) {
    vector<P> warp(0);
    while (!q.empty()) {
      while (!q.empty()) {
        P nowp = q.front();
        q.pop();
        int nh = nowp.first;
        int nw = nowp.second;
        warp.emplace_back(make_pair(nh, nw));
        /*for(i=nh-2;i<=nh+2;i++)
        {
                for(j=nw-2;j<=nw+2;j++)
                {
                        if(mins[i][j] == INF && s[i][j] == '.')
                        {
                                warp.emplace_back(make_pair(i,j));
                        }
                }
        }*/
        rep(i, 4) {
          int toh = nh + dy[i];
          int tow = nw + dx[i];
          if (mins[toh][tow] == INF && s[toh][tow] == '.') {
            mins[toh][tow] = mins[nh][nw];
            q2.push(make_pair(toh, tow));
          }
        }
      }
      while (!q2.empty()) {
        q.push(q2.front());
        q2.pop();
      }
    }

    for (k = 0; k < warp.size(); k++) {
      P nowp = warp[k];
      int nh = nowp.first;
      int nw = nowp.second;
      for (i = nh - 2; i <= nh + 2; i++) {
        for (j = nw - 2; j <= nw + 2; j++) {
          if (mins[i][j] == INF && s[i][j] == '.') {
            mins[i][j] = cnt + 1;
            q.push(make_pair(i, j));
          }
        }
      }
    }

    cnt++;
  }
  if (mins[dh + 5][dw + 5] == INF)
    cout << -1 << endl;
  else
    cout << mins[dh + 5][dw + 5] << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

// macro
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(v) v.begin(), v.end()
using ll = long long;
using P = pair<int, int>;

// code starts
#define INF 1020304050

int main() {
  int h, w;
  cin >> h >> w;
  int ch, dh, cw, dw;
  cin >> ch >> cw >> dh >> dw;
  vector<vector<char>> s(h + 10, vector<char>(w + 10));
  int i, j, k;
  rep(i, h + 10) rep(j, w + 10) {
    if (i >= 5 && i < h + 5 && j >= 5 && j < w + 5) {
      cin >> s[i][j];
    } else
      s[i][j] = '#';
  }
  ch--;
  cw--;
  dh--;
  dw--;
  vector<vector<int>> mins(h + 10, vector<int>(w + 10, INF));
  mins[ch + 5][cw + 5] = 0;
  queue<P> q;
  queue<P> q2;
  vector<int> dx = {0, 1, 0, -1};
  vector<int> dy = {1, 0, -1, 0};
  q.push(make_pair(ch + 5, cw + 5));
  vector<P> warp(0);
  int cnt = 0;
  while (!q.empty()) {
    while (!q.empty()) {
      while (!q.empty()) {
        P nowp = q.front();
        q.pop();
        int nh = nowp.first;
        int nw = nowp.second;
        warp.emplace_back(make_pair(nh, nw));
        /*for(i=nh-2;i<=nh+2;i++)
        {
                for(j=nw-2;j<=nw+2;j++)
                {
                        if(mins[i][j] == INF && s[i][j] == '.')
                        {
                                warp.emplace_back(make_pair(i,j));
                        }
                }
        }*/
        rep(i, 4) {
          int toh = nh + dy[i];
          int tow = nw + dx[i];
          if (mins[toh][tow] == INF && s[toh][tow] == '.') {
            mins[toh][tow] = mins[nh][nw];
            q2.push(make_pair(toh, tow));
          }
        }
      }
      while (!q2.empty()) {
        q.push(q2.front());
        q2.pop();
      }
    }

    for (k = 0; k < warp.size(); k++) {
      P nowp = warp[k];
      int nh = nowp.first;
      int nw = nowp.second;
      for (i = nh - 2; i <= nh + 2; i++) {
        for (j = nw - 2; j <= nw + 2; j++) {
          if (mins[i][j] == INF && s[i][j] == '.') {
            mins[i][j] = cnt + 1;
            q.push(make_pair(i, j));
          }
        }
      }
    }

    cnt++;
  }
  if (mins[dh + 5][dw + 5] == INF)
    cout << -1 << endl;
  else
    cout << mins[dh + 5][dw + 5] << endl;
}