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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1000000007
using namespace std;

int main() {
  int h, w;
  cin >> h >> w;
  int ch, dh, cw, dw;
  cin >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  string s[h];
  rep(i, h) cin >> s[i];
  int flag[h][w];
  rep(i, h) rep(j, w) flag[i][j] = INF;
  int nexth[4] = {-1, 0, 1, 0};
  int nextw[4] = {0, -1, 0, 1};
  queue<pair<int, int>> que;
  queue<pair<int, int>> nque;
  que.push({ch, cw});
  flag[ch][cw] = 0;
  int iter = 0;
  while (que.empty() == 0) {
    while (que.empty() == 0) {
      pair<int, int> tmp = que.front();
      que.pop();
      int nowh = tmp.first, noww = tmp.second;
      nque.push({nowh, noww});
      iter = flag[nowh][noww];
      rep(i, 4) {
        int nh = nowh + nexth[i], nw = noww + nextw[i];
        if (nh < 0 || nh >= h || nw < 0 || nw >= w)
          continue;
        if (s[nh][nw] == '#')
          continue;
        if (flag[nh][nw] <= iter)
          continue;
        que.push({nh, nw});
        flag[nh][nw] = iter;
      }
    }
    while (nque.empty() == 0) {
      pair<int, int> tmp = nque.front();
      nque.pop();
      int nowh = tmp.first, noww = tmp.second;
      iter = flag[nowh][noww];
      for (int i = nowh - 2; i < nowh + 3; i++) {
        for (int j = noww - 2; j < noww + 3; j++) {
          if (i < 0 || i >= h || j < 0 || j >= w)
            continue;
          if (s[i][j] == '#')
            continue;
          if (flag[i][j] <= iter + 1)
            continue;
          que.push({i, j});
          flag[i][j] = iter + 1;
        }
      }
    }
  }
  if (flag[dh][dw] == INF)
    cout << "-1" << endl;
  else
    cout << flag[dh][dw] << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1000000007
using namespace std;

int main() {
  int h, w;
  cin >> h >> w;
  int ch, dh, cw, dw;
  cin >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  string s[h];
  rep(i, h) cin >> s[i];
  int flag[h][w];
  rep(i, h) rep(j, w) flag[i][j] = INF;
  int nexth[4] = {-1, 0, 1, 0};
  int nextw[4] = {0, -1, 0, 1};
  priority_queue<pair<int, pair<int, int>>> que;
  que.push({0, {ch, cw}});
  flag[ch][cw] = 0;
  int iter = 0;
  while (que.empty() == 0) {
    pair<int, pair<int, int>> now = que.top();
    que.pop();
    pair<int, int> tmp = now.second;
    int step = now.first;
    int nowh = tmp.first, noww = tmp.second;
    if (step > flag[nowh][noww])
      continue;
    iter = flag[nowh][noww]; // = iter;
    rep(i, 4) {
      int nh = nowh + nexth[i], nw = noww + nextw[i];
      if (nh < 0 || nh >= h || nw < 0 || nw >= w)
        continue;
      if (s[nh][nw] == '#')
        continue;
      if (flag[nh][nw] <= iter)
        continue;
      que.push({iter, {nh, nw}});
      flag[nh][nw] = iter;
    }
    for (int i = nowh - 2; i < nowh + 3; i++) {
      for (int j = noww - 2; j < noww + 3; j++) {
        if (i < 0 || i >= h || j < 0 || j >= w)
          continue;
        if (s[i][j] == '#')
          continue;
        if (flag[i][j] <= iter + 1)
          continue;
        que.push({iter + 1, {i, j}});
        flag[i][j] = iter + 1;
      }
    }
  }
  if (flag[dh][dw] == INF)
    cout << "-1" << endl;
  else
    cout << flag[dh][dw] << endl;
  return 0;
}