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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int mvx[] = {0, 1, 0, -1};
int mvy[] = {1, 0, -1, 0};
int INF = 1e9;

int main() {
  int h, w;
  cin >> h >> w;
  P c, d;
  cin >> c.first >> c.second >> d.first >> d.second;
  c.first--;
  c.second--;
  d.first--;
  d.second--;
  char s[h][w];
  rep(i, h) rep(j, w) cin >> s[i][j];

  int dist[h][w];
  rep(i, h) rep(j, w) dist[i][j] = INF;
  dist[c.first][c.second] = 0;

  deque<P> q;
  q.emplace_back(c.first, c.second);

  while (!q.empty()) {
    P now = q.front();
    q.pop_front();
    int dis = dist[now.first][now.second];
    rep(i, 4) {
      int nx = now.first + mvx[i], ny = now.second + mvy[i];
      if (nx < 0 || nx >= h || ny < 0 || ny >= w || s[nx][ny] == '#' ||
          dist[nx][ny] <= dis)
        continue;
      dist[nx][ny] = dis;
      q.emplace_front(nx, ny);
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int nx = now.first + i, ny = now.second + j;
        if (nx < 0 || nx >= h || ny < 0 || ny >= w || s[nx][ny] == '#' ||
            dist[nx][ny] <= dis + 1)
          continue;
        dist[nx][ny] = dis + 1;
        q.emplace_back(nx, ny);
      }
    }
  }

  cout << (dist[d.first][d.second] == INF ? -1 : dist[d.first][d.second])
       << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int mvx[] = {0, 1, 0, -1};
int mvy[] = {1, 0, -1, 0};
int INF = 1e9;

int main() {
  int h, w;
  cin >> h >> w;
  P c, d;
  cin >> c.first >> c.second >> d.first >> d.second;
  c.first--;
  c.second--;
  d.first--;
  d.second--;
  char s[h][w];
  rep(i, h) rep(j, w) cin >> s[i][j];

  bool visited[h][w];
  rep(i, h) rep(j, w) visited[i][j] = false;
  int warp[h][w];
  rep(i, h) rep(j, w) warp[i][j] = -1;
  int cnt = 0;
  warp[c.first][c.second] = 0;
  while (1) {
    bool ok = false;
    rep(i, h) {
      rep(j, w) {
        if (s[i][j] == '#' || warp[i][j] != cnt)
          continue;
        ok = true;
        P start = make_pair(i, j);
        queue<P> q;
        q.push(start);
        visited[start.first][start.second] = true;
        warp[start.first][start.second] = cnt;
        bool first = true;
        while (q.size() > 0) {
          P now = q.front();
          q.pop();
          rep(i, 4) {
            int nx = now.first + mvx[i], ny = now.second + mvy[i];
            if (nx < 0 || nx >= h || ny < 0 || ny >= w || s[nx][ny] == '#' ||
                visited[nx][ny])
              continue;
            visited[nx][ny] = true;
            warp[nx][ny] = cnt;
            q.push(make_pair(nx, ny));
          }
          for (int i = -2; i <= 2; i++) {
            for (int j = -2; j <= 2; j++) {
              int nx = now.first + i, ny = now.second + j;
              if (nx < 0 || nx >= h || ny < 0 || ny >= w || s[nx][ny] == '#' ||
                  visited[nx][ny] || warp[nx][ny] != -1)
                continue;
              warp[nx][ny] = cnt + 1;
            }
          }
        }
        /*rep(i, h){
          rep(j, w) cout << warp[i][j] << " ";
          cout << endl;
        }*/
      }
    }
    cnt++;

    if (!ok)
      break;
  }
  cout << warp[d.first][d.second] << endl;

  return 0;
}