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 PI 3.14159265359
#define INF 1000100100 // 000000000
#define MOD 1000000007
#define all(x) (x).begin(), (x).end()
typedef long long ll;
#define P pair<int, int>
#define PP pair<P, P>
#define T tuple<int, int, int>
using namespace std;

int main() {
  int h, w;
  cin >> h >> w;
  int ih, iw, gh, gw;
  cin >> ih >> iw >> gh >> gw;
  ih--;
  iw--;
  gh--;
  gw--;
  vector<string> s(h);
  vector<vector<int>> check(h, vector<int>(w, INF));
  rep(i, h) cin >> s[i];

  int tr[5] = {-2, -1, 0, 1, 2};
  int trh[4] = {0, 1, 0, -1};
  int trw[4] = {1, 0, -1, 0};

  deque<P> q;
  q.push_front(P(ih, iw));
  check[ih][iw] = 0;

  while (!q.empty()) {
    int hh = q.front().first;
    int ww = q.front().second;
    q.pop_front();
    rep(i, 4) {
      int nh = hh + trh[i];
      int nw = ww + trw[i];
      if (nh < 0 || nh >= h || nw < 0 || nw >= w)
        continue;
      if (s[nh][nw] == '#')
        continue;
      if (check[nh][nw] <= check[hh][ww])
        continue;
      q.push_front(P(nh, nw));
      check[nh][nw] = check[hh][ww];
    }
    rep(i, 5) rep(j, 5) {
      int nh = hh + tr[i];
      int nw = ww + tr[j];
      if (nh < 0 || nh >= h || nw < 0 || nw >= w)
        continue;
      if (s[nh][nw] == '#')
        continue;
      if (check[nh][nw] <= check[hh][ww])
        continue;
      q.push_back(P(nh, nw));
      check[nh][nw] = check[hh][ww] + 1;
    }
  }
  if (check[gh][gw] == INF)
    cout << -1 << endl;
  else
    cout << check[gh][gw] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define PI 3.14159265359
#define INF 1000100100 // 000000000
#define MOD 1000000007
#define all(x) (x).begin(), (x).end()
typedef long long ll;
#define P pair<int, int>
#define PP pair<P, P>
#define T tuple<int, int, int>
using namespace std;

int main() {
  int h, w;
  cin >> h >> w;
  int ih, iw, gh, gw;
  cin >> ih >> iw >> gh >> gw;
  ih--;
  iw--;
  gh--;
  gw--;
  vector<string> s(h);
  vector<vector<int>> check(h, vector<int>(w, -1));
  rep(i, h) cin >> s[i];

  int tr[5] = {-2, -1, 0, 1, 2};

  queue<P> q;
  q.push(P(ih, iw));
  int ans = -1;
  for (;;) {
    ans++;
    vector<P> ok;
    if (check[gh][gw] != -1)
      break;
    while (!q.empty()) {
      int hh = q.front().first;
      int ww = q.front().second;
      q.pop();
      check[hh][ww] = 0;
      if (hh - 1 >= 0) {
        if (check[hh - 1][ww] == -1 && s[hh - 1][ww] == '.')
          q.push(P(hh - 1, ww));
      }
      if (ww - 1 >= 0) {
        if (check[hh][ww - 1] == -1 && s[hh][ww - 1] == '.')
          q.push(P(hh, ww - 1));
      }
      if (hh + 1 < h) {
        if (check[hh + 1][ww] == -1 && s[hh + 1][ww] == '.')
          q.push(P(hh + 1, ww));
      }
      if (ww + 1 < w) {
        if (check[hh][ww + 1] == -1 && s[hh][ww + 1] == '.')
          q.push(P(hh, ww + 1));
      }
      ok.push_back(P(hh, ww));
    }
    if (check[gh][gw] != -1)
      break;
    rep(i, ok.size()) {
      int nh = ok[i].first;
      int nw = ok[i].second;
      rep(j, 5) rep(z, 5) {
        int nnh = nh + tr[j];
        int nnw = nw + tr[z];
        if (nnh >= 0 && nnh < h && nnw >= 0 && nnw < w) {
          if (check[nnh][nnw] == -1 && s[nnh][nnw] == '.') {
            q.push(P(nnh, nnw));
            check[nnh][nnw]++;
          }
        }
      }
    }
    if (ok.size() == 0) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << ans << endl;
  return 0;
}