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

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

#define fi first
#define se second
#define pb push_back
#define sz(a) (int)(a).size()
#define mp(a, b) make_pair((a), (b))
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;

const ll MOD = 1e9 + 7;
const ll INF = 1e18;
const ll IINF = INT_MAX;

const int N = 1e3 + 5;
int dist[N][N];

int dr[] = {-1, 1, 0, 0};
int dc[] = {0, 0, -1, 1};
int h, w;
string s[N];

bool ok(int r, int c) { return r >= 0 && r < h && c >= 0 && c < w; }

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> h >> w;
  int sr, sc, er, ec;
  cin >> sr >> sc >> er >> ec;
  sr--, sc--, er--, ec--;
  int inf = h * w + 69;
  for (int i = 0; i < h; i++) {
    cin >> s[i];
    for (int j = 0; j < w; j++) {
      dist[i][j] = inf;
    }
  }

  deque<pii> q;
  dist[sr][sc] = 0;
  q.push_front(mp(sr, sc));
  while (!q.empty()) {
    pii cur = q.front();
    q.pop_front();
    for (int i = 0; i < 4; i++) {
      int nr = cur.fi + dr[i], nc = cur.se + dc[i];
      if (ok(nr, nc)) {
        if (s[nr][nc] == '.' && dist[cur.fi][cur.se] < dist[nr][nc]) {
          dist[nr][nc] = dist[cur.fi][cur.se];
          q.push_front(mp(nr, nc));
        }
      }
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        if (i == j && j == 0)
          continue;
        int a = abs(i), b = abs(j);
        if (a > b)
          swap(a, b);
        if (a == 0 && b == 1)
          continue;

        int nr = cur.fi + i, nc = cur.se + j;
        if (ok(nr, nc) && s[nr][nc] == '.' &&
            dist[cur.fi][cur.se] + 1 < dist[nr][nc]) {
          dist[nr][nc] = dist[cur.fi][cur.se] + 1;
          q.push_back(mp(nr, nc));
        }
      }
    }
  }
  if (dist[er][ec] == inf)
    dist[er][ec] = -1;
  cout << dist[er][ec] << '\\n';
  return 0;
}


Error: TLE

Fixed submission:

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

#define fi first
#define se second
#define pb push_back
#define sz(a) (int)(a).size()
#define mp(a, b) make_pair((a), (b))
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;

const ll MOD = 1e9 + 7;
const ll INF = 1e18;
const ll IINF = INT_MAX;

const int N = 1e3 + 5;
int dist[N][N];

int dr[] = {-1, 1, 0, 0};
int dc[] = {0, 0, -1, 1};
int h, w;
string s[N];
bool udh[N][N];

bool ok(int r, int c) { return r >= 0 && r < h && c >= 0 && c < w; }

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> h >> w;
  int sr, sc, er, ec;
  cin >> sr >> sc >> er >> ec;
  sr--, sc--, er--, ec--;
  int inf = h * w + 69;
  for (int i = 0; i < h; i++) {
    cin >> s[i];
    for (int j = 0; j < w; j++) {
      dist[i][j] = inf;
    }
  }

  queue<pii> q;
  dist[sr][sc] = 0;
  q.push(mp(sr, sc));
  while (!q.empty()) {
    pii cur = q.front();
    q.pop();
    udh[cur.fi][cur.se] = false;
    for (int i = 0; i < 4; i++) {
      int nr = cur.fi + dr[i], nc = cur.se + dc[i];

      if (ok(nr, nc)) {
        if (s[nr][nc] == '.' && dist[cur.fi][cur.se] < dist[nr][nc]) {
          dist[nr][nc] = dist[cur.fi][cur.se];
          if (!udh[nr][nc]) {
            q.push(mp(nr, nc));
            udh[nr][nc] = true;
          }
        }
      }
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        if (i == j && j == 0)
          continue;
        int a = abs(i), b = abs(j);
        if (a > b)
          swap(a, b);
        if (a == 0 && b == 1)
          continue;

        int nr = cur.fi + i, nc = cur.se + j;
        if (ok(nr, nc) && s[nr][nc] == '.' &&
            dist[cur.fi][cur.se] + 1 < dist[nr][nc]) {
          dist[nr][nc] = dist[cur.fi][cur.se] + 1;
          if (!udh[nr][nc]) {
            q.push(mp(nr, nc));
            udh[nr][nc] = true;
          }
        }
      }
    }
  }
  if (dist[er][ec] == inf)
    dist[er][ec] = -1;
  cout << dist[er][ec] << '\\n';
  return 0;
}