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

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

int main() {
  int H, W, Ch, Cw, Dh, Dw;
  cin >> H >> W >> Ch >> Cw >> Dh >> Dw;
  Ch--;
  Cw--;
  Dh--;
  Dw--;
  vector<string> S(H);
  for (int i = 0; i < H; i++) {
    cin >> S[i];
  }

  typedef tuple<int, int, int> T;
  const int INF = INT32_MAX / 2;
  vector<vector<int>> dist(H, vector<int>(W, INF));
  dist[Ch][Cw] = 0;
  priority_queue<T, vector<T>, greater<T>> que;
  que.push(T(0, Ch, Cw));
  int dirv[4] = {1, -1, 0, 0};
  int dirh[4] = {0, 0, 1, -1};
  while (!que.empty()) {
    int d, y, x;
    tie(d, y, x) = que.top();
    que.pop();
    if (dist[y][x] < d)
      continue;
    if (y == Dh && x == Dw)
      break;
    for (int i = 0; i < 4; i++) {
      int ny = y + dirv[i];
      int nx = x + dirh[i];
      if (ny < 0 || ny >= H || nx < 0 || nx >= W)
        continue;
      if (S[ny][nx] == '#')
        continue;
      if (dist[ny][nx] > d) {
        que.push(T(d, ny, nx));
        dist[ny][nx] = d;
      }
    }
    int sty = y - 2;
    int endy = y + 2;
    int stx = x - 2;
    int endx = x + 2;
    for (int i = sty; i <= endy; i++) {
      for (int j = stx; j <= endx; j++) {
        if (i < 0 || i >= H || j < 0 || j >= W)
          continue;
        if (S[i][j] == '#')
          continue;
        if (dist[i][j] > d + 1) {
          que.push(T(d + 1, i, j));
          dist[i][j] = d + 1;
        }
      }
    }
  }
  int ans = dist[Dh][Dw];
  if (ans == INF)
    ans = -1;

  cout << ans << endl;
}


Error: TLE

Fixed submission:

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

int main() {
  int H, W, Ch, Cw, Dh, Dw;
  cin >> H >> W >> Ch >> Cw >> Dh >> Dw;
  Ch--;
  Cw--;
  Dh--;
  Dw--;
  vector<string> S(H);
  for (int i = 0; i < H; i++) {
    cin >> S[i];
  }

  typedef pair<int, int> P;
  long long ans = 0;
  int flag = 0;
  vector<vector<int>> oldv;
  while (1) {
    vector<vector<int>> visit(H, vector<int>(W));
    queue<P> que;
    if (ans == 0) {
      que.push(P(Ch, Cw));
      visit[Ch][Cw] = 1;
    } else {
      for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
          if (oldv[i][j]) {
            int sty = i - 2;
            int endy = i + 2;
            int stx = j - 2;
            int endx = j + 2;
            for (int yy = sty; yy <= endy; yy++) {
              for (int xx = stx; xx <= endx; xx++) {
                if (yy < 0 || yy >= H || xx < 0 || xx >= W)
                  continue;
                if (visit[yy][xx])
                  continue;
                if (S[yy][xx] == '#')
                  continue;
                que.push(P(yy, xx));
                visit[yy][xx] = 1;
              }
            }
          }
        }
      }
    }
    int dirv[4] = {1, -1, 0, 0};
    int dirh[4] = {0, 0, 1, -1};
    while (!que.empty()) {
      int y, x;
      tie(y, x) = que.front();
      que.pop();
      if (y == Dh && x == Dw) {
        flag = 1;
        break;
      }
      for (int i = 0; i < 4; i++) {
        int ny = y + dirv[i];
        int nx = x + dirh[i];
        if (ny < 0 || ny >= H || nx < 0 || nx >= W)
          continue;
        if (visit[ny][nx])
          continue;
        if (S[ny][nx] == '#')
          continue;
        que.push(P(ny, nx));
        visit[ny][nx] = 1;
      }
    }
    if (flag)
      break;
    if (visit == oldv) {
      cout << -1 << endl;
      return 0;
    }
    ans++;
    oldv = visit;
  }

  cout << ans << endl;
}