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

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

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using ll = long long;
using P = pair<ll, int>;
using Q = pair<int, P>;

#define INF 1001001001

int main() {
  int h, w;
  cin >> h >> w;
  int ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  vector<string> s(h);
  rep(i, h) cin >> s[i];
  vector<vector<int>> cost(h, vector<int>(w, INF));
  vector<vector<bool>> visit(h, vector<bool>(w));

  auto fill = [&](int x, int y, int k) {
    int dx[4] = {0, 1, 0, -1};
    int dy[4] = {1, 0, -1, 0};

    queue<P> q;
    q.push({x, y});
    cost[x][y] = k;
    while (!q.empty()) {
      int xx, yy;
      tie(xx, yy) = q.front();
      q.pop();
      rep(i, 4) {
        int nx = xx + dx[i];
        int ny = yy + dy[i];
        if (nx < 0 || nx >= h || ny < 0 || ny >= w || s[nx][ny] == '#' ||
            cost[nx][ny] != INF)
          continue;
        cost[nx][ny] = k;
        q.push({nx, ny});
      }
    }
  };

  priority_queue<Q, vector<Q>, greater<Q>> q;
  q.push({0, {ch, cw}});
  cost[ch][cw] = 0;
  visit[ch][cw] = true;
  fill(ch, cw, 0);
  while (!q.empty()) {
    int c, x, y;
    P p;
    tie(c, p) = q.top();
    q.pop();
    tie(x, y) = p;

    if (c != cost[x][y])
      continue;

    for (int nx = x - 2; nx <= x + 2; nx++) {
      for (int ny = y - 2; ny <= y + 2; ny++) {
        if (nx < 0 || nx >= h || ny < 0 || ny >= w || s[nx][ny] == '#' ||
            visit[nx][ny])
          continue;
        if (cost[nx][ny] == INF)
          fill(nx, ny, cost[x][y] + 1);
        visit[nx][ny] = true;
        q.push({cost[nx][ny], {nx, ny}});
      }
    }
  }
  int ans = cost[dh][dw];
  if (ans == INF)
    ans = -1;
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using ll = long long;
using P = pair<ll, int>;

#define INF 1001001001

int main() {
  int h, w;
  cin >> h >> w;
  int ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  vector<string> s(h);
  rep(i, h) cin >> s[i];
  vector<vector<int>> dp(h, vector<int>(w, INF));

  queue<P> q;
  q.push({ch, cw});
  dp[ch][cw] = 0;
  while (!q.empty()) {
    int x, y;
    tie(x, y) = q.front();
    q.pop();

    for (int dx = -2; dx <= 2; dx++) {
      for (int dy = -2; dy <= 2; dy++) {
        int nx = x + dx;
        int ny = y + dy;
        if (nx < 0 || nx >= h || ny < 0 || ny >= w || s[nx][ny] == '#' ||
            dp[nx][ny] <= dp[x][y])
          continue;
        int wp = !(dx == 0 && abs(dy) <= 1 || dy == 0 && abs(dx) <= 1);
        if (wp) {
          if (ny > y && s[x][y + 1] != '#')
            continue;
          if (ny < y && s[x][y - 1] != '#')
            continue;
          if (nx > x && s[x + 1][y] != '#')
            continue;
          if (nx < x && s[x - 1][y] != '#')
            continue;
        }
        if (dp[x][y] + wp < dp[nx][ny]) {
          dp[nx][ny] = dp[x][y] + wp;
          q.push({nx, ny});
        }
      }
    }
  }
  int ans = dp[dh][dw];
  if (ans == INF)
    ans = -1;
  cout << ans << endl;

  return 0;
}